Skip to content

Conversation

@jamiemccarthy
Copy link
Contributor

nil is a valid second argument to JSON.pretty_generate, in fact, it's the default.

As a user of the oj gem, I would expect that after invoking Oj.mimic_JSON, this argument would still be valid.

However, at least on the current version (I didn't check earlier versions), a mimicked JSON.pretty_generate crashes ruby with a segmentation fault inside oj_mimic_pretty_generate.

This PR's first commit adds a test for this, which on my laptop ("ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux]") also crashes with a segfault.

The second commit adds a check to oj_mimic_pretty_generate to handle a nil second argument exactly as if there were no second argument. I think I did this right, at least the test passes now, but I haven't written C for a ruby extension before so of course I hope you'll correct me if there's a better way to do that.

I haven't looked into other mimicked functions to see if there are similar issues.

I'd like to give credit to @antonivanopoulos in this blog post for uncovering this issue and doing a deep-dive explaining it thoroughly.

Thanks for your attention and for providing this open-source gem to the community!

rb_raise(rb_eArgError, "wrong number of arguments (0))");
}
if (1 == argc) {
if (1 == argc || T_NIL == TYPE(argv[1])) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While that would (should) work This might be better:

if (1 == argc || Qnil == argv[1]) {

@ohler55
Copy link
Owner

ohler55 commented May 27, 2022

Thanks for the contribution. Just one suggested change.

@jamiemccarthy
Copy link
Contributor Author

Sounds good, let me know if there are any other concerns!

@ohler55 ohler55 merged commit a1218b2 into ohler55:develop May 29, 2022
@ohler55
Copy link
Owner

ohler55 commented May 29, 2022

Thanks for the improvement.

@jamiemccarthy jamiemccarthy deleted the jm-pretty-generate-nil branch April 17, 2023 13:20
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.

2 participants