1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[flori/json] Fix parser bug for empty string allocation

When `HAVE_RB_ENC_INTERNED_STR` is enabled it is possible to
pass through a null pointer to `rb_enc_interned_str` resulting
in a segfault

Fixes #495

https://github.com/flori/json/commit/b59368a8c2
This commit is contained in:
Andrew Bromwich 2022-04-20 22:30:35 +10:00 committed by Hiroshi SHIBATA
parent 767f3904ee
commit a15d0e267a
No known key found for this signature in database
GPG key ID: F9CF13417264FAC2
3 changed files with 17 additions and 0 deletions

View file

@ -2363,9 +2363,17 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
char buf[4];
if (bufferSize > MAX_STACK_BUFFER_SIZE) {
# ifdef HAVE_RB_ENC_INTERNED_STR
bufferStart = buffer = ALLOC_N(char, bufferSize ? bufferSize : 1);
# else
bufferStart = buffer = ALLOC_N(char, bufferSize);
# endif
} else {
# ifdef HAVE_RB_ENC_INTERNED_STR
bufferStart = buffer = ALLOCA_N(char, bufferSize ? bufferSize : 1);
# else
bufferStart = buffer = ALLOCA_N(char, bufferSize);
# endif
}
while (pe < stringEnd) {

View file

@ -462,9 +462,17 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
char buf[4];
if (bufferSize > MAX_STACK_BUFFER_SIZE) {
# ifdef HAVE_RB_ENC_INTERNED_STR
bufferStart = buffer = ALLOC_N(char, bufferSize ? bufferSize : 1);
# else
bufferStart = buffer = ALLOC_N(char, bufferSize);
# endif
} else {
# ifdef HAVE_RB_ENC_INTERNED_STR
bufferStart = buffer = ALLOCA_N(char, bufferSize ? bufferSize : 1);
# else
bufferStart = buffer = ALLOCA_N(char, bufferSize);
# endif
}
while (pe < stringEnd) {

View file

@ -84,6 +84,7 @@ class JSONParserTest < Test::Unit::TestCase
assert_equal({ "a" => 23 }, parse(' { "a" : 23 } '))
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
assert_equal({ "" => 123 }, parse('{"":123}'))
end
def test_parse_numbers