#164 memory leak: parseLongOption

open
nobody
None
5
2007-06-02
2007-06-02
Elliott Hughes
No

there are two allocations in parseLongOption with no matching deallocations:

static void parseLongOption (cookedArgs *const args, const char *item)
{
const char* const equal = strchr (item, '=');
if (equal == NULL)
{
args->item = eStrdup (item); /* FIXME: memory leak. */
args->parameter = "";
}
else
{
const size_t length = equal - item;
args->item = xMalloc (length + 1, char); /* FIXME: memory leak. */
strncpy (args->item, item, length);
args->item [length] = '\0';
args->parameter = equal + 1;
}
Assert (args->item != NULL);
Assert (args->parameter != NULL);
}

one choice would be to take advantage of the fact that argv is non-const, removing the eStrdup and the const in the function signature, and replacing the xMalloc line with "args->item = item". but it seems to me that a better solution would be to not molest argv and to free allocated memory.

Discussion