Skip to content

Commit

Permalink
Fix null pointer access in Ripper#initialize
Browse files Browse the repository at this point in the history
In `rb_ruby_ripper_parser_allocate`, `r->p` is NULL between creating
`self` and `parser_params` assignment.  As GC can happen there, the
typed-data functions for it need to consider the case.
  • Loading branch information
nobu committed Jul 16, 2023
1 parent da39936 commit 5c77402
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions ext/ripper/ripper_init.c.tmpl
Expand Up @@ -25,22 +25,22 @@ static void
ripper_parser_mark2(void *ptr)
{
struct ripper *r = (struct ripper*)ptr;
ripper_parser_mark(r->p);
if (r->p) ripper_parser_mark(r->p);
}

static void
ripper_parser_free2(void *ptr)
{
struct ripper *r = (struct ripper*)ptr;
ripper_parser_free(r->p);
if (r->p) ripper_parser_free(r->p);
xfree(r);
}

static size_t
ripper_parser_memsize2(const void *ptr)
{
struct ripper *r = (struct ripper*)ptr;
return ripper_parser_memsize(r->p);
return (r->p) ? ripper_parser_memsize(r->p) : 0;
}

static const rb_data_type_t parser_data_type = {
Expand Down
7 changes: 7 additions & 0 deletions test/ripper/test_ripper.rb
Expand Up @@ -14,6 +14,13 @@ def setup
@ripper = Ripper.new '1 + 1'
end

def test_new
assert_separately(%w[-rripper], "#{<<~"begin;"}\n#{<<~'end;'}")
begin;
assert_nil EnvUtil.under_gc_stress {Ripper.new("")}.state
end;
end

def test_column
assert_nil @ripper.column
end
Expand Down

0 comments on commit 5c77402

Please sign in to comment.