Chris Ahlstrom <OFee…@teleworm.us> writes:
> rbowman wrote this screed in ALL-CAPS (fixed):
>> On Mon, 2 Nov 2026 14:18:27 -0500, Chris Ahlstrom wrote:
>>> It found a few issues in a couple of my projects, a few issues
>>> important.
>>> There is one that I don't get
>>>
>>> subprojects/libcfg66/src/util/filefunctions.cpp:1138:33: warning:
>>> Memory allocated by 'new[]' should be deallocated by 'delete[]',
>>> not 'getline()' [unix.MismatchedDeallocator]
>>>
>>> 1138 | ssize_t count { getline(&destination, &maxim, input) };
>>> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>
>>> I guess I need to use malloc() instead.
>>
>> Did you allocate destination with new? getline will malloc a buffer
>> if you pass in NULL.
>
> Yes, and I know that from the man page.
>
> The point is scan-build complaining about something getline() will
> not do in the given use case.
>
> A limitation of static analysis, perhaps.
There’s not enough context to give a completely definitive answer, but:
1) Heap memory can be managed with new/delete, or with
malloc/realloc/free, but not both. The consequences of playing
mix-and-match vary between platform, so you might get away with it in
some cases, by luck, but static analysis tools will consider it a
bug, if they’re smart enough to notice.
2) In getline(), if *lineptr!=NULL then it will call realloc() on it if
the line from stream needs more than *n bytes (using argument names
from [1]). So passing a pointer allocated with new to it is a bug,
for the reasons given in (1) above.
3) The diagnostic referring to deallocation reather than realloc is
probably a (trivial) bug in the tool - for example it may model
realloc as malloc+free, which isn’t unreasonable as such, but leads
to an opaque diagnostic.
The only case where it would not be a bug is if the line read by
getline() can never possibly exceed the initial allocation. Whether this
is true in your use case I don’t know, but your static analysis tool
either couldn’t prove that it was or more likely, isn’t equipped to do
so.
[1] https://man7.org/linux/man-pages/man3/getline.3.html