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/dump.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/dump.c')
| -rw-r--r-- | src/dump.c | 76 |
1 files changed, 56 insertions, 20 deletions
diff --git a/src/dump.c b/src/dump.c index 374f709ee..2885c225a 100644 --- a/src/dump.c +++ b/src/dump.c @@ -351,10 +351,14 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) char *char_buf; uint16_t buf_size =0; uint16_t len =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; + } buf += uint32_dump((uint32_t)irep->plen, buf, type); /* number of pool */ @@ -376,8 +380,11 @@ 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)) == 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); } str_dump(RSTRING_PTR(str), char_buf, RSTRING_LEN(str), type); @@ -389,8 +396,11 @@ 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)) == NULL) + char_buf = 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); } str_dump(RSTRING_PTR(str), char_buf, RSTRING_LEN(str), type); @@ -408,9 +418,10 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) buf += len; } + result = (int)(buf - buf_top); error_exit: mrb_free(mrb, char_buf); - return (int)(buf - buf_top); + return result; } static int @@ -422,7 +433,8 @@ 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)) == NULL) + char_buf = (char *)mrb_malloc(mrb, buf_size); + if (char_buf == NULL) goto error_exit; buf += uint32_dump((uint32_t)irep->slen, buf, type); /* number of symbol */ @@ -438,7 +450,8 @@ 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)) == NULL) + char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) goto error_exit; } memset(char_buf, 0, buf_size); @@ -464,6 +477,7 @@ calc_crc_section(mrb_state *mrb, mrb_irep *irep, uint16_t *crc, int section) char *buf, *buf_top; uint32_t buf_size; int type = DUMP_TYPE_BIN; + int result; switch (section) { case DUMP_IREP_HEADER: buf_size = get_irep_header_size(mrb, irep, type); break; @@ -473,24 +487,40 @@ 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)) == NULL) + buf = (char *)mrb_calloc(mrb, 1, buf_size); + if (buf == NULL) return MRB_DUMP_GENERAL_FAILURE; buf_top = buf; switch (section) { - case DUMP_IREP_HEADER: buf += write_irep_header(mrb, irep, buf, type); break; - case DUMP_ISEQ_BLOCK: buf += write_iseq_block(mrb, irep, buf, type); break; - case DUMP_POOL_BLOCK: buf += write_pool_block(mrb, irep, buf, type); break; - case DUMP_SYMS_BLOCK: buf += write_syms_block(mrb, irep, buf, type); break; - default: break; + case DUMP_IREP_HEADER: + result = write_irep_header(mrb, irep, buf, type); + break; + case DUMP_ISEQ_BLOCK: + result = write_iseq_block(mrb, irep, buf, type); + break; + case DUMP_POOL_BLOCK: + result = write_pool_block(mrb, irep, buf, type); + break; + case DUMP_SYMS_BLOCK: + result = write_syms_block(mrb, irep, buf, type); + break; + default: + break; /* Already checked above. */ } + if (result < 0) { + goto error_exit; + } + buf += result; *crc = calc_crc_16_ccitt((unsigned char*)buf_top, (int)(buf - buf_top)); mrb_free(mrb, buf_top); - return MRB_DUMP_OK; + result = MRB_DUMP_OK; + error_exit: + return result; } static uint16_t @@ -597,7 +627,8 @@ write_irep_record(mrb_state *mrb, int irep_no, char* bin, uint32_t *rlen, int ty default: break; } - if ((rc = calc_crc_section(mrb, irep, &crc, section)) != 0) + rc = calc_crc_section(mrb, irep, &crc, section); + if (rc != MRB_DUMP_OK) return rc; bin += uint16_dump(crc, bin, type); /* crc */ @@ -622,10 +653,12 @@ 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)) == NULL) + buf = (char *)mrb_calloc(mrb, 1, irep_record_size); + if (buf == NULL) return MRB_DUMP_GENERAL_FAILURE; - if ((rc = write_irep_record(mrb, irep_no, buf, rlen, DUMP_TYPE_HEX)) != MRB_DUMP_OK) { + rc = write_irep_record(mrb, irep_no, buf, rlen, DUMP_TYPE_HEX); + if (rc != MRB_DUMP_OK) { rc = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -655,7 +688,8 @@ mrb_write_irep(mrb_state *mrb, int top, char *bin) bin += sizeof(rite_binary_header) + MRB_DUMP_SIZE_OF_SHORT/* crc */; for (irep_no=top; irep_no<mrb->irep_len; irep_no++) { - if ((rc = write_irep_record(mrb, irep_no, bin, &rlen, DUMP_TYPE_BIN)) != 0) + rc = write_irep_record(mrb, irep_no, bin, &rlen, DUMP_TYPE_BIN); + if (rc != 0) return rc; bin += (rlen + DUMP_SIZE(MRB_DUMP_SIZE_OF_LONG, DUMP_TYPE_BIN)); @@ -683,7 +717,8 @@ mrb_dump_irep(mrb_state *mrb, int top, FILE* fp) return MRB_DUMP_WRITE_FAULT; for (irep_no=top; irep_no<mrb->irep_len; irep_no++) { - if ((rc = dump_irep_record(mrb, irep_no, fp, &rlen)) != 0) + rc = dump_irep_record(mrb, irep_no, fp, &rlen); + if (rc != 0) return rc; rbds += rlen; @@ -707,14 +742,15 @@ mrb_bdump_irep(mrb_state *mrb, int n, FILE *f,const char *initname) int buf_idx = 0; if (mrb == NULL || n < 0 || n >= mrb->irep_len || f == NULL || initname == NULL) - return -1; + return MRB_DUMP_INVALID_ARGUMENT; buf_size = sizeof(rite_binary_header) + MRB_DUMP_SIZE_OF_SHORT/* crc */; for (irep_no=n; irep_no<mrb->irep_len; irep_no++) 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)) == NULL) + buf = (char *)mrb_malloc(mrb, buf_size); + if (buf == NULL) return MRB_DUMP_GENERAL_FAILURE; rc = mrb_write_irep(mrb, n, buf); |
