diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-01-08 20:49:02 -0800 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-01-08 20:49:02 -0800 |
| commit | eab894e5be20babfc5761835a6d085ec077095c0 (patch) | |
| tree | e1730a438cb6d227d4f443e09bc1913a04237741 /src/load.c | |
| parent | 2c6d34f109c556b8e825aaeb2642f4377ae2f88e (diff) | |
| parent | d5b8dc54a69a90b5f0e304fefcd0d62ca10ec64a (diff) | |
| download | mruby-eab894e5be20babfc5761835a6d085ec077095c0.tar.gz mruby-eab894e5be20babfc5761835a6d085ec077095c0.zip | |
Merge pull request #710 from monaka/pr-strict-error-checking
Strict runtime error checking
Diffstat (limited to 'src/load.c')
| -rw-r--r-- | src/load.c | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/src/load.c b/src/load.c index 1b7059a34..504246799 100644 --- a/src/load.c +++ b/src/load.c @@ -142,10 +142,14 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ unsigned char *pStart; char *char_buf; uint16_t buf_size =0; + int result; buf_size = MRB_DUMP_DEFAULT_STR_LEN; - if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) + char_buf = (char *)mrb_malloc(mrb, buf_size); + if (char_buf == NULL) { + result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; + } pStart = dst; @@ -193,8 +197,11 @@ 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)) == NULL) + char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) { + result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; + } } memset(char_buf, '\0', buf_size); rite_fgets(rfp, (unsigned char*)char_buf, pdl, FALSE); //pool @@ -220,8 +227,11 @@ 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)) == NULL) + char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) { + result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; + } } memset(char_buf, '\0', buf_size); rite_fgets(rfp, (unsigned char*)char_buf, snl, FALSE); //symbol name @@ -234,10 +244,11 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ *len = dst - pStart; + result = MRB_DUMP_OK; error_exit: mrb_free(mrb, char_buf); - return MRB_DUMP_OK; + return result; } int @@ -258,11 +269,13 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) rfp = &ritefp; //Read File Header Section - if ((ret = load_rite_header(fp, &bin_header, hcrc)) != MRB_DUMP_OK) + ret = load_rite_header(fp, &bin_header, hcrc); + if (ret != MRB_DUMP_OK) return ret; len = sizeof(rite_binary_header) + bin_to_uint32(bin_header.rbds); - if ((rite_dst = (unsigned char *)mrb_malloc(mrb, len)) == NULL) + rite_dst = (unsigned char *)mrb_malloc(mrb, len); + if (rite_dst == NULL) return MRB_DUMP_GENERAL_FAILURE; dst = rite_dst; @@ -276,7 +289,8 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) for (i=0; i<len; i++) { rite_fgets(rfp, hex8, sizeof(hex8), TRUE); //record len dst += hex_to_bin32(dst, hex8); - if ((ret = load_rite_irep_record(mrb, rfp, dst, &rlen)) != MRB_DUMP_OK) //irep info + ret = load_rite_irep_record(mrb, rfp, dst, &rlen); + if (ret != MRB_DUMP_OK) //irep info goto error_exit; dst += rlen; } @@ -334,7 +348,7 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) recordStart = src; buf = (char *)mrb_malloc(mrb, bufsize); if (buf == NULL) { - ret = MRB_DUMP_INVALID_IREP; + ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -360,7 +374,8 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) irep->ilen = bin_to_uint32(src); //iseq length src += MRB_DUMP_SIZE_OF_LONG; if (irep->ilen > 0) { - if ((irep->iseq = (mrb_code *)mrb_malloc(mrb, sizeof(mrb_code) * irep->ilen)) == NULL) { + irep->iseq = (mrb_code *)mrb_malloc(mrb, sizeof(mrb_code) * irep->ilen); + if (irep->iseq == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -383,7 +398,7 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) if (plen > 0) { irep->pool = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value) * plen); if (irep->pool == NULL) { - ret = MRB_DUMP_INVALID_IREP; + ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -395,7 +410,8 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) if (pdl > bufsize - 1) { mrb_free(mrb, buf); bufsize = pdl + 1; - if ((buf = (char *)mrb_malloc(mrb, bufsize)) == NULL) { + buf = (char *)mrb_malloc(mrb, bufsize); + if (buf == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -446,8 +462,9 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) irep->slen = bin_to_uint32(src); //syms length src += MRB_DUMP_SIZE_OF_LONG; if (irep->slen > 0) { - if ((irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen)) == NULL) { - ret = MRB_DUMP_INVALID_IREP; + irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen); + if (irep->syms == NULL) { + ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -467,7 +484,8 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) if (snl > bufsize - 1) { mrb_free(mrb, buf); bufsize = snl + 1; - if ((buf = (char *)mrb_malloc(mrb, bufsize)) == NULL) { + buf = (char *)mrb_malloc(mrb, bufsize); + if (buf == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -507,7 +525,8 @@ mrb_read_irep(mrb_state *mrb, const char *bin) sirep = mrb->irep_len; //Read File Header Section - if ((nirep = read_rite_header(mrb, src, &bin_header)) < 0) + nirep = read_rite_header(mrb, src, &bin_header); + if (nirep < 0) return nirep; src += sizeof(bin_header) + MRB_DUMP_SIZE_OF_SHORT; //header + crc @@ -515,7 +534,8 @@ mrb_read_irep(mrb_state *mrb, const char *bin) //Read Binary Data Section for (n=0,i=sirep; n<nirep; n++,i++) { src += MRB_DUMP_SIZE_OF_LONG; //record ren - if ((ret = read_rite_irep_record(mrb, src, &len)) != MRB_DUMP_OK) + ret = read_rite_irep_record(mrb, src, &len); + if (ret != MRB_DUMP_OK) goto error_exit; src += len; } |
