Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring merge sort and insertion sort cmp function semantics together #17473

Merged
merged 6 commits into from Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions libr/anal/reflines.c
Expand Up @@ -16,11 +16,11 @@ typedef struct refline_end {
} ReflineEnd;

static int cmp_asc(const struct refline_end *a, const struct refline_end *b) {
return a->val > b->val;
return (a->val > b->val) - (a->val < b->val);
}

static int cmp_by_ref_lvl(const RAnalRefline *a, const RAnalRefline *b) {
return a->level < b->level;
return (a->level < b->level) - (a->level > b->level);
}

static ReflineEnd *refline_end_new(ut64 val, bool is_from, RAnalRefline *ref) {
Expand Down
2 changes: 1 addition & 1 deletion libr/cons/grep.c
Expand Up @@ -441,7 +441,7 @@ static int cmp(const void *a, const void *b) {
if (IS_DIGIT (*ca) && IS_DIGIT (*cb)) {
ut64 na = r_num_get (NULL, ca);
ut64 nb = r_num_get (NULL, cb);
int ret = na > nb;
int ret = (na > nb) - (na < nb);
free (da);
free (db);
return ret;
Expand Down
6 changes: 3 additions & 3 deletions libr/core/agraph.c
Expand Up @@ -995,7 +995,7 @@ static RList **compute_classes(const RAGraph *g, Sdb *v_nodes, int is_left, int
}

static int cmp_dist(const size_t a, const size_t b) {
return (int) a < (int) b;
return (a < b) - (a > b);
}

static RGraphNode *get_sibling(const RAGraph *g, const RANode *n, int is_left, int is_adjust_class) {
Expand Down Expand Up @@ -1332,11 +1332,11 @@ static void place_single(const RAGraph *g, int l, const RGraphNode *bm, const RG
}

static int RM_listcmp(const struct len_pos_t *a, const struct len_pos_t *b) {
return a->pos < b->pos;
return (a->pos < b->pos) - (a->pos > b->pos);
}

static int RP_listcmp(const struct len_pos_t *a, const struct len_pos_t *b) {
return a->pos >= b->pos;
return (a->pos > b->pos) - (a->pos < b->pos);
}

static void collect_changes(const RAGraph *g, int l, const RGraphNode *b, int from_up, int s, int e, RList *list, int is_left) {
Expand Down
26 changes: 20 additions & 6 deletions libr/core/cconfig.c
Expand Up @@ -51,28 +51,42 @@ static void print_node_options(RConfigNode *node) {
}

static int compareName(const RAnalFunction *a, const RAnalFunction *b) {
return a && b && a->name && b->name && strcmp (a->name, b->name);
return (a && b && a->name && b->name ? strcmp (a->name, b->name) : 0);
}

static int compareNameLen(const RAnalFunction *a, const RAnalFunction *b) {
return a && b && a->name && b->name && strlen (a->name) > strlen (b->name);
size_t la, lb;
if (!a || !b || !a->name || !b->name) {
return 0;
}
la = strlen (a->name);
lb = strlen (a->name);
return (la > lb) - (la < lb);
}

static int compareAddress(const RAnalFunction *a, const RAnalFunction *b) {
return a && b && a->addr && b->addr && a->addr > b->addr;
return (a && b && a->addr && b->addr ? (a->addr > b->addr) - (a->addr < b->addr) : 0);
}

static int compareType(const RAnalFunction *a, const RAnalFunction *b) {
return a && b && a->diff->type && b->diff->type && a->diff->type > b->diff->type;
return (a && b && a->diff->type && b->diff->type ?
(a->diff->type > b->diff->type) - (a->diff->type < b->diff->type) : 0);
}

static int compareSize(const RAnalFunction *a, const RAnalFunction *b) {
ut64 sa, sb;
// return a && b && a->_size < b->_size;
return a && b && r_anal_function_realsize (a) > r_anal_function_realsize (b);
if (!a || !b) {
return 0;
}
sa = r_anal_function_realsize (a);
sb = r_anal_function_realsize (b);
return (sa > sb) - (sa < sb);
}

static int compareDist(const RAnalFunction *a, const RAnalFunction *b) {
return a && b && a->diff->dist && b->diff->dist && a->diff->dist > b->diff->dist;
return (a && b && a->diff->dist && b->diff->dist ?
(a->diff->dist > b->diff->dist) - (a->diff->dist < b->diff->dist) : 0);
}

static bool cb_diff_sort(void *_core, void *_node) {
Expand Down
1 change: 1 addition & 0 deletions libr/reg/double.c
Expand Up @@ -77,6 +77,7 @@ R_API long double r_reg_get_longdouble(RReg *reg, RRegItem *item) {
case 80:
case 96:
case 128:
case 256:
if (regset->arena->size - off - 1 >= 0) {
memcpy (&vld, regset->arena->bytes + off, sizeof (long double));
ret = vld;
Expand Down
2 changes: 1 addition & 1 deletion libr/reg/reg.c
Expand Up @@ -200,7 +200,7 @@ R_API void r_reg_free_internal(RReg *reg, bool init) {
static int regcmp(RRegItem *a, RRegItem *b) {
int offa = (a->offset * 16) + a->size;
int offb = (b->offset * 16) + b->size;
return offa > offb;
return (offa > offb) - (offa < offb);
ret2libc marked this conversation as resolved.
Show resolved Hide resolved
}

R_API void r_reg_reindex(RReg *reg) {
Expand Down
6 changes: 4 additions & 2 deletions libr/reg/rvalue.c
Expand Up @@ -125,7 +125,8 @@ R_API ut64 r_reg_get_value(RReg *reg, RRegItem *item) {
// FIXME: It is a precision loss, please implement me properly!
return (ut64)r_reg_get_longdouble (reg, item);
case 128:
// XXX 128 bit
case 256:
// XXX 128 & 256 bit
return (ut64)r_reg_get_longdouble (reg, item);
default:
eprintf ("r_reg_get_value: Bit size %d not supported\n", item->size);
Expand Down Expand Up @@ -202,7 +203,8 @@ R_API bool r_reg_set_value(RReg *reg, RRegItem *item, ut64 value) {
}
return true;
case 128:
// XXX 128 bit
case 256:
// XXX 128 & 256 bit
return false; // (ut64)r_reg_get_longdouble (reg, item);
default:
eprintf ("r_reg_set_value: Bit size %d not supported\n", item->size);
Expand Down
2 changes: 1 addition & 1 deletion libr/util/list.c
Expand Up @@ -479,7 +479,7 @@ static RListIter *_merge(RListIter *first, RListIter *second, RListComparator cm
} else if (!first) {
next = second;
second = second->n;
} else if (cmp (first->data, second->data) < 0) {
} else if (cmp (first->data, second->data) <= 0) {
next = first;
first = first->n;
} else {
Expand Down
2 changes: 1 addition & 1 deletion libr/util/range.c
Expand Up @@ -248,7 +248,7 @@ R_API int r_range_contains(RRange *rgs, ut64 addr) {
static int cmp_ranges(void *a, void *b) {
RRangeItem *first = (RRangeItem *)a;
RRangeItem *second = (RRangeItem *)b;
return first->fr > second->fr;
return (first->fr > second->fr) - (first->fr < second->fr);
}

R_API int r_range_sort(RRange *rgs) {
Expand Down
5 changes: 3 additions & 2 deletions shlr/sdb/meson.build
Expand Up @@ -64,7 +64,7 @@ libsdb_sources = [

sdb_inc = include_directories(['.', 'src'])

libsdb = both_libraries('libsdb', libsdb_sources,
libsdb = both_libraries('sdb', libsdb_sources,
include_directories: sdb_inc,
implicit_include_directories: false,
soversion: sdb_libversion,
Expand All @@ -90,6 +90,7 @@ else
'src/ls.h',
'src/sdb.h',
'src/sdbht.h',
'src/set.h',
'src/types.h'
]
install_headers(include_files, subdir: 'sdb')
Expand Down Expand Up @@ -117,7 +118,7 @@ pkgconfig_mod.generate(
filebase: 'sdb',
libraries: [libsdb.get_shared_lib()],
description: 'Simple DataBase',
subdirs: ['sdb', '.'],
subdirs: ['sdb'],
version: sdb_version,
url: 'https://github.com/radareorg/sdb'
)
Expand Down
2 changes: 1 addition & 1 deletion shlr/sdb/src/ls.c
Expand Up @@ -45,7 +45,7 @@ static SdbListIter *_merge(SdbListIter *first, SdbListIter *second, SdbListCompa
} else if (!first) {
next = second;
second = second->n;
} else if (cmp (first->data, second->data) < 0) {
} else if (cmp (first->data, second->data) <= 0) {
anisse marked this conversation as resolved.
Show resolved Hide resolved
next = first;
first = first->n;
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/db/anal/vars
Expand Up @@ -151,8 +151,8 @@ afv
EOF
EXPECT=<<EOF
arg int64_t arg3 @ r8
arg int64_t arg2 @ rdx
ret2libc marked this conversation as resolved.
Show resolved Hide resolved
arg const char * s @ rcx
arg int64_t arg2 @ rdx
EOF
RUN

Expand Down
2 changes: 1 addition & 1 deletion test/db/anal/x86_64
Expand Up @@ -1570,8 +1570,8 @@ EXPECT=<<EOF
var int64_t var_14h @ rbp-0x14
var int64_t var_10h @ rbp-0x10
var int64_t var_8h @ rbp-0x8
arg int64_t arg1 @ rdi
arg int64_t arg2 @ rsi
arg int64_t arg1 @ rdi
EOF
RUN

Expand Down