Page MenuHomeFreeBSD

Don't leak memory on realloc failure
ClosedPublic

Authored by ngie on Apr 21 2016, 7:00 PM.
Tags
None
Referenced Files
Unknown Object (File)
Mar 9 2024, 1:56 AM
Unknown Object (File)
Feb 18 2024, 10:17 PM
Unknown Object (File)
Feb 11 2024, 2:07 AM
Unknown Object (File)
Feb 8 2024, 12:02 AM
Unknown Object (File)
Jan 18 2024, 1:53 PM
Unknown Object (File)
Dec 19 2023, 11:58 PM
Unknown Object (File)
Dec 18 2023, 6:17 AM
Unknown Object (File)
Sep 14 2023, 3:26 AM

Details

Reviewers
markj
jhb
araujo
Summary

Don't leak memory on realloc failure

Use reallocf instead of realloc to free on failure and deal with the
failure case more gracefully by freeing memory.

If anything fails, free buf as well as bufp.

Reported by: cppcheck
Sponsored by: EMC / Isilon Storage Division

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 3506
Build 3546: arc lint + arc unit

Event Timeline

ngie retitled this revision from to Don't leak memory on realloc failure.
ngie updated this object.
ngie edited the test plan for this revision. (Show Details)
ngie added subscribers: jhb, markj.
This revision is now accepted and ready to land.Apr 22 2016, 12:33 AM
lib/libkvm/kvm_proc.c
671

This doesn't seem right. buf is static, so we'll leak memory here if the previous call succeeded.

ngie added inline comments.
lib/libkvm/kvm_proc.c
671

Yeah, you're right. I missed that it was static... that reduces the diff a lot

ngie marked 2 inline comments as done.Apr 22 2016, 1:39 AM

I need to step back and look at this a bit more. This function is confusing.. I'll handle the other CRs first.

ngie planned changes to this revision.Apr 22 2016, 1:39 AM
lib/libkvm/kvm_proc.c
671

The idea is that the library keeps a single buffer allocated (since the caller uses the return value) and that the returned buffer remains valid until the next call to kvm_getprocs(). This is noted in the manpage.

Contrast this with kinfo_getprocs() in libutil which hands the returned buffer off to the caller which is required to free(3) it. (This seems more sane to me, but kvm_getargv()'s API is rather set at this point.)

In that sense I think you should not use reallocf() or free on failure, but keep the buffer around. However, you should fix the leak by using a temporary variable for the return value of realloc() and only updating 'bufp' and 'argc' if realloc succeeds. The code is careful to do this earlier when resizing 'buf' in the 'nchr > buflen' case at line 690.

This does mean that you can also avoid the goto's and just fix the returns to use NULL instead of 0.

ngie edited edge metadata.
ngie marked an inline comment as done.
  • Remove goto/free complexity that broke general re-use.
  • Ensure the bufp malloc succeeds before continuing; if it fails, free buf to get back to a relatively clean slate.
  • Convert the reallocf back to a realloc, and instead use a temporary buffer for capturing the result of the realloc and reassigning bufp if successful.
lib/libkvm/kvm_proc.c
685

We need to set argc before doing this.

ngie added inline comments.
lib/libkvm/kvm_proc.c
685

Derp... yes.

ngie edited edge metadata.
ngie marked 2 inline comments as done.

Move argc assignment back up.

jhb added a reviewer: jhb.
This revision is now accepted and ready to land.Apr 29 2016, 4:48 PM
markj added a reviewer: markj.