Skip to content

Commit b59368a

Browse files
committed
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
1 parent 75ada77 commit b59368a

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Diff for: ext/json/ext/parser/parser.c

+8
Original file line numberDiff line numberDiff line change
@@ -2363,9 +2363,17 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
23632363
char buf[4];
23642364

23652365
if (bufferSize > MAX_STACK_BUFFER_SIZE) {
2366+
# ifdef HAVE_RB_ENC_INTERNED_STR
2367+
bufferStart = buffer = ALLOC_N(char, bufferSize ? bufferSize : 1);
2368+
# else
23662369
bufferStart = buffer = ALLOC_N(char, bufferSize);
2370+
# endif
23672371
} else {
2372+
# ifdef HAVE_RB_ENC_INTERNED_STR
2373+
bufferStart = buffer = ALLOCA_N(char, bufferSize ? bufferSize : 1);
2374+
# else
23682375
bufferStart = buffer = ALLOCA_N(char, bufferSize);
2376+
# endif
23692377
}
23702378

23712379
while (pe < stringEnd) {

Diff for: ext/json/ext/parser/parser.rl

+8
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,17 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
462462
char buf[4];
463463

464464
if (bufferSize > MAX_STACK_BUFFER_SIZE) {
465+
# ifdef HAVE_RB_ENC_INTERNED_STR
466+
bufferStart = buffer = ALLOC_N(char, bufferSize ? bufferSize : 1);
467+
# else
465468
bufferStart = buffer = ALLOC_N(char, bufferSize);
469+
# endif
466470
} else {
471+
# ifdef HAVE_RB_ENC_INTERNED_STR
472+
bufferStart = buffer = ALLOCA_N(char, bufferSize ? bufferSize : 1);
473+
# else
467474
bufferStart = buffer = ALLOCA_N(char, bufferSize);
475+
# endif
468476
}
469477

470478
while (pe < stringEnd) {

Diff for: tests/json_parser_test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def test_parse_simple_objects
8484
assert_equal({ "a" => 23 }, parse(' { "a" : 23 } '))
8585
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
8686
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
87+
assert_equal({ "" => 123 }, parse('{"":123}'))
8788
end
8889

8990
def test_parse_numbers

0 commit comments

Comments
 (0)