diff --git a/libr/bin/format/pe/pe.c b/libr/bin/format/pe/pe.c index ba582c823d..89f3ba2868 100644 --- a/libr/bin/format/pe/pe.c +++ b/libr/bin/format/pe/pe.c @@ -116,7 +116,7 @@ static int PE_(r_bin_pe_parse_imports)(struct PE_(r_bin_pe_obj_t)* bin, struct r snprintf (import_name, PE_NAME_LENGTH, "%s_%s", dll_name, name); } if (!(*importp = realloc (*importp, (*nimp+1) * sizeof(struct r_bin_pe_import_t)))) { - perror ("realloc (import)"); + r_sys_perror ("realloc (import)"); return R_FALSE; } memcpy((*importp)[*nimp].name, import_name, PE_NAME_LENGTH); @@ -134,7 +134,7 @@ static int PE_(r_bin_pe_parse_imports)(struct PE_(r_bin_pe_obj_t)* bin, struct r static int PE_(r_bin_pe_init_hdr)(struct PE_(r_bin_pe_obj_t)* bin) { if (!(bin->dos_header = malloc(sizeof(PE_(image_dos_header))))) { - perror ("malloc (dos header)"); + r_sys_perror ("malloc (dos header)"); return R_FALSE; } if (r_buf_read_at (bin->b, 0, (ut8*)bin->dos_header, sizeof(PE_(image_dos_header))) == -1) { @@ -146,7 +146,7 @@ static int PE_(r_bin_pe_init_hdr)(struct PE_(r_bin_pe_obj_t)* bin) { return R_FALSE; } if (!(bin->nt_headers = malloc(sizeof(PE_(image_nt_headers))))) { - perror("malloc (nt header)"); + r_sys_perror("malloc (nt header)"); return R_FALSE; } if (r_buf_read_at (bin->b, bin->dos_header->e_lfanew, @@ -280,7 +280,7 @@ static int PE_(r_bin_pe_init_sections)(struct PE_(r_bin_pe_obj_t)* bin) { return R_FALSE; } if (!(bin->section_header = malloc (sections_size))) { - perror ("malloc (section header)"); + r_sys_perror ("malloc (section header)"); return R_FALSE; } if (r_buf_read_at (bin->b, bin->dos_header->e_lfanew + 4 + sizeof (PE_(image_file_header)) + @@ -331,20 +331,23 @@ struct symrec { static int PE_(r_bin_pe_init_imports)(struct PE_(r_bin_pe_obj_t) *bin) { PE_(image_data_directory) *data_dir_import = \ - &bin->nt_headers->optional_header.DataDirectory[PE_IMAGE_DIRECTORY_ENTRY_IMPORT]; + &bin->nt_headers->optional_header.DataDirectory[ \ + PE_IMAGE_DIRECTORY_ENTRY_IMPORT]; PE_(image_data_directory) *data_dir_delay_import = \ - &bin->nt_headers->optional_header.DataDirectory[PE_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT]; - PE_DWord import_dir_paddr = PE_(r_bin_pe_vaddr_to_paddr)(bin, data_dir_import->VirtualAddress); - PE_DWord delay_import_dir_paddr = PE_(r_bin_pe_vaddr_to_paddr)(bin, data_dir_delay_import->VirtualAddress); - - PE_DWord import_dir_offset = PE_(r_bin_pe_vaddr_to_paddr)(bin, data_dir_import->VirtualAddress); - PE_DWord delay_import_dir_offset = PE_(r_bin_pe_vaddr_to_paddr)(bin, data_dir_delay_import->VirtualAddress); - PE_(image_import_directory) *import_dir = 0; - PE_(image_import_directory) *curr_import_dir = 0; - PE_(image_delay_import_directory) *delay_import_dir = 0; - PE_(image_delay_import_directory) *curr_delay_import_dir = 0; - int dir_size = sizeof(PE_(image_import_directory)); - int delay_import_size = sizeof(PE_(image_delay_import_directory)); + &bin->nt_headers->optional_header.DataDirectory[\ + PE_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT]; + PE_DWord import_dir_paddr = PE_(r_bin_pe_vaddr_to_paddr)(bin, + data_dir_import->VirtualAddress); + PE_DWord import_dir_offset = PE_(r_bin_pe_vaddr_to_paddr)(bin, + data_dir_import->VirtualAddress); + PE_DWord delay_import_dir_offset = PE_(r_bin_pe_vaddr_to_paddr)(bin, + data_dir_delay_import->VirtualAddress); + PE_(image_import_directory) *import_dir = NULL; + PE_(image_import_directory) *curr_import_dir = NULL; + PE_(image_delay_import_directory) *delay_import_dir = NULL; + PE_(image_delay_import_directory) *curr_delay_import_dir = NULL; + int dir_size = sizeof (PE_(image_import_directory)); + int delay_import_size = sizeof (PE_(image_delay_import_directory)); int indx = 0; int import_dir_size = data_dir_import->Size; @@ -361,26 +364,25 @@ static int PE_(r_bin_pe_init_imports)(struct PE_(r_bin_pe_obj_t) *bin) { if (import_dir_paddr != 0) { if (import_dir_size<1 || import_dir_size>0xffff) { - eprintf ("Warning: Invalid import directory size: 0x%x\n", import_dir_size); + eprintf ("Warning: Invalid import directory size: 0x%x\n", + import_dir_size); import_dir_size = 0xffff; } do { indx++; - import_dir = (PE_(image_import_directory) *)realloc(import_dir, - (indx * dir_size)); - - if (import_dir == 0) { - perror("malloc (import directory)"); - return R_FALSE; + import_dir = (PE_(image_import_directory) *)realloc ( + import_dir, (indx * dir_size)+1); + if (!import_dir) { + r_sys_perror ("malloc (import directory)"); + goto fail; } curr_import_dir = import_dir + (indx - 1); - if (r_buf_read_at( bin->b, - import_dir_offset + (indx - 1) * dir_size, - (ut8*)(curr_import_dir), - dir_size) == -1) { - eprintf("Error: read (import directory)\n"); + if (r_buf_read_at (bin->b, import_dir_offset + (indx - 1) * dir_size, + (ut8*)(curr_import_dir), dir_size) == -1) { + eprintf ("Error: read (import directory)\n"); + free (import_dir); return R_FALSE; } } while ((curr_import_dir->Characteristics != 0) && (curr_import_dir->Name != 0)); @@ -393,19 +395,18 @@ static int PE_(r_bin_pe_init_imports)(struct PE_(r_bin_pe_obj_t) *bin) { do { indx++; - delay_import_dir = (PE_(image_delay_import_directory) *)realloc(delay_import_dir, - (indx * delay_import_size)); + delay_import_dir = (PE_(image_delay_import_directory) *)realloc ( + delay_import_dir, (indx * delay_import_size)+1); if (delay_import_dir == 0) { - perror("malloc (delay import directory)"); + r_sys_perror ("malloc (delay import directory)"); + free (delay_import_dir); return R_FALSE; } curr_delay_import_dir = delay_import_dir + (indx - 1); - if (r_buf_read_at( bin->b, - delay_import_dir_offset + (indx - 1) * delay_import_size, - (ut8*)(curr_delay_import_dir), - dir_size) == -1) { + if (r_buf_read_at (bin->b, delay_import_dir_offset + (indx - 1) * delay_import_size, + (ut8*)(curr_delay_import_dir), dir_size) == -1) { eprintf("Error: read (delay import directory)\n"); return R_FALSE; } @@ -415,6 +416,10 @@ static int PE_(r_bin_pe_init_imports)(struct PE_(r_bin_pe_obj_t) *bin) { } return R_TRUE; +fail: + free (import_dir); + free (delay_import_dir); + return R_FALSE; } static int PE_(r_bin_pe_init_exports)(struct PE_(r_bin_pe_obj_t) *bin) { @@ -528,7 +533,7 @@ printf ("SYMBOL 0x%x = %d (%s)\n", (ut32)si->n_value, (int)si->n_strx, //sdb_setn (DB, "hdr.exports_directory", export_dir_paddr); //eprintf ("Pexports paddr at 0x%"PFMT64x"\n", export_dir_paddr); if (!(bin->export_directory = malloc(sizeof(PE_(image_export_directory))))) { - perror ("malloc (export directory)"); + r_sys_perror ("malloc (export directory)"); return R_FALSE; } if (r_buf_read_at (bin->b, export_dir_paddr, (ut8*)bin->export_directory, @@ -601,7 +606,7 @@ struct r_bin_pe_addr_t* PE_(r_bin_pe_get_entrypoint)(struct PE_(r_bin_pe_obj_t)* if (!bin || !bin->nt_headers) return NULL; if ((entry = malloc(sizeof(struct r_bin_pe_addr_t))) == NULL) { - perror("malloc (entrypoint)"); + r_sys_perror("malloc (entrypoint)"); return NULL; } entry->vaddr = bin->nt_headers->optional_header.AddressOfEntryPoint; @@ -705,25 +710,23 @@ struct r_bin_pe_import_t* PE_(r_bin_pe_get_imports)(struct PE_(r_bin_pe_obj_t) * struct r_bin_pe_import_t *imps, *imports = NULL; char dll_name[PE_NAME_LENGTH + 1]; int nimp = 0; - PE_DWord dll_name_offset; + PE_DWord dll_name_offset = 0; PE_DWord import_func_name_offset; - PE_(image_import_directory) *curr_import_dir = 0; + PE_(image_import_directory) *curr_import_dir = NULL; PE_(image_delay_import_directory) *curr_delay_import_dir = 0; if (bin->import_directory) { curr_import_dir = bin->import_directory; while ((curr_import_dir->Characteristics != 0) && (dll_name_offset != 0)) { dll_name_offset = curr_import_dir->Name; - if (r_buf_read_at( bin->b, PE_(r_bin_pe_vaddr_to_paddr)(bin, dll_name_offset), - (ut8*)dll_name, PE_NAME_LENGTH) == -1) { + if (r_buf_read_at (bin->b, PE_(r_bin_pe_vaddr_to_paddr)(bin, dll_name_offset), + (ut8*)dll_name, PE_NAME_LENGTH) == -1) { eprintf("Error: read (magic)\n"); return NULL; } - - if (!PE_(r_bin_pe_parse_imports)( bin, &imports, &nimp, dll_name, - curr_import_dir->Characteristics, curr_import_dir->FirstThunk)) + if (!PE_(r_bin_pe_parse_imports)(bin, &imports, &nimp, dll_name, + curr_import_dir->Characteristics, curr_import_dir->FirstThunk)) break; - curr_import_dir++; } } @@ -732,8 +735,10 @@ struct r_bin_pe_import_t* PE_(r_bin_pe_get_imports)(struct PE_(r_bin_pe_obj_t) * curr_delay_import_dir = bin->delay_import_directory; if (curr_delay_import_dir->Attributes == 0) { - dll_name_offset = PE_(r_bin_pe_vaddr_to_paddr)(bin, curr_delay_import_dir->Name - PE_(r_bin_pe_get_image_base)(bin)); - import_func_name_offset = curr_delay_import_dir->DelayImportNameTable - PE_(r_bin_pe_get_image_base)(bin); + dll_name_offset = PE_(r_bin_pe_vaddr_to_paddr)(bin, + curr_delay_import_dir->Name - PE_(r_bin_pe_get_image_base)(bin)); + import_func_name_offset = curr_delay_import_dir->DelayImportNameTable - + PE_(r_bin_pe_get_image_base)(bin); } else { dll_name_offset = PE_(r_bin_pe_vaddr_to_paddr)(bin, curr_delay_import_dir->Name); import_func_name_offset = curr_delay_import_dir->DelayImportNameTable; @@ -745,8 +750,8 @@ struct r_bin_pe_import_t* PE_(r_bin_pe_get_imports)(struct PE_(r_bin_pe_obj_t) * return NULL; } if (!PE_(r_bin_pe_parse_imports)(bin, &imports, &nimp, dll_name, - import_func_name_offset, - curr_delay_import_dir->DelayImportAddressTable)) + import_func_name_offset, + curr_delay_import_dir->DelayImportAddressTable)) break; curr_delay_import_dir++; @@ -756,7 +761,7 @@ struct r_bin_pe_import_t* PE_(r_bin_pe_get_imports)(struct PE_(r_bin_pe_obj_t) * if (nimp) { imps = realloc (imports, (nimp+1) * sizeof(struct r_bin_pe_import_t)); if (!imps) { - perror ("realloc (import)"); + r_sys_perror ("realloc (import)"); return NULL; } imports = imps; @@ -785,7 +790,7 @@ struct r_bin_pe_lib_t* PE_(r_bin_pe_get_libs)(struct PE_(r_bin_pe_obj_t) *bin) { mallocsz = bin->size; libs = malloc (mallocsz); if (!libs) { - perror ("malloc (libs)"); + r_sys_perror ("malloc (libs)"); return NULL; } @@ -1011,7 +1016,7 @@ struct r_bin_pe_section_t* PE_(r_bin_pe_get_sections)(struct PE_(r_bin_pe_obj_t) shdr = bin->section_header; sections_count = bin->nt_headers->file_header.NumberOfSections; if ((sections = malloc ((sections_count + 1) * sizeof (struct r_bin_pe_section_t))) == NULL) { - perror ("malloc (sections)"); + r_sys_perror ("malloc (sections)"); return NULL; } for (i = 0; i < sections_count; i++) { diff --git a/libr/bin/p/bin_xbe.c b/libr/bin/p/bin_xbe.c index 76383619b2..2c22724950 100644 --- a/libr/bin/p/bin_xbe.c +++ b/libr/bin/p/bin_xbe.c @@ -15,14 +15,12 @@ static Sdb* get_sdb (RBinObject *o) { return NULL; } -static int check_bytes(const ut8 *buf, ut64 size) -{ +static int check_bytes(const ut8 *buf, ut64 size) { xbe_header *header = (xbe_header *)buf; return (size > sizeof(xbe_header) && header->magic == XBE_MAGIC); } -static int check(RBinFile *arch) -{ +static int check(RBinFile *arch) { const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; const ut64 size = arch ? r_buf_size (arch->buf) : 0; @@ -32,44 +30,38 @@ static int check(RBinFile *arch) return check_bytes(bytes, size); } -static int load(RBinFile *arch) -{ +static int load(RBinFile *arch) { r_bin_xbe_obj_t *obj = NULL; const ut8 *bytes = arch ? r_buf_buffer (arch->buf) : NULL; - if (!arch || !arch->o) return R_FALSE; - - arch->o->bin_obj = malloc(sizeof(r_bin_plugin_xbe)); + arch->o->bin_obj = malloc (sizeof (r_bin_plugin_xbe)); + if (!arch->o->bin_obj) + return R_FALSE; obj = arch->o->bin_obj; if (obj) { obj->header = (xbe_header *)bytes; - - // Sega Chihiro xbe if ((obj->header->ep&0xf0000000) == 0x40000000) { + // Sega Chihiro xbe obj->ep_key = XBE_EP_CHIHIRO; obj->kt_key = XBE_KP_CHIHIRO; - } - // Debug xbe - else if ((obj->header->ep ^ XBE_EP_RETAIL) > 0x1000000) { + } else if ((obj->header->ep ^ XBE_EP_RETAIL) > 0x1000000) { + // Debug xbe obj->ep_key = XBE_EP_DEBUG; obj->kt_key = XBE_KP_DEBUG; - } - // Retail xbe - else { + } else { + // Retail xbe obj->ep_key = XBE_EP_RETAIL; obj->kt_key = XBE_KP_RETAIL; } - return R_TRUE; } return R_FALSE; } -static int destroy(RBinFile *arch) -{ +static int destroy(RBinFile *arch) { free(arch->o->bin_obj); r_buf_free (arch->buf); arch->buf = NULL; @@ -92,23 +84,20 @@ static RBinAddr* binsym(RBinFile *arch, int type) { static RList* entries(RBinFile *arch) { r_bin_xbe_obj_t *obj = arch->o->bin_obj; - RList *ret = r_list_new(); - RBinAddr *ptr = R_NEW0(RBinAddr); - + RList *ret = r_list_new (); + RBinAddr *ptr = R_NEW0 (RBinAddr); + // XXX possible memleak if 1 of 2 alloc fails if (!arch || !arch->buf || !ret || !ptr) return NULL; - ret->free = free; - ptr->vaddr = obj->header->ep ^ obj->ep_key; ptr->paddr = ptr->vaddr - obj->header->base; - r_list_append (ret, ptr); - return ret; } static RList* sections(RBinFile *arch) { + xbe_section *sect; r_bin_xbe_obj_t *obj; RList *ret; int i; @@ -116,8 +105,10 @@ static RList* sections(RBinFile *arch) { if (!arch || !arch->o) return NULL; obj = arch->o->bin_obj; + if (obj->header->sections<1) + return NULL; - ret = r_list_new(); + ret = r_list_new (); if (!ret ) return NULL; @@ -128,9 +119,10 @@ static RList* sections(RBinFile *arch) { ret->free = free; - xbe_section sect[obj->header->sections]; + sect = calloc (obj->header->sections, sizeof (xbe_section)); - r_buf_read_at (arch->buf, obj->header->sechdr_addr - obj->header->base, (ut8 *)sect, sizeof(sect)); + r_buf_read_at (arch->buf, obj->header->sechdr_addr - obj->header->base, + (ut8 *)sect, sizeof (xbe_section)*obj->header->sections); for (i = 0; i < obj->header->sections; i++) { RBinSection *item = R_NEW0(RBinSection); @@ -149,26 +141,26 @@ static RList* sections(RBinFile *arch) { item->srwx |= 1; if (sect[i].flags & SECT_FLAG_W) item->srwx |= 2; - r_list_append (ret, item); } + free (sect); return ret; } static RList* libs(RBinFile *arch) { r_bin_xbe_obj_t *obj; - RList *ret = r_list_new(); xbe_lib lib; + RList *ret; char *s; int i; - if (!arch || !ret || !arch->o) + if (!arch || !arch->o) return NULL; obj = arch->o->bin_obj; - + ret = r_list_new (); + if (!ret) return NULL; ret->free = free; - r_buf_read_at (arch->buf, obj->header->kernel_lib_addr - obj->header->base, (ut8 *)&lib, sizeof(xbe_lib)); s = r_str_newf ("%s %i.%i.%i", lib.name, lib.major, lib.minor, lib.build); @@ -206,26 +198,28 @@ static RList* symbols(RBinFile *arch) { // PA -> VA translation for (i = 0; found == R_FALSE && i < obj->header->sections; i++) { - r_buf_read_at (arch->buf, obj->header->sechdr_addr - obj->header->base + (sizeof(xbe_section) * i), (ut8 *)§, sizeof(sect)); + r_buf_read_at (arch->buf, obj->header->sechdr_addr - \ + obj->header->base + (sizeof (xbe_section) * i), \ + (ut8 *)§, sizeof(sect)); if (kt_addr >= sect.vaddr && kt_addr < sect.vaddr + sect.vsize) found = R_TRUE; } if (found == R_FALSE) { - free(ret); + free (ret); return NULL; } - r_buf_read_at (arch->buf, sect.offset + (kt_addr - sect.vaddr), (ut8 *)&thunk_addr, sizeof(thunk_addr)); - + r_buf_read_at (arch->buf, sect.offset + (kt_addr - sect.vaddr), \ + (ut8 *)&thunk_addr, sizeof (thunk_addr)); for (i = 0; thunk_addr[i]; i++) { - RBinSymbol *sym = R_NEW0(RBinSymbol); + RBinSymbol *sym = R_NEW0 (RBinSymbol); if (!sym) { ret->free(sym); return NULL; } - ut32 thunk_index = thunk_addr[i] ^ 0x80000000; + const ut32 thunk_index = thunk_addr[i] ^ 0x80000000; // Basic sanity checks if (thunk_addr[i]&0x80000000 && thunk_index <= XBE_MAX_THUNK) { @@ -234,16 +228,13 @@ static RList* symbols(RBinFile *arch) { sym->paddr = sym->vaddr - obj->header->base; sym->size = 4; sym->ordinal = i; - - r_list_append(ret, sym); + r_list_append (ret, sym); } else free (sym); } - return ret; } -static RBinInfo* info(RBinFile *arch) -{ +static RBinInfo* info(RBinFile *arch) { r_bin_xbe_obj_t *obj = arch->o->bin_obj; RBinInfo *ret = R_NEW0 (RBinInfo); ut8 dbg_name[256]; @@ -256,8 +247,8 @@ static RBinInfo* info(RBinFile *arch) return NULL; } - r_buf_read_at (arch->buf, obj->header->debug_name_addr - obj->header->base, dbg_name, sizeof(dbg_name)); - + r_buf_read_at (arch->buf, obj->header->debug_name_addr - \ + obj->header->base, dbg_name, sizeof(dbg_name)); strncpy (ret->file, (const char*)dbg_name, R_BIN_SIZEOF_STRINGS); strncpy (ret->bclass, "program", R_BIN_SIZEOF_STRINGS); strncpy (ret->machine, "Microsoft Xbox", R_BIN_SIZEOF_STRINGS); diff --git a/libr/debug/debug.c b/libr/debug/debug.c index 2318811a28..1b94c9951b 100644 --- a/libr/debug/debug.c +++ b/libr/debug/debug.c @@ -7,7 +7,7 @@ R_LIB_VERSION(r_debug); R_API RDebugInfo *r_debug_info(RDebug *dbg, const char *arg) { - if (dbg && dbg->h && dbg->h->info) + if (!dbg || dbg->h || dbg->h->info) return NULL; return dbg->h->info (dbg, arg); } diff --git a/libr/diff/bdiff.c b/libr/diff/bdiff.c index 05996f89b0..c25e05be5b 100644 --- a/libr/diff/bdiff.c +++ b/libr/diff/bdiff.c @@ -35,8 +35,7 @@ struct hunklist { struct hunk *base, *head; }; -static int splitlines(const char *a, int len, struct line **lr) -{ +static int splitlines(const char *a, int len, struct line **lr) { int h, i; const char *p, *b = a; const char * const plast = a + len - 1; @@ -80,13 +79,11 @@ static int splitlines(const char *a, int len, struct line **lr) return i - 1; } -static int inline cmp(struct line *a, struct line *b) -{ +static int inline cmp(struct line *a, struct line *b) { return a->h != b->h || a->len != b->len || memcmp(a->l, b->l, a->len); } -static int equatelines(struct line *a, int an, struct line *b, int bn) -{ +static int equatelines(struct line *a, int an, struct line *b, int bn) { int i, j, buckets = 1, t, scale; struct pos *h = NULL; @@ -281,7 +278,9 @@ R_API int r_diff_buffers_delta(RDiff *d, const ut8 *sa, int la, const ut8 *sb, i int hits = -1; an = splitlines ((const char *)sa, la, &al); - bn = splitlines ((const char*)sb, lb, &bl); + if (an<0) return -1; + bn = splitlines ((const char *)sb, lb, &bl); + if (bn<0) return -1; if (!al || !bl) { eprintf ("bindiff_buffers: Out of memory.\n"); goto beach; @@ -337,4 +336,3 @@ R_API int r_diff_buffers_delta(RDiff *d, const ut8 *sa, int la, const ut8 *sb, i return hits; } - diff --git a/libr/io/io.c b/libr/io/io.c index b59e5c03ec..bd6d972de5 100644 --- a/libr/io/io.c +++ b/libr/io/io.c @@ -284,7 +284,7 @@ R_API int r_io_read_at(RIO *io, ut64 addr, ut8 *buf, int len) { return r_io_vread (io, addr, buf, len); #else ut64 paddr, last, last2; - int ms, ret, l, olen = len, w = 0; + int ms, ret, l = 0, olen = len, w = 0; io->off = addr; memset (buf, 0xff, len); // probably unnecessary