Skip to content

Commit

Permalink
use uint16_t instead of short; ref #2568
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Sep 4, 2014
2 parents 33e44a6 + 89b2764 commit 9b4ec38
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/mruby.h
Expand Up @@ -161,6 +161,7 @@ typedef struct mrb_state {
mrb_bool gc_full:1;
mrb_bool is_generational_gc_mode:1;
mrb_bool out_of_memory:1;
mrb_bool symbol_table_overflow:1;
size_t majorgc_old_threshold;
struct alloca_header *mems;

Expand Down
2 changes: 1 addition & 1 deletion include/mruby/value.h
Expand Up @@ -7,7 +7,7 @@
#ifndef MRUBY_VALUE_H
#define MRUBY_VALUE_H

typedef short mrb_sym;
typedef uint16_t mrb_sym;
typedef uint8_t mrb_bool;
struct mrb_state;

Expand Down
22 changes: 22 additions & 0 deletions src/symbol.c
Expand Up @@ -5,6 +5,7 @@
*/

#include <ctype.h>
#include <limits.h>
#include <string.h>
#include "mruby.h"
#include "mruby/khash.h"
Expand Down Expand Up @@ -44,6 +45,14 @@ sym_validate_len(mrb_state *mrb, size_t len)
}
}

#define MRB_SYM_MAX UINT16_MAX

static mrb_value
sym_tbl_overflow_new_str(mrb_state *mrb, const char *name, size_t len)
{
return mrb_str_inspect(mrb, mrb_str_new(mrb, name, len));
}

static mrb_sym
sym_intern(mrb_state *mrb, const char *name, size_t len, mrb_bool lit)
{
Expand All @@ -61,6 +70,19 @@ sym_intern(mrb_state *mrb, const char *name, size_t len, mrb_bool lit)
if (k != kh_end(h))
return kh_value(h, k);

if (mrb->symbol_table_overflow) {
if (mrb->symidx == MRB_SYM_MAX) {
mrb_bug(mrb, "symbol table overflow (symbol %S)", sym_tbl_overflow_new_str(mrb, name, len));
}
}
else {
if (mrb->symidx >= MRB_SYM_MAX - 8) { /* raising might intern a few new strings */
mrb->symbol_table_overflow = TRUE;
mrb_raisef(mrb, E_RUNTIME_ERROR, "symbol table overflow (symbol %S)",
sym_tbl_overflow_new_str(mrb, name, len));
}
}

sym = ++mrb->symidx;
if (lit) {
sname.name = name;
Expand Down

0 comments on commit 9b4ec38

Please sign in to comment.