check for corrupted mrb file data

parent 29792d17
...@@ -406,7 +406,7 @@ mrb_irep* ...@@ -406,7 +406,7 @@ mrb_irep*
mrb_read_irep(mrb_state *mrb, const uint8_t *bin) mrb_read_irep(mrb_state *mrb, const uint8_t *bin)
{ {
int result; int result;
mrb_irep *irep; mrb_irep *irep = NULL;
const struct rite_section_header *section_header; const struct rite_section_header *section_header;
uint16_t crc; uint16_t crc;
size_t bin_size = 0; size_t bin_size = 0;
...@@ -434,12 +434,14 @@ mrb_read_irep(mrb_state *mrb, const uint8_t *bin) ...@@ -434,12 +434,14 @@ mrb_read_irep(mrb_state *mrb, const uint8_t *bin)
if (!irep) return NULL; if (!irep) return NULL;
} }
else if (memcmp(section_header->section_identify, RITE_SECTION_LINENO_IDENTIFIER, sizeof(section_header->section_identify)) == 0) { else if (memcmp(section_header->section_identify, RITE_SECTION_LINENO_IDENTIFIER, sizeof(section_header->section_identify)) == 0) {
if (!irep) return NULL; /* corrupted data */
result = read_section_lineno(mrb, bin, irep); result = read_section_lineno(mrb, bin, irep);
if (result < MRB_DUMP_OK) { if (result < MRB_DUMP_OK) {
return NULL; return NULL;
} }
} }
else if (memcmp(section_header->section_identify, RITE_SECTION_DEBUG_IDENTIFIER, sizeof(section_header->section_identify)) == 0) { else if (memcmp(section_header->section_identify, RITE_SECTION_DEBUG_IDENTIFIER, sizeof(section_header->section_identify)) == 0) {
if (!irep) return NULL; /* corrupted data */
result = read_section_debug(mrb, bin, irep); result = read_section_debug(mrb, bin, irep);
if (result < MRB_DUMP_OK) { if (result < MRB_DUMP_OK) {
return NULL; return NULL;
...@@ -602,7 +604,7 @@ read_section_irep_file(mrb_state *mrb, FILE *fp) ...@@ -602,7 +604,7 @@ read_section_irep_file(mrb_state *mrb, FILE *fp)
mrb_irep* mrb_irep*
mrb_read_irep_file(mrb_state *mrb, FILE* fp) mrb_read_irep_file(mrb_state *mrb, FILE* fp)
{ {
mrb_irep *irep; mrb_irep *irep = NULL;
int result; int result;
uint8_t *buf; uint8_t *buf;
uint16_t crc, crcwk = 0; uint16_t crc, crcwk = 0;
...@@ -671,12 +673,16 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) ...@@ -671,12 +673,16 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp)
if (!irep) return NULL; if (!irep) return NULL;
} }
else if (memcmp(section_header.section_identify, RITE_SECTION_LINENO_IDENTIFIER, sizeof(section_header.section_identify)) == 0) { else if (memcmp(section_header.section_identify, RITE_SECTION_LINENO_IDENTIFIER, sizeof(section_header.section_identify)) == 0) {
if (!irep) return NULL; /* corrupted data */
fseek(fp, fpos, SEEK_SET); fseek(fp, fpos, SEEK_SET);
result = read_section_lineno_file(mrb, fp, irep); result = read_section_lineno_file(mrb, fp, irep);
if (result < MRB_DUMP_OK) return NULL; if (result < MRB_DUMP_OK) return NULL;
} }
else if (memcmp(section_header.section_identify, RITE_SECTION_DEBUG_IDENTIFIER, sizeof(section_header.section_identify)) == 0) { else if (memcmp(section_header.section_identify, RITE_SECTION_DEBUG_IDENTIFIER, sizeof(section_header.section_identify)) == 0) {
if (!irep) return NULL; /* corrupted data */
else {
uint8_t* const bin = mrb_malloc(mrb, section_size); uint8_t* const bin = mrb_malloc(mrb, section_size);
fseek(fp, fpos, SEEK_SET); fseek(fp, fpos, SEEK_SET);
if(fread((char*)bin, section_size, 1, fp) != 1) { if(fread((char*)bin, section_size, 1, fp) != 1) {
mrb_free(mrb, bin); mrb_free(mrb, bin);
...@@ -684,6 +690,7 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) ...@@ -684,6 +690,7 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp)
} }
result = read_section_debug(mrb, bin, irep); result = read_section_debug(mrb, bin, irep);
mrb_free(mrb, bin); mrb_free(mrb, bin);
}
if (result < MRB_DUMP_OK) return NULL; if (result < MRB_DUMP_OK) return NULL;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment