Skip to content

Commit

Permalink
Fix #11553 - Remove the R_NOTNULL bad practice ##refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpolverari authored and radare committed Oct 18, 2018
1 parent 9beda2f commit f57a12d
Show file tree
Hide file tree
Showing 41 changed files with 169 additions and 184 deletions.
5 changes: 2 additions & 3 deletions libr/bin/obj.c
Expand Up @@ -57,9 +57,8 @@ R_API RBinObject *r_bin_object_new(RBinFile *binfile, RBinPlugin *plugin, ut64 b
if (sz < bsz) {
bsz = sz;
}
o->bin_obj = plugin->load_bytes (binfile, bytes + offset, sz,
loadaddr, sdb);
if (!o->bin_obj) {
if (!plugin->load_bytes (binfile, &o->bin_obj, bytes + offset, sz,
loadaddr, sdb)) {
bprintf (
"Error in r_bin_object_new: load_bytes failed "
"for %s plugin\n",
Expand Down
9 changes: 5 additions & 4 deletions libr/bin/p/bin_art.c
Expand Up @@ -63,19 +63,20 @@ static Sdb *get_sdb(RBinFile *bf) {
return ao? ao->kv: NULL;
}

static void *load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 la, Sdb *sdb){
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 la, Sdb *sdb){
ArtObj *ao = R_NEW0 (ArtObj);
if (!ao) {
return NULL;
return false;
}
ao->kv = sdb_new0 ();
if (!ao->kv) {
free (ao);
return NULL;
return false;
}
art_header_load (&ao->art, bf->buf, ao->kv);
sdb_ns_set (sdb, "info", ao->kv);
return ao;
*bin_obj = ao;
return true;
}

static bool load(RBinFile *bf) {
Expand Down
5 changes: 2 additions & 3 deletions libr/bin/p/bin_avr.c
Expand Up @@ -68,9 +68,8 @@ static bool check_bytes(const ut8 *b, ut64 length) {
return check_bytes_rjmp (b, length);
}

static void * load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
check_bytes (buf, sz);
return R_NOTNULL;
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
return check_bytes (buf, sz);
}

static RBinInfo* info(RBinFile *bf) {
Expand Down
4 changes: 2 additions & 2 deletions libr/bin/p/bin_bf.c
Expand Up @@ -5,8 +5,8 @@
#include <r_lib.h>
#include <r_bin.h>

static void *load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
return R_NOTNULL;
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
return true;
}

static bool load(RBinFile *bf) {
Expand Down
12 changes: 6 additions & 6 deletions libr/bin/p/bin_bflt.c
Expand Up @@ -7,25 +7,25 @@
#include <r_io.h>
#include "bflt/bflt.h"

static void *load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loaddr, Sdb *sdb) {
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loaddr, Sdb *sdb) {
if (!buf || !sz || sz == UT64_MAX) {
return NULL;
return false;
}
RBuffer *tbuf = r_buf_new ();
if (!tbuf) {
return NULL;
return false;
}
r_buf_set_bytes (tbuf, buf, sz);
struct r_bin_bflt_obj *res = r_bin_bflt_new_buf (tbuf);
r_buf_free (tbuf);
return res? res: NULL;
*bin_obj = res;
return true;
}

static bool load(RBinFile *bf) {
const ut8 *bytes = r_buf_buffer (bf->buf);
ut64 sz = r_buf_size (bf->buf);
bf->o->bin_obj = load_bytes (bf, bytes, sz, bf->o->loadaddr, bf->sdb);
return bf->o->bin_obj? true: false;
return load_bytes (bf, &bf->o->bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
}

static RList *entries(RBinFile *bf) {
Expand Down
7 changes: 2 additions & 5 deletions libr/bin/p/bin_bios.c
Expand Up @@ -21,11 +21,8 @@ static bool check_bytes(const ut8 *buf, ut64 length) {
return false;
}

static void *load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
if (!check_bytes (buf, sz)) {
return NULL;
}
return R_NOTNULL;
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
return check_bytes (buf, sz);
}

static bool load(RBinFile *bf) {
Expand Down
11 changes: 6 additions & 5 deletions libr/bin/p/bin_bootimg.c
Expand Up @@ -82,22 +82,23 @@ static Sdb *get_sdb(RBinFile *bf) {
return ao? ao->kv: NULL;
}

static void *load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 la, Sdb *sdb) {
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 la, Sdb *sdb) {
BootImageObj *bio = R_NEW0 (BootImageObj);
if (!bio) {
return NULL;
return false;
}
bio->kv = sdb_new0 ();
if (!bio->kv) {
free (bio);
return NULL;
return false;
}
if (!bootimg_header_load (&bio->bi, bf->buf, bio->kv)) {
free(bio);
return NULL;
return false;
}
sdb_ns_set (sdb, "info", bio->kv);
return bio;
*bin_obj = bio;
return true;
}

static bool load(RBinFile *bf) {
Expand Down
11 changes: 5 additions & 6 deletions libr/bin/p/bin_coff.c
Expand Up @@ -19,15 +19,15 @@ static Sdb* get_sdb(RBinFile *bf) {
return NULL;
}

static void * load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
if (!buf || !sz || sz == UT64_MAX) {
return NULL;
return false;
}
RBuffer *tbuf = r_buf_new();
r_buf_set_bytes (tbuf, buf, sz);
void *res = r_bin_coff_new_buf (tbuf, bf->rbin->verbose);
*bin_obj = r_bin_coff_new_buf (tbuf, bf->rbin->verbose);
r_buf_free (tbuf);
return res;
return true;
}

static bool load(RBinFile *bf) {
Expand All @@ -37,8 +37,7 @@ static bool load(RBinFile *bf) {
if (!bf || !bf->o) {
return false;
}
bf->o->bin_obj = load_bytes (bf, bytes, sz, bf->o->loadaddr, bf->sdb);
return bf->o->bin_obj ? true: false;
return load_bytes (bf, &bf->o->bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
}

static int destroy(RBinFile *bf) {
Expand Down
14 changes: 6 additions & 8 deletions libr/bin/p/bin_dex.c
Expand Up @@ -710,20 +710,19 @@ static Sdb *get_sdb (RBinFile *bf) {
return bin? bin->kv: NULL;
}

static void *load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
void *res = NULL;
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
RBuffer *tbuf = NULL;
if (!buf || !sz || sz == UT64_MAX) {
return NULL;
return false;
}
tbuf = r_buf_new ();
if (!tbuf) {
return NULL;
return false;
}
r_buf_set_bytes (tbuf, buf, sz);
res = r_bin_dex_new_buf (tbuf);
*bin_obj = r_bin_dex_new_buf (tbuf);
r_buf_free (tbuf);
return res;
return true;
}

static void * load_buffer(RBinFile *bf, RBuffer *buf, ut64 loadaddr, Sdb *sdb) {
Expand All @@ -737,8 +736,7 @@ static bool load(RBinFile *bf) {
if (!bf || !bf->o) {
return false;
}
bf->o->bin_obj = load_bytes (bf, bytes, sz, bf->o->loadaddr, bf->sdb);
return bf->o->bin_obj ? true: false;
return load_bytes (bf, &bf->o->bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
}

static ut64 baddr(RBinFile *bf) {
Expand Down
16 changes: 8 additions & 8 deletions libr/bin/p/bin_dol.c
Expand Up @@ -45,21 +45,21 @@ static bool check_bytes(const ut8 *buf, ut64 length) {
return (!memcmp (buf, "\x00\x00\x01\x00\x00\x00", 6));
}

static void *load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
bool has_dol_extension = false;
DolHeader *dol;
char *lowername, *ext;
if (!bf || sz < sizeof (DolHeader)) {
return NULL;
return false;
}
dol = R_NEW0 (DolHeader);
if (!dol) {
return NULL;
return false;
}
lowername = strdup (bf->file);
if (!lowername) {
free (dol);
return NULL;
return false;
}
r_str_case (lowername, 0);
ext = strstr (lowername, ".dol");
Expand All @@ -71,12 +71,12 @@ static void *load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sd
r_buf_fread_at (bf->buf, 0, (void *) dol, "67I", 1);
// r_buf_fread_at (bf->buf, 0, (void*)dol, "67i", 1);
if (bf && bf->o && bf->o->bin_obj) {
bf->o->bin_obj = dol;
*bin_obj = bf->o->bin_obj = dol;
}
return (void *) dol;
return true;
}
free (dol);
return NULL;
return false;
}

static bool load(RBinFile *bf) {
Expand All @@ -85,7 +85,7 @@ static bool load(RBinFile *bf) {
if (!bf || !bf->o) {
return false;
}
bf->o->bin_obj = load_bytes (bf, bytes,
load_bytes (bf, &bf->o->bin_obj, bytes,
sz, bf->o->loadaddr, bf->sdb);
return check_bytes (bytes, sz);
}
Expand Down
6 changes: 3 additions & 3 deletions libr/bin/p/bin_dyldcache.c
Expand Up @@ -872,15 +872,15 @@ static void *load_buffer(RBinFile *bf, RBuffer *buf, ut64 loadaddr, Sdb *sdb) {
return cache;
}

static void *load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
return (void *) (size_t) check_bytes (buf, sz);
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
return check_bytes (buf, sz);
}

static bool load(RBinFile *bf) {
const ut8 *bytes = bf ? r_buf_buffer (bf->buf) : NULL;
ut64 sz = bf ? r_buf_size (bf->buf): 0;
ut64 la = (bf && bf->o) ? bf->o->loadaddr: 0;
return load_bytes (bf, bytes, sz, la, bf? bf->sdb: NULL) != NULL;
return load_bytes (bf, &bf->o->bin_obj, bytes, sz, la, bf? bf->sdb: NULL);
}

static RList *entries(RBinFile *bf) {
Expand Down
10 changes: 5 additions & 5 deletions libr/bin/p/bin_elf.inc
Expand Up @@ -84,10 +84,10 @@ static void * load_buffer(RBinFile *bf, RBuffer *buf, ut64 loadaddr, Sdb *sdb) {
return res;
}

static void * load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
struct Elf_(r_bin_elf_obj_t) *res;
if (!buf || !sz || sz == UT64_MAX) {
return NULL;
return false;
}
RBuffer *tbuf = r_buf_new ();
// NOOOEES must use io!
Expand All @@ -97,7 +97,8 @@ static void * load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, S
sdb_ns_set (sdb, "info", res->kv);
}
r_buf_free (tbuf);
return res;
*bin_obj = res;
return true;
}

static bool load(RBinFile *bf) {
Expand All @@ -106,8 +107,7 @@ static bool load(RBinFile *bf) {
if (!bf || !bf->o) {
return false;
}
bf->o->bin_obj = load_bytes (bf, bytes, sz, bf->o->loadaddr, bf->sdb);
return bf->o->bin_obj != NULL;
return load_bytes (bf, &bf->o->bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);
}

static int destroy(RBinFile *bf) {
Expand Down
9 changes: 3 additions & 6 deletions libr/bin/p/bin_fs.c
Expand Up @@ -53,18 +53,15 @@ static bool check_bytes(const ut8 *buf, ut64 length) {
return p != NULL;
}

static void * load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
if (check_bytes (buf, sz)) {
return R_NOTNULL;
}
return NULL;
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
return check_bytes (buf, sz);
}

static bool load(RBinFile *bf) {
const ut8 *bytes = bf ? r_buf_buffer (bf->buf) : NULL;
ut64 sz = bf ? r_buf_size (bf->buf): 0;
ut64 la = (bf && bf->o) ? bf->o->loadaddr: 0;
return load_bytes (bf, bytes, sz, la, bf? bf->sdb: NULL) != NULL;
return load_bytes (bf, &bf->o->bin_obj, bytes, sz, la, bf? bf->sdb: NULL);
}

static int destroy(RBinFile *bf) {
Expand Down
18 changes: 9 additions & 9 deletions libr/bin/p/bin_java.c
Expand Up @@ -74,22 +74,22 @@ static Sdb *get_sdb(RBinFile *bf) {
return NULL;
}

static void *load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
struct r_bin_java_obj_t *bin_obj = NULL;
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
struct r_bin_java_obj_t *tmp_bin_obj = NULL;
RBuffer *tbuf = NULL;
void *res = NULL;
if (!buf || sz == 0 || sz == UT64_MAX) {
return NULL;
return false;
}
tbuf = r_buf_new ();
r_buf_set_bytes (tbuf, buf, sz);
res = bin_obj = r_bin_java_new_buf (tbuf, loadaddr, sdb);
add_bin_obj_to_sdb (bin_obj);
tmp_bin_obj = r_bin_java_new_buf (tbuf, loadaddr, sdb);
*bin_obj = tmp_bin_obj;
add_bin_obj_to_sdb (tmp_bin_obj);
if (bf && bf->file) {
bin_obj->file = strdup (bf->file);
tmp_bin_obj->file = strdup (bf->file);
}
r_buf_free (tbuf);
return res;
return true;
}

static bool load(RBinFile *bf) {
Expand All @@ -102,7 +102,7 @@ static bool load(RBinFile *bf) {
return false;
}

bin_obj = load_bytes (bf, bytes, sz, bf->o->loadaddr, bf->sdb);
load_bytes (bf, (void **) &bin_obj, bytes, sz, bf->o->loadaddr, bf->sdb);

if (bin_obj) {
if (!bf->o->kv) {
Expand Down

0 comments on commit f57a12d

Please sign in to comment.