From 170d5e7181cef90daa63c39b16ea66f6bd2b17b4 Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Mon, 15 Oct 2012 13:15:29 +0900 Subject: Avoid memcpy() on copying structure. --- src/load.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/load.c') diff --git a/src/load.c b/src/load.c index 6c7396407..a960e883a 100644 --- a/src/load.c +++ b/src/load.c @@ -111,22 +111,22 @@ load_rite_header(FILE* fp, rite_binary_header* bin_header, unsigned char* hcrc) if (fread(&file_header, 1, sizeof(file_header), fp) < sizeof(file_header)) { return MRB_DUMP_READ_FAULT; } - memcpy(bin_header->rbfi, file_header.rbfi, sizeof(file_header.rbfi)); + *bin_header->rbfi = *file_header.rbfi; if (memcmp(bin_header->rbfi, RITE_FILE_IDENFIFIER, sizeof(bin_header->rbfi)) != 0) { return MRB_DUMP_INVALID_FILE_HEADER; //File identifier error } - memcpy(bin_header->rbfv, file_header.rbfv, sizeof(file_header.rbfv)); + *bin_header->rbfv = *file_header.rbfv; if (memcmp(bin_header->rbfv, RITE_FILE_FORMAT_VER, sizeof(bin_header->rbfv)) != 0) { return MRB_DUMP_INVALID_FILE_HEADER; //File format version error } - memcpy(bin_header->risv, file_header.risv, sizeof(file_header.risv)); - memcpy(bin_header->rct, file_header.rct, sizeof(file_header.rct)); - memcpy(bin_header->rcv, file_header.rcv, sizeof(file_header.rcv)); + *bin_header->risv = *file_header.risv; + *bin_header->rct = *file_header.rct; + *bin_header->rcv = *file_header.rcv; hex_to_bin32(bin_header->rbds, file_header.rbds); hex_to_bin16(bin_header->nirep, file_header.nirep); hex_to_bin16(bin_header->sirep, file_header.sirep); - memcpy(bin_header->rsv, file_header.rsv, sizeof(file_header.rsv)); - memcpy(hcrc, file_header.hcrc, sizeof(file_header.hcrc)); + *bin_header->rsv = *file_header.rsv; + *hcrc = *file_header.hcrc; return MRB_DUMP_OK; } @@ -267,7 +267,7 @@ mrb_load_irep(mrb_state *mrb, FILE* fp) dst = rite_dst; memset(dst, 0x00, len); - memcpy(dst, &bin_header, sizeof(rite_binary_header)); + *(rite_binary_header *)dst = bin_header; dst += sizeof(rite_binary_header); dst += hex_to_bin16(dst, hcrc); @@ -302,7 +302,7 @@ read_rite_header(mrb_state *mrb, unsigned char *bin, rite_binary_header* bin_he { uint16_t crc; - memcpy(bin_header, bin, sizeof(rite_binary_header)); + *bin_header = *(rite_binary_header *)bin; bin += sizeof(rite_binary_header); if (memcmp(bin_header->rbfi, RITE_FILE_IDENFIFIER, sizeof(bin_header->rbfi)) != 0) { return MRB_DUMP_INVALID_FILE_HEADER; //File identifier error -- cgit v1.2.3 From e84dfd43375f1b144ae7dd1b054e9944bdf2552f Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Wed, 17 Oct 2012 21:23:42 +0900 Subject: Fix degrades by #490. (Some changes are reverted.) --- src/load.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/load.c') diff --git a/src/load.c b/src/load.c index a960e883a..a2ae4100b 100644 --- a/src/load.c +++ b/src/load.c @@ -111,22 +111,22 @@ load_rite_header(FILE* fp, rite_binary_header* bin_header, unsigned char* hcrc) if (fread(&file_header, 1, sizeof(file_header), fp) < sizeof(file_header)) { return MRB_DUMP_READ_FAULT; } - *bin_header->rbfi = *file_header.rbfi; + memcpy(bin_header->rbfi, file_header.rbfi, sizeof(file_header.rbfi)); if (memcmp(bin_header->rbfi, RITE_FILE_IDENFIFIER, sizeof(bin_header->rbfi)) != 0) { return MRB_DUMP_INVALID_FILE_HEADER; //File identifier error } - *bin_header->rbfv = *file_header.rbfv; + memcpy(bin_header->rbfv, file_header.rbfv, sizeof(file_header.rbfv)); if (memcmp(bin_header->rbfv, RITE_FILE_FORMAT_VER, sizeof(bin_header->rbfv)) != 0) { return MRB_DUMP_INVALID_FILE_HEADER; //File format version error } - *bin_header->risv = *file_header.risv; - *bin_header->rct = *file_header.rct; - *bin_header->rcv = *file_header.rcv; + memcpy(bin_header->risv, file_header.risv, sizeof(file_header.risv)); + memcpy(bin_header->rct, file_header.rct, sizeof(file_header.rct)); + memcpy(bin_header->rcv, file_header.rcv, sizeof(file_header.rcv)); hex_to_bin32(bin_header->rbds, file_header.rbds); hex_to_bin16(bin_header->nirep, file_header.nirep); hex_to_bin16(bin_header->sirep, file_header.sirep); - *bin_header->rsv = *file_header.rsv; - *hcrc = *file_header.hcrc; + memcpy(bin_header->rsv, file_header.rsv, sizeof(file_header.rsv)); + memcpy(hcrc, file_header.hcrc, sizeof(file_header.hcrc)); return MRB_DUMP_OK; } -- cgit v1.2.3 From 1933e2660c86c0731806993a9a212116bb454622 Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Wed, 24 Oct 2012 17:00:54 +0900 Subject: Use substitution instead of memset in structure initialization. --- include/mruby/data.h | 2 +- src/codegen.c | 8 ++++++-- src/gc.c | 3 ++- src/load.c | 8 ++++++-- src/parse.y | 3 ++- src/proc.c | 3 ++- src/state.c | 8 ++++++-- src/vm.c | 9 +++++---- 8 files changed, 30 insertions(+), 14 deletions(-) (limited to 'src/load.c') diff --git a/include/mruby/data.h b/include/mruby/data.h index ad91e0044..b9bedb3f9 100644 --- a/include/mruby/data.h +++ b/include/mruby/data.h @@ -30,7 +30,7 @@ struct RData *mrb_data_object_alloc(mrb_state *mrb, struct RClass* klass, void * #define Data_Make_Struct(mrb,klass,strct,type,sval) (\ sval = mrb_malloc(mrb, sizeof(strct)),\ - memset(sval, 0, sizeof(strct)),\ + { static const strct zero = { 0 }; *sval = zero},\ Data_Wrap_Struct(mrb,klass,type,sval)\ ) diff --git a/src/codegen.c b/src/codegen.c index 2d01ed701..7c3182599 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -453,7 +453,10 @@ new_sym(codegen_scope *s, mrb_sym sym) } if (s->slen > 125 && s->slen < 256) { s->syms = (mrb_sym *)codegen_realloc(s, s->syms, sizeof(mrb_sym)*65536); - memset(s->syms+s->slen, 0, sizeof(mrb_sym)*(256-s->slen)); + for (i = 0; i < 256 - s->slen; i++) { + static const mrb_sym mrb_sym_zero = { 0 }; + s->syms[i + s->slen] = mrb_sym_zero; + } s->slen = 256; } s->syms[s->slen] = sym; @@ -2049,11 +2052,12 @@ codegen(codegen_scope *s, node *tree, int val) static codegen_scope* scope_new(mrb_state *mrb, codegen_scope *prev, node *lv) { + static const codegen_scope codegen_scope_zero = { 0 }; mrb_pool *pool = mrb_pool_open(mrb); codegen_scope *p = (codegen_scope *)mrb_pool_alloc(pool, sizeof(codegen_scope)); if (!p) return 0; - memset(p, 0, sizeof(codegen_scope)); + *p = codegen_scope_zero; p->mrb = mrb; p->mpool = pool; if (!prev) return p; diff --git a/src/gc.c b/src/gc.c index 46699aea3..d92a231b8 100644 --- a/src/gc.c +++ b/src/gc.c @@ -326,6 +326,7 @@ struct RBasic* mrb_obj_alloc(mrb_state *mrb, enum mrb_vtype ttype, struct RClass *cls) { struct RBasic *p; + static const RVALUE RVALUE_zero = { 0 }; #ifdef MRB_GC_STRESS mrb_garbage_collect(mrb); @@ -345,7 +346,7 @@ mrb_obj_alloc(mrb_state *mrb, enum mrb_vtype ttype, struct RClass *cls) mrb->live++; gc_protect(mrb, p); - memset(p, 0, sizeof(RVALUE)); + *(RVALUE *)p = RVALUE_zero; p->tt = ttype; p->c = cls; paint_partial_white(mrb, p); diff --git a/src/load.c b/src/load.c index a2ae4100b..751c2619b 100644 --- a/src/load.c +++ b/src/load.c @@ -448,7 +448,10 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, mrb_irep *irep, uint32 goto error_exit; } - memset(irep->syms, 0, sizeof(mrb_sym)*(irep->slen)); + for (i = 0; i < irep->slen; i++) { + static const mrb_sym mrb_sym_zero = { 0 }; + *irep->syms = mrb_sym_zero; + } for (i=0; islen; i++) { snl = bin_to_uint16(src); //symbol name length src += MRB_DUMP_SIZE_OF_SHORT; @@ -509,11 +512,12 @@ mrb_read_irep(mrb_state *mrb, const char *bin) mrb_add_irep(mrb, sirep + nirep); for (n=0,i=sirep; nirep[i] = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep))) == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } - memset(mrb->irep[i], 0, sizeof(mrb_irep)); + *mrb->irep[i] = mrb_irep_zero; } src += sizeof(bin_header) + MRB_DUMP_SIZE_OF_SHORT; //header + crc diff --git a/src/parse.y b/src/parse.y index b903ac840..cffb6ba09 100644 --- a/src/parse.y +++ b/src/parse.y @@ -4747,13 +4747,14 @@ mrb_parser_new(mrb_state *mrb) { mrb_pool *pool; parser_state *p; + static const parser_state parser_state_zero = { 0 }; pool = mrb_pool_open(mrb); if (!pool) return 0; p = (parser_state *)mrb_pool_alloc(pool, sizeof(parser_state)); if (!p) return 0; - memset(p, 0, sizeof(parser_state)); + *p = parser_state_zero; p->mrb = mrb; p->pool = pool; p->in_def = p->in_single = 0; diff --git a/src/proc.c b/src/proc.c index 3321c2a91..07834e86d 100644 --- a/src/proc.c +++ b/src/proc.c @@ -149,11 +149,12 @@ mrb_init_proc(mrb_state *mrb) { struct RProc *m; mrb_irep *call_irep = (mrb_irep *)mrb_alloca(mrb, sizeof(mrb_irep)); + static const mrb_irep mrb_irep_zero = { 0 }; if ( call_iseq == NULL || call_irep == NULL ) return; - memset(call_irep, 0, sizeof(mrb_irep)); + *call_irep = mrb_irep_zero; call_irep->flags = MRB_ISEQ_NO_FREE; call_irep->idx = -1; call_irep->iseq = call_iseq; diff --git a/src/state.c b/src/state.c index 991be310e..8bd222fb5 100644 --- a/src/state.c +++ b/src/state.c @@ -16,10 +16,11 @@ void mrb_init_ext(mrb_state*); mrb_state* mrb_open_allocf(mrb_allocf f, void *ud) { + static const mrb_state mrb_state_zero = { 0 }; mrb_state *mrb = (mrb_state *)(f)(NULL, NULL, sizeof(mrb_state), ud); if (mrb == NULL) return NULL; - memset(mrb, 0, sizeof(mrb_state)); + *mrb = mrb_state_zero; mrb->ud = ud; mrb->allocf = f; mrb->current_white_part = MRB_GC_WHITE_A; @@ -119,12 +120,15 @@ mrb_add_irep(mrb_state *mrb, int idx) mrb->irep_capa = max; } else if (mrb->irep_capa <= idx) { + int i; size_t old_capa = mrb->irep_capa; while (mrb->irep_capa <= idx) { mrb->irep_capa *= 2; } mrb->irep = (mrb_irep **)mrb_realloc(mrb, mrb->irep, sizeof(mrb_irep*)*mrb->irep_capa); - memset(mrb->irep + old_capa, 0, sizeof(mrb_irep*) * (mrb->irep_capa - old_capa)); + for (i = old_capa; i < mrb->irep_capa - old_capa; i++) { + mrb->irep[i] = NULL; + } } } diff --git a/src/vm.c b/src/vm.c index 693e2413a..120e0d4c0 100644 --- a/src/vm.c +++ b/src/vm.c @@ -100,15 +100,16 @@ stack_extend(mrb_state *mrb, int room, int keep) envadjust(mrb, oldbase, mrb->stbase); } if (room > keep) { -#ifndef MRB_NAN_BOXING - memset(mrb->stack+keep, 0, sizeof(mrb_value) * (room-keep)); -#else int i; for (i=keep; istack[i] = mrb_value_zero; +#else SET_NIL_VALUE(mrb->stack[i]); - } #endif + } } } -- cgit v1.2.3 From 15f46a1feba74cba4f25ec99df79a3f1e07b0b09 Mon Sep 17 00:00:00 2001 From: Yuichiro MASUI Date: Sat, 27 Oct 2012 14:26:50 +0900 Subject: define convert method mrb_int/mrb_float with C string --- include/mrbconf.h | 9 ++++++++- src/codegen.c | 4 ++-- src/dump.c | 8 ++++---- src/load.c | 4 ++-- src/numeric.c | 10 +++++----- 5 files changed, 21 insertions(+), 14 deletions(-) (limited to 'src/load.c') diff --git a/include/mrbconf.h b/include/mrbconf.h index 76c1b37f9..841ef1823 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -55,19 +55,26 @@ #ifdef MRB_USE_FLOAT typedef float mrb_float; +#define mrb_float_to_str(buf, i) sprintf((buf), "%.7e", (i)) +#define str_to_mrb_float(buf) (mrb_float)strtof((buf),NULL) #else typedef double mrb_float; +#define mrb_float_to_str(buf, i) sprintf((buf), "%.16e", (i)) +#define str_to_mrb_float(buf) (mrb_float)strtod((buf),NULL) #endif -#define readfloat(p) (mrb_float)strtod((p),NULL) #ifdef MRB_NAN_BOXING typedef int32_t mrb_int; #define MRB_INT_MIN INT32_MIN #define MRB_INT_MAX INT32_MAX +#define mrb_int_to_str(buf, i) sprintf((buf), "%d", (i)) +#define str_to_mrb_int(buf) (mrb_int)strtol((buf), NULL, 10); #else typedef int mrb_int; #define MRB_INT_MIN INT_MIN #define MRB_INT_MAX INT_MAX +#define mrb_int_to_str(buf, i) sprintf((buf), "%d", (i)) +#define str_to_mrb_int(buf) (mrb_int)strtol((buf), NULL, 10); #endif typedef short mrb_sym; diff --git a/src/codegen.c b/src/codegen.c index 2d01ed701..9bba9de71 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -1738,7 +1738,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_FLOAT: if (val) { char *p = (char*)tree; - mrb_float f = readfloat(p); + mrb_float f = str_to_mrb_float(p); int off = new_lit(s, mrb_float_value(f)); genop(s, MKOP_ABx(OP_LOADL, cursp(), off)); @@ -1754,7 +1754,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_FLOAT: { char *p = (char*)tree; - mrb_float f = readfloat(p); + mrb_float f = str_to_mrb_float(p); int off = new_lit(s, mrb_float_value(-f)); genop(s, MKOP_ABx(OP_LOADL, cursp(), off)); diff --git a/src/dump.c b/src/dump.c index 15e11153e..5100014d9 100644 --- a/src/dump.c +++ b/src/dump.c @@ -226,11 +226,11 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep, int type) switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: - len = sprintf( buf, "%d", mrb_fixnum(irep->pool[pool_no])); + len = mrb_int_to_str( buf, mrb_fixnum(irep->pool[pool_no])); size += (uint32_t)len; break; case MRB_TT_FLOAT: - len = sprintf( buf, "%.16e", mrb_float(irep->pool[pool_no])); + len = mrb_float_to_str( buf, mrb_float(irep->pool[pool_no])); size += (uint32_t)len; break; case MRB_TT_STRING: @@ -346,11 +346,11 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: - len = sprintf(char_buf, "%d", mrb_fixnum(irep->pool[pool_no])); + len = mrb_int_to_str(char_buf, mrb_fixnum(irep->pool[pool_no])); break; case MRB_TT_FLOAT: - len = sprintf(char_buf, "%.16e", mrb_float(irep->pool[pool_no])); + len = mrb_float_to_str(char_buf, mrb_float(irep->pool[pool_no])); break; case MRB_TT_STRING: diff --git a/src/load.c b/src/load.c index a2ae4100b..1575ffb4d 100644 --- a/src/load.c +++ b/src/load.c @@ -405,12 +405,12 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, mrb_irep *irep, uint32 switch (tt) { //pool data case MRB_TT_FIXNUM: - fix_num = strtol(buf, NULL, 10); + fix_num = str_to_mrb_int(buf); irep->pool[i] = mrb_fixnum_value(fix_num); break; case MRB_TT_FLOAT: - f = readfloat(buf); + f = str_to_mrb_float(buf); irep->pool[i] = mrb_float_value(f); break; diff --git a/src/numeric.c b/src/numeric.c index 102e52827..c1491ac51 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -246,8 +246,8 @@ flodivmod(mrb_state *mrb, mrb_float x, mrb_float y, mrb_float *divp, mrb_float * mrb_float div, mod; if (y == 0.0) { - *divp = strtod("inf", NULL); - *modp = strtod("nan", NULL); + *divp = str_to_mrb_float("inf"); + *modp = str_to_mrb_float("nan"); return; } mod = fmod(x, y); @@ -778,7 +778,7 @@ fix_mod(mrb_state *mrb, mrb_value x) mrb_int mod; if (mrb_fixnum(y) == 0) { - return mrb_float_value(strtod("nan", NULL)); + return mrb_float_value(str_to_mrb_float("nan")); } fixdivmod(mrb, a, mrb_fixnum(y), 0, &mod); return mrb_fixnum_value(mod); @@ -807,8 +807,8 @@ fix_divmod(mrb_state *mrb, mrb_value x) mrb_int div, mod; if (mrb_fixnum(y) == 0) { - return mrb_assoc_new(mrb, mrb_float_value(strtod("inf", NULL)), - mrb_float_value(strtod("nan", NULL))); + return mrb_assoc_new(mrb, mrb_float_value(str_to_mrb_float("inf")), + mrb_float_value(str_to_mrb_float("nan"))); } fixdivmod(mrb, mrb_fixnum(x), mrb_fixnum(y), &div, &mod); return mrb_assoc_new(mrb, mrb_fixnum_value(div), mrb_fixnum_value(mod)); -- cgit v1.2.3 From 94d16805a53487a45e30cb275a9f55f9f8054669 Mon Sep 17 00:00:00 2001 From: Akira Yumiyama Date: Sat, 27 Oct 2012 17:12:32 +0900 Subject: fixes *.mrb dump/load format with escaped character. - hex-style string support - mrb format changes like: - "\n" (before: \n -> after: \n) - '\n' (before: \n -> after: \\n) --- src/dump.c | 28 +++++++++++++++++++++++----- src/load.c | 19 ++++++++++++++++--- 2 files changed, 39 insertions(+), 8 deletions(-) (limited to 'src/load.c') diff --git a/src/dump.c b/src/dump.c index 5100014d9..628550df6 100644 --- a/src/dump.c +++ b/src/dump.c @@ -6,6 +6,7 @@ #include #include "mruby/dump.h" +#include #include "mruby/string.h" #ifdef ENABLE_REGEXP @@ -119,13 +120,16 @@ uint32_dump(uint32_t bin, char *hex, int type) } } +#define CHAR_ESC_LEN 13 /* sizeof(\x{ hex of 32bit unsigned int } \0) */ + static char* str_dump(char *str, char *hex, uint16_t len, int type) { if (type == DUMP_TYPE_BIN) memcpy(hex, str, len); else { - char *src, *dst; + char *src, *dst, buf[CHAR_ESC_LEN + 1]; + int n; for (src = str, dst = hex; len > 0; src++, dst++, len--) { switch (*src) { @@ -136,11 +140,19 @@ str_dump(char *str, char *hex, uint16_t len, int type) case 0x0B:/* VT */ *dst++ = '\\'; *dst = 'v'; break; case 0x0C:/* FF */ *dst++ = '\\'; *dst = 'f'; break; case 0x0D:/* CR */ *dst++ = '\\'; *dst = 'r'; break; + case 0x5C:/* \ */ *dst++ = '\\'; *dst = '\\'; break; case 0x22:/* " */ /* fall through */ case 0x27:/* ' */ /* fall through */ // case 0x3F:/* ? */ /* fall through */ - case 0x5C:/* \ */ /* fall through */ - default: *dst = *src; break; + default: + if (*src >= ' ' && *src <= '~') { + *dst = *src; + } else { + n = sprintf(buf, "\\%03o", *src & 0377); + memcpy(dst, buf, n); + dst += (n-1); + } + break; } } } @@ -167,15 +179,21 @@ str_dump_len(char *str, uint16_t len, int type) case 0x0B:/* VT */ /* fall through */ case 0x0C:/* FF */ /* fall through */ case 0x0D:/* CR */ /* fall through */ + case 0x5C:/* \ */ /* fall through */ dump_len += 2; break; case 0x22:/* " */ /* fall through */ case 0x27:/* ' */ /* fall through */ // case 0x3F:/* ? */ /* fall through */ - case 0x5C:/* \ */ /* fall through */ default: - dump_len++; break; + if (*src >= ' ' && *src <= '~') { + dump_len++; + } else { + // dump_len += sprintf(buf, "\\%03o", *src & 0377); + dump_len += 4; + } + break; } } } diff --git a/src/load.c b/src/load.c index 4d26b02ca..61fdeed8a 100644 --- a/src/load.c +++ b/src/load.c @@ -611,8 +611,8 @@ hex_to_uint32(unsigned char *hex) static char* hex_to_str(char *hex, char *str, uint16_t *str_len) { - char *src, *dst; - int escape = 0; + char *src, *dst, buf[4]; + int escape = 0, base = 0; *str_len = 0; for (src = hex, dst = str; *src != '\0'; src++) { @@ -629,7 +629,20 @@ hex_to_str(char *hex, char *str, uint16_t *str_len) case '\'': /* fall through */ case '\?': /* fall through */ case '\\': *dst++ = *src; break; - default:break; + default: + if (*src >= '0' && *src <= '7') { + base = 8; + strncpy(buf, src, 3); + } else if (*src == 'x' || *src == 'X') { + base = 16; + src++; + strncpy(buf, src, 2); + } + + char *err_ptr; + *dst++ = (unsigned char) strtol(buf, &err_ptr, base) & 0xff; + src += (err_ptr - buf - 1); + break; } escape = 0; } else { -- cgit v1.2.3 From 30f7a93d415722ec070447d573355b2c67d71310 Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Tue, 30 Oct 2012 14:25:00 +0900 Subject: Use NULL instead of 0. (It is not a bug fix but a cosmetic change.) --- src/cdump.c | 10 +++++----- src/dump.c | 26 +++++++++++++------------- src/load.c | 6 +++--- 3 files changed, 21 insertions(+), 21 deletions(-) (limited to 'src/load.c') diff --git a/src/cdump.c b/src/cdump.c index c7597e12f..b43c86ee7 100644 --- a/src/cdump.c +++ b/src/cdump.c @@ -22,7 +22,7 @@ make_cdump_isec(mrb_state *mrb, int irep_no, FILE *f) int i; mrb_irep *irep = mrb->irep[irep_no]; - if (irep == 0) + if (irep == NULL) return -1; /* dump isec struct*/ @@ -103,11 +103,11 @@ make_cdump_irep(mrb_state *mrb, int irep_no, FILE *f) char *buf = 0; size_t buf_len, str_len; - if (irep == 0) + if (irep == NULL) return -1; buf_len = MRB_CDUMP_LINE_LEN; - if ((buf = (char *)mrb_malloc(mrb, buf_len)) == 0 ) { + if ((buf = (char *)mrb_malloc(mrb, buf_len)) == NULL) { return MRB_CDUMP_GENERAL_FAILURE; } @@ -150,7 +150,7 @@ make_cdump_irep(mrb_state *mrb, int irep_no, FILE *f) str_len = str_format_len(irep->pool[n]) + 1; if ( str_len > buf_len ) { buf_len = str_len; - if ((buf = (char *)mrb_realloc(mrb, buf, buf_len)) == 0 ) { + if ((buf = (char *)mrb_realloc(mrb, buf, buf_len)) == NULL) { return MRB_CDUMP_GENERAL_FAILURE; } } @@ -174,7 +174,7 @@ mrb_cdump_irep(mrb_state *mrb, int n, FILE *f,const char *initname) { int irep_no, irep_num; - if (mrb == 0 || n < 0 || n >= mrb->irep_len || f == 0 || initname == 0) + if (mrb == NULL || n < 0 || n >= mrb->irep_len || f == NULL || initname == NULL) return -1; irep_num = mrb->irep_len - n; diff --git a/src/dump.c b/src/dump.c index 5100014d9..bdcb739b4 100644 --- a/src/dump.c +++ b/src/dump.c @@ -335,7 +335,7 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) uint16_t len =0; buf_size = MRB_DUMP_DEFAULT_STR_LEN; - if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == 0) + if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) goto error_exit; buf += uint32_dump((uint32_t)irep->plen, buf, type); /* number of pool */ @@ -358,7 +358,7 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) len = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if (len > buf_size - 1) { buf_size = len + 1; - if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == 0) + if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == NULL) goto error_exit; memset(char_buf, 0, buf_size); } @@ -371,7 +371,7 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) len = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if ( len > buf_size - 1) { buf_size = len + 1; - if ((char_buf = mrb_realloc(mrb, char_buf, buf_size)) == 0) + if ((char_buf = mrb_realloc(mrb, char_buf, buf_size)) == NULL) goto error_exit; memset(char_buf, 0, buf_size); } @@ -405,7 +405,7 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) uint16_t buf_size =0; buf_size = MRB_DUMP_DEFAULT_STR_LEN; - if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == 0) + if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) goto error_exit; buf += uint32_dump((uint32_t)irep->slen, buf, type); /* number of symbol */ @@ -421,7 +421,7 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) nlen = str_dump_len((char*)name, len, type); if ( nlen > buf_size - 1) { buf_size = nlen + 1; - if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == 0) + if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == NULL) goto error_exit; } memset(char_buf, 0, buf_size); @@ -457,7 +457,7 @@ calc_crc_section(mrb_state *mrb, mrb_irep *irep, uint16_t *crc, int section) default: return MRB_DUMP_GENERAL_FAILURE; } - if ((buf = (char *)mrb_calloc(mrb, 1, buf_size)) == 0) + if ((buf = (char *)mrb_calloc(mrb, 1, buf_size)) == NULL) return MRB_DUMP_GENERAL_FAILURE; buf_top = buf; @@ -542,7 +542,7 @@ write_irep_record(mrb_state *mrb, int irep_no, char* bin, uint32_t *rlen, int ty mrb_irep *irep = mrb->irep[irep_no]; int section; - if (irep == 0) + if (irep == NULL) return MRB_DUMP_INVALID_IREP; /* buf alloc */ @@ -586,7 +586,7 @@ dump_irep_record(mrb_state *mrb, int irep_no, FILE* fp, uint32_t *rlen) char *buf; mrb_irep *irep = mrb->irep[irep_no]; - if (irep == 0) + if (irep == NULL) return MRB_DUMP_INVALID_IREP; /* buf alloc */ @@ -594,7 +594,7 @@ dump_irep_record(mrb_state *mrb, int irep_no, FILE* fp, uint32_t *rlen) if (irep_record_size == 0) return MRB_DUMP_GENERAL_FAILURE; - if ((buf = (char *)mrb_calloc(mrb, 1, irep_record_size)) == 0) + if ((buf = (char *)mrb_calloc(mrb, 1, irep_record_size)) == NULL) return MRB_DUMP_GENERAL_FAILURE; if ((rc = write_irep_record(mrb, irep_no, buf, rlen, DUMP_TYPE_HEX)) != MRB_DUMP_OK) { @@ -620,7 +620,7 @@ mrb_write_irep(mrb_state *mrb, int top, char *bin) int irep_no; char *bin_top; - if (mrb == 0 || top < 0 || top >= mrb->irep_len || bin == 0) + if (mrb == NULL || top < 0 || top >= mrb->irep_len || bin == NULL) return MRB_DUMP_INVALID_ARGUMENT; bin_top = bin; @@ -648,7 +648,7 @@ mrb_dump_irep(mrb_state *mrb, int top, FILE* fp) uint32_t rlen=0; /* size of irep record */ int irep_no; - if (mrb == 0 || top < 0 || top >= mrb->irep_len || fp == 0) + if (mrb == NULL || top < 0 || top >= mrb->irep_len || fp == NULL) return MRB_DUMP_INVALID_ARGUMENT; if (fwrite(&def_rite_file_header, sizeof(rite_file_header), 1, fp) != 1) /* dummy write */ @@ -678,7 +678,7 @@ mrb_bdump_irep(mrb_state *mrb, int n, FILE *f,const char *initname) int buf_size = 0; int buf_idx = 0; - if (mrb == 0 || n < 0 || n >= mrb->irep_len || f == 0 || initname == 0) + if (mrb == NULL || n < 0 || n >= mrb->irep_len || f == NULL || initname == NULL) return -1; buf_size = sizeof(rite_binary_header) + MRB_DUMP_SIZE_OF_SHORT/* crc */; @@ -686,7 +686,7 @@ mrb_bdump_irep(mrb_state *mrb, int n, FILE *f,const char *initname) buf_size += get_irep_record_size(mrb, irep_no, DUMP_TYPE_BIN); buf_size += MRB_DUMP_SIZE_OF_LONG; /* end of file */ - if ((buf = (char *)mrb_malloc(mrb, buf_size)) == 0) + if ((buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) return MRB_DUMP_GENERAL_FAILURE; rc = mrb_write_irep(mrb, n, buf); diff --git a/src/load.c b/src/load.c index 4d26b02ca..a31ef98a8 100644 --- a/src/load.c +++ b/src/load.c @@ -143,7 +143,7 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ uint16_t buf_size =0; buf_size = MRB_DUMP_DEFAULT_STR_LEN; - if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == 0) + if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) goto error_exit; pStart = dst; @@ -192,7 +192,7 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ if ( pdl > buf_size - 1) { buf_size = pdl + 1; - if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == 0) + if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == NULL) goto error_exit; } memset(char_buf, '\0', buf_size); @@ -219,7 +219,7 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ if ( snl > buf_size - 1) { buf_size = snl + 1; - if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == 0) + if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == NULL) goto error_exit; } memset(char_buf, '\0', buf_size); -- cgit v1.2.3