Skip to content

Commit

Permalink
RAnalCond: make explicit both left and right vals ##refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
astralia committed Nov 25, 2024
1 parent e7f8893 commit 7812dd7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
28 changes: 14 additions & 14 deletions libr/anal/cond.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ R_API RAnalCond *r_anal_cond_new(void) {

R_API void r_anal_cond_fini(RAnalCond *c) {
R_RETURN_IF_FAIL (c);
r_anal_value_free (c->arg[0]);
r_anal_value_free (c->arg[1]);
c->arg[0] = c->arg[1] = NULL;
r_anal_value_free (c->left);
r_anal_value_free (c->right);
c->left = c->right = NULL;
}

R_API void r_anal_cond_free(R_NULLABLE RAnalCond *c) {
Expand All @@ -66,8 +66,8 @@ R_API RAnalCond *r_anal_cond_clone(RAnalCond *cond) {
RAnalCond *c = R_NEW (RAnalCond);
if (R_LIKELY (c)) {
c->type = cond->type;
c->arg[0] = r_anal_value_clone (cond->arg[0]);
c->arg[1] = r_anal_value_clone (cond->arg[1]);
c->left = r_anal_value_clone (cond->left);
c->right = r_anal_value_clone (cond->right);
return c;
}
return NULL;
Expand All @@ -76,9 +76,9 @@ R_API RAnalCond *r_anal_cond_clone(RAnalCond *cond) {
R_API int r_anal_cond_eval(RAnal *anal, RAnalCond *cond) {
R_RETURN_VAL_IF_FAIL (anal && cond, false);
// XXX: sign issue here?
st64 arg0 = (st64) r_anal_value_to_ut64 (anal, cond->arg[0]);
if (cond->arg[1]) {
st64 arg1 = (st64) r_anal_value_to_ut64 (anal, cond->arg[1]);
st64 arg0 = (st64) r_anal_value_to_ut64 (anal, cond->left);
if (cond->right) {
st64 arg1 = (st64) r_anal_value_to_ut64 (anal, cond->right);
switch (cond->type) {
case R_ANAL_CONDTYPE_EQ: return arg0 == arg1;
case R_ANAL_CONDTYPE_NE: return arg0 != arg1;
Expand All @@ -103,13 +103,13 @@ R_API int r_anal_cond_eval(RAnal *anal, RAnalCond *cond) {
R_API char *r_anal_cond_tostring(RAnalCond *cond) {
R_RETURN_VAL_IF_FAIL (cond, NULL);
const char *cnd = r_anal_cond_typeexpr_tostring (cond->type);
char *val0 = r_anal_value_tostring (cond->arg[0]);
char *val0 = r_anal_value_tostring (cond->left);
char *out = NULL;
if (val0) {
if (R_ANAL_CONDTYPE_SINGLE (cond)) {
out = r_str_newf ("%s%s", cnd, val0);
} else {
char *val1 = r_anal_value_tostring (cond->arg[1]);
char *val1 = r_anal_value_tostring (cond->right);
if (val1) {
out = r_str_newf ("%s %s %s", val0, cnd, val1);
free (val1);
Expand All @@ -133,8 +133,8 @@ R_API RAnalCond *r_anal_cond_new_from_op(RAnalOp *op) {
return NULL;
}
// TODO: use r_ref
cond->arg[0] = r_anal_value_clone (src0);
cond->arg[1] = r_anal_value_clone (src1);
cond->left = r_anal_value_clone (src0);
cond->right = r_anal_value_clone (src1);
return cond;
}

Expand All @@ -156,8 +156,8 @@ R_API RAnalCond *r_anal_cond_new_from_string(const char *str) {
cond->type = r_anal_cond_type_fromstring (condtypestr_expr[i]);
char *left = strndup (substr, substr - str);
char *right = strdup (substr + strlen (condtypestr_expr[i]));
cond->arg[0] = r_anal_value_new_from_string (left);
cond->arg[1] = r_anal_value_new_from_string (right);
cond->left = r_anal_value_new_from_string (left);
cond->right = r_anal_value_new_from_string (right);
free (left);
free (right);
return cond;
Expand Down
5 changes: 3 additions & 2 deletions libr/include/r_anal.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,12 @@ typedef struct r_anal_bind_t {
RAnalUse use;
} RAnalBind;

#define R_ANAL_CONDTYPE_SINGLE(x) (!x->arg[1] || x->arg[0]==x->arg[1])
#define R_ANAL_CONDTYPE_SINGLE(x) (!x->right || x->left==x->right)

typedef struct r_anal_cond_t {
int type; // filled by CJMP opcode
RArchValue *arg[2]; // filled by CMP opcode
RArchValue *left; // filled by CMP left opcode
RArchValue *right; // filled by CMP right opcode
} RAnalCond;

typedef struct r_anal_bb_t {
Expand Down

0 comments on commit 7812dd7

Please sign in to comment.