cpputest icon indicating copy to clipboard operation
cpputest copied to clipboard

getline can allocate memory that cpputest doesn't know about

Open mtfurlan opened this issue 4 years ago • 4 comments

From the man page:

If *lineptr is set to NULL and *n is set 0 before the call, then getline() will allocate a buffer for storing the line. This buffer should be freed by the user program even if getline() failed.

Alternatively, before calling getline(), *lineptr can contain a pointer to a malloc(3)-allocated buffer *n bytes in size. If the buffer is not large enough to hold the line, getline() resizes it with realloc(3), updating *lineptr and *n as necessary.

So if you pass in either NULL or a buffer that is too small, cpputest will

foo:16: error:
        Deallocating non-allocated memory
   allocated at file: <unknown> line: 0 size: 0 type: unknown

The only idea I have for fixing this is making a cpputest_getline_location that checks if the size going in and going out are the same. I think it could work, I'm just not sure if there are edge cases I'm not considering.

mtfurlan avatar Mar 19 '21 17:03 mtfurlan

is this a similar issue to #289?

pjshelton avatar Nov 06 '21 10:11 pjshelton

cpputest_getline_location is a good solution. You can make one locally and perhaps we can also include one in CppUTest. In the mean time, you can turn off the memory leak detection before feeing the buffer.

basvodde avatar Nov 23 '21 20:11 basvodde

This would also work with string?

pjshelton avatar Nov 23 '21 23:11 pjshelton

Do you mean. would this also work with std::string ? No, that seems to be a buffering issue and is usually best done by allocating a string outside of the memory leak detection scope (e.g. in main)

basvodde avatar Nov 24 '21 07:11 basvodde