diff options
| -rw-r--r-- | mrbgems/mruby-struct/src/struct.c | 15 | ||||
| -rw-r--r-- | src/string.c | 2 |
2 files changed, 14 insertions, 3 deletions
diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c index 44822a03e..e1f64df55 100644 --- a/mrbgems/mruby-struct/src/struct.c +++ b/mrbgems/mruby-struct/src/struct.c @@ -123,18 +123,29 @@ mrb_struct_ref(mrb_state *mrb, mrb_value obj) static mrb_sym mrb_id_attrset(mrb_state *mrb, mrb_sym id) { +#define STACKED_ALLOC_MAX 32 +#define STACKED_STRING_MAX (STACKED_ALLOC_MAX - 1) /* '=' character */ + const char *name; char *buf; mrb_int len; mrb_sym mid; + char stacked[STACKED_ALLOC_MAX]; name = mrb_sym2name_len(mrb, id, &len); - buf = (char *)mrb_malloc(mrb, (size_t)len+1); + if (len > STACKED_STRING_MAX) { + buf = (char *)mrb_malloc(mrb, (size_t)len+1); + } + else { + buf = stacked; + } memcpy(buf, name, (size_t)len); buf[len] = '='; mid = mrb_intern(mrb, buf, len+1); - mrb_free(mrb, buf); + if (buf != stacked) { + mrb_free(mrb, buf); + } return mid; } diff --git a/src/string.c b/src/string.c index bfe73b359..ed58c484b 100644 --- a/src/string.c +++ b/src/string.c @@ -234,7 +234,7 @@ utf8len(const char* p, const char* e) mrb_int i; len = utf8len_codepage[(unsigned char)*p]; - if (p + len > e) return 1; + if (len > e - p) return 1; for (i = 1; i < len; ++i) if ((p[i] & 0xc0) != 0x80) return 1; |
