Skip to content

C: Check whether memory allocation was successful #2480

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

Merged
merged 2 commits into from
Apr 2, 2022

Conversation

Garfield96
Copy link
Contributor

@Garfield96 Garfield96 commented Mar 18, 2022

What problem is this PR intended to solve?

The C extension didn't check whether memory allocations were successful (= malloc/calloc return a value other than NULL). In the rare case, that memory allocation fails, this leads to undefined behavior, which usually results in a segmentation fault.

This problem was discovered using the static analyzer (-fanalyzer) of GCC.

Have you included adequate test coverage?

There is no easy way to test this behavior, hence I omitted test cases.

Does this change affect the behavior of either the C or the Java implementations?

If no more memory is available rb_eNoMemError is raised. As far as I can tell, it is not necessary to change the Java implementation, since Java would automatically raise an OutOfMemoryError exception.

@larskanis
Copy link
Member

Wouldn't it make sense to use ruby_xmalloc instead?

@Garfield96
Copy link
Contributor Author

Good idea. I don't have much experience with the Ruby API and wasn't aware of this function. I'll change it.

@Garfield96 Garfield96 marked this pull request as draft March 18, 2022 13:53
@flavorjones
Copy link
Member

flavorjones commented Mar 18, 2022

+1 we should have been using ruby_xmalloc all along (and in fact this is what we configure libxml2 to use internally).

@flavorjones
Copy link
Member

@Garfield96 Thanks so much for submitting this PR. Are you comfortable making the change that Lars suggested? If not, that's OK and I'm happy to do it.

Please also note there exists a ruby_xcalloc that would replace calloc.

@Garfield96
Copy link
Contributor Author

I started to implement it with ruby allocators and I'll push the new version tomorrow.

@Garfield96
Copy link
Contributor Author

Ruby allocators are now used everywhere, except for vasprintf. I tried to adapt the vasprintf implementation in nokogiri.c but it results in a segmentation fault. To not block this PR, I'll open another PR for vasprintf.

@Garfield96 Garfield96 marked this pull request as ready for review March 19, 2022 12:32
@flavorjones
Copy link
Member

flavorjones commented Mar 22, 2022 via email

@flavorjones flavorjones added the topic/memory Segfaults, memory leaks, valgrind testing, etc. label Apr 1, 2022
@flavorjones flavorjones requested a review from larskanis April 1, 2022 19:03
@flavorjones
Copy link
Member

I pushed a second commit that uses ruby_xfree to free the nokogiriXsltStylesheetTuple struct in xslt_stylesheet.c, which I think you missed. Otherwise this looks good to me.

I've pinged @larskanis for his review as well, because he's very familiar with memory handling in C extensions.

@flavorjones
Copy link
Member

Appveyor is green, not sure why that's not reflected here in the PR checks. I'll give Lars a chance to comment, but I'm pretty confident this is good to merge so I'm planning to do that this weekend. Thank you!

@flavorjones
Copy link
Member

Merged! Thank you for your contribution, @Garfield96. I'll credit you in the CHANGELOG.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic/memory Segfaults, memory leaks, valgrind testing, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants