Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle realloc() failures #202

Closed
wants to merge 4 commits into from

Conversation

bryonglodencissp
Copy link
Contributor

[hwloc/topology-linux.c:1787]: (error) Common realloc mistake: 'maps' nulled but not freed upon failure

Passing one pointer into realloc() and assigning the result directly into that same pointer variable can cause a memory leak if the reallocation fails, because the original allocation will still exist. The correct way to do this is to use a temporary pointer variable.

Credit 1: http://stackoverflow.com/a/11548901
Credit 2: RRZE-HPC/likwid#44

Found by https://github.com/bryongloden/cppcheck

[hwloc/topology-linux.c:1787]: (error) Common realloc mistake: 'maps' nulled but not freed upon failure

Passing one pointer into realloc() and assigning the result directly into that same pointer variable can cause a memory leak if the reallocation fails, because the original allocation will still exist. The correct way to do this is to use a temporary pointer variable.

Credit 1: http://stackoverflow.com/a/11548901
Credit 2: RRZE-HPC/likwid#44

Found by https://github.com/bryongloden/cppcheck
@bgoglin
Copy link
Contributor

bgoglin commented Aug 8, 2016

Thanks, I'll apply this. That said, there are many places where we don't check the malloc return value anyway...

Does your cppcheck tool find any other issue in hwloc?

@bryonglodencissp
Copy link
Contributor Author

Greetings @bgoglin. My tool has found 14 other instances of the same error...

[hwloc/topology-linux.c:2450]: (error) Common realloc mistake: 'ret' nulled but not freed upon failure
[hwloc/topology-linux.c:3679]: (error) Common realloc mistake: 'Lprocs' nulled but not freed upon failure
[hwloc/topology-solaris.c:526]: (error) Common realloc mistake: 'Pproc' nulled but not freed upon failure
[hwloc/topology-solaris.c:538]: (error) Common realloc mistake: 'Lproc' nulled but not freed upon failure
[hwloc/topology-solaris.c:587]: (error) Common realloc mistake: 'Lpkg' nulled but not freed upon failure
[hwloc/topology-solaris.c:633]: (error) Common realloc mistake: 'Lcore' nulled but not freed upon failure
[hwloc/topology-synthetic.c:995]: (error) Common realloc mistake: 'loops' nulled but not freed upon failure
[hwloc/topology-windows.c:756]: (error) Common realloc mistake: 'procInfo' nulled but not freed upon failure
[hwloc/topology-windows.c:875]: (error) Common realloc mistake: 'procInfoTotal' nulled but not freed upon failure
[hwloc/topology-xml-nolibxml.c:353]: (error) Common realloc mistake: 'buffer' nulled but not freed upon failure
[hwloc/topology-xml-nolibxml.c:687]: (error) Common realloc mistake: 'buffer' nulled but not freed upon failure
[hwloc/topology-xml-nolibxml.c:779]: (error) Common realloc mistake: 'buffer' nulled but not freed upon failure
[hwloc/topology.c:290]: (error) Common realloc mistake: 'infos' nulled but not freed upon failure
[hwloc/topology.c:320]: (error) Common realloc mistake: 'dst_infos' nulled but not freed upon failure

I believe it is a good idea to try fixing one before I trying anymore of them. This way, if there was a problem with collaboration, at least I wouldn't sacrifice too much time. Plus, I believe that sometimes people download the tool for themselves and use it to fix error themselves. Unfortunately, one of the obstacles on my end is that I'm unable to test my code changes. For example, when I changed this one file, topology-linux.c, and I ran the build process:

./autogen.sh
./configure
make
make install
lstopo

my system, an iMac running OS X El Capitan, just didn't compile the file. I'm not sure I know enough to know why it didn't compile, I just know that when I introduced a programming error into the file, and then rerun the previous commands, I wasn't presented with a build error (which is what I was expecting). I presume it didn't compile this "linux" file because I'm running a different OS.

All that being said. I'm happy to dive in and start fixing some of these, but I believe I need someone else to review my code changes (for my sanity and yours)!!!

For what it's worth, this tool also found this:

[hwloc/topology-solaris.c:431]: (error) Memory leak: glob_lgrps

I haven't look at the file yet, but these are usually easy to fix. I just wanted to get this comment out while I still had your attention 👍

bryonglodencissp pushed a commit to bryonglodencissp/hwloc that referenced this pull request Aug 8, 2016
Hey there @bgoglin, I am able to test this locally, and my patch is building. This is one of the 14 realloc() errors I mentioned in open-mpi#202. I realize this may not be the most efficient way to propose a file change, but I'm unable to edit https://github.com/open-mpi/hwloc/blob/master/hwloc/topology-synthetic.c and amend it to open-mpi#202 using the website GUI. If this is an issue, give me a shout out, and maybe we can come up with something different for future patches related to open-mpi#202.
[hwloc/topology-xml-nolibxml.c:353]: (error) Common realloc mistake: 'buffer' nulled but not freed upon failure
@bgoglin bgoglin changed the title Update topology-linux.c Properly handle realloc() failures Aug 9, 2016
Bryon Gloden, CISSP® added 2 commits August 9, 2016 18:57
[hwloc/topology-xml-nolibxml.c:687]: (error) Common realloc mistake: 'buffer' nulled but not freed upon failure

NOTE: the line numbers are changing as consecutive patches are merged into the same file. For housekeeping, I'm using the original 'cppcheck' output in this commit message.
[hwloc/topology-xml-nolibxml.c:779]: (error) Common realloc mistake: 'buffer' nulled but not freed upon failure

NOTE:  using the original 'cppcheck' output in this commit message again
@bgoglin
Copy link
Contributor

bgoglin commented Aug 9, 2016

@bryongloden I already have patches in my local tree for most realloc() calls including that one in nolibxml.c, I'll push everything to master and v1.11 tomorrow after more testing.

@bryonglodencissp
Copy link
Contributor Author

Bonjour @bgoglin! Sorry I don't speak French, so bear with me through my English 👍

I was interested in testing the idea in your comment from #204. Namely, updating a branch via the GitHub GUI. Now that I know that works, I'll stop patching realloc() errors in the hwloc/hwloc directory 👍

Also, when you asked earlier if my tool found any more errors in hwloc, did you mean specifically in the hwloc/hwloc directory? Because that is/was the way I interpreted your question.

I've run the tool now across the entire hwloc repository and I've discovered errors/bugs inside hwloc/netloc, hwloc/tests, and hwloc/utils.

If you ping me tomorrow after you push your local edits, I'll sync up with master, rerun my tool, and open a new issue that includes a full list of all the errors I see. I believe this is the most efficient way for me to report the bugs to you (otherwise just download and build cppcheck yourself... and I'll go away) 👍

bgoglin added a commit that referenced this pull request Aug 10, 2016
Found by https://github.com/bryongloden/cppcheck

Thanks to Bryon Golden for reporting the issue.

Refs #202.
bgoglin added a commit that referenced this pull request Aug 10, 2016
bgoglin added a commit that referenced this pull request Aug 10, 2016
@bgoglin
Copy link
Contributor

bgoglin commented Aug 10, 2016

I pushed all realloc() fixes.
Some realloc() remain unfixed in the netloc lib and utils but this code is going to be deeply revamped, so fixing realloc() there is useless right now.

@bgoglin bgoglin closed this Aug 10, 2016
bgoglin added a commit that referenced this pull request Aug 10, 2016
Found by https://github.com/bryongloden/cppcheck

Thanks to Bryon Golden for reporting the issue.

Refs #202.

(cherry picked from commit 63592bd)
bgoglin added a commit that referenced this pull request Aug 10, 2016
bgoglin added a commit that referenced this pull request Aug 10, 2016
bgoglin added a commit that referenced this pull request Aug 10, 2016
bgoglin added a commit that referenced this pull request Aug 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants