Skip to content

Commit

Permalink
Fix a double free problem in codegen.c; fix #3378
Browse files Browse the repository at this point in the history
This issue was first reported by https://hackerone.com/geeknik
The fix was proposed by @titanous
  • Loading branch information
matz committed Jan 23, 2017
1 parent 3ce8260 commit f0f095b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/mruby/irep.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ typedef struct mrb_irep {

struct mrb_locals *lv;
/* debug info */
mrb_bool own_filename;
const char *filename;
uint16_t *lines;
struct mrb_irep_debug_info* debug_info;
Expand Down
4 changes: 1 addition & 3 deletions mrbgems/mruby-compiler/core/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2844,6 +2844,7 @@ scope_finish(codegen_scope *s)
memcpy(fname, s->filename, fname_len);
fname[fname_len] = '\0';
irep->filename = fname;
irep->own_filename = TRUE;
}

irep->nlocals = s->nlocals;
Expand Down Expand Up @@ -2951,9 +2952,6 @@ mrb_generate_code(mrb_state *mrb, parser_state *p)
return proc;
}
MRB_CATCH(&scope->jmp) {
if (scope->filename == scope->irep->filename) {
scope->irep->filename = NULL;
}
mrb_irep_decref(mrb, scope->irep);
mrb_pool_close(scope->mpool);
return NULL;
Expand Down
5 changes: 4 additions & 1 deletion src/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ mrb_irep_free(mrb_state *mrb, mrb_irep *irep)
}
mrb_free(mrb, irep->reps);
mrb_free(mrb, irep->lv);
mrb_free(mrb, (void *)irep->filename);
if (irep->own_filename) {
mrb_free(mrb, (void *)irep->filename);
}
mrb_free(mrb, irep->lines);
mrb_debug_info_free(mrb, irep->debug_info);
mrb_free(mrb, irep);
Expand Down Expand Up @@ -261,6 +263,7 @@ mrb_add_irep(mrb_state *mrb)
irep = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep));
*irep = mrb_irep_zero;
irep->refcnt = 1;
irep->own_filename = FALSE;

return irep;
}
Expand Down

0 comments on commit f0f095b

Please sign in to comment.