sds
sds copied to clipboard
memory leak in sdssplitlen() ?
In sdssplitlen()
, slots
starts with a default of 5. Then
tokens = malloc(sizeof(sds)*slots);
or generally 40 bytes.
However, if (len == 0) { *count = 0; return tokens;}
How do we free the 40 bytes? sdsfreesplitres()
will use a count of 0.
Am I wrong that this is technically a memory leak? Of course, this is a situation that will probably never come up, since who splits a string with 0 chars, but still, it looks incorrect.
Why do we allocate memory to tokens
before the test for len == 0
?
vtavares00dev,
Actually there is no leak since sdsfreesplitres
calls free(tokens);
in the end thus always freeing memory allocated for tokens.
However the current behavior seems odd to me since it contradicts the docstring:
On out of memory, zero length string, zero length
separator, NULL is returned.
and thus in the case of input zero length string non-NULL is returned. I suppose it needs to be fixed to bring it along the lines with the documentation.