Skip to content

Commit

Permalink
Fix ~$$? ~?$$ and add tests ##cons
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae authored Dec 19, 2024
1 parent 74636e3 commit 8f9bf35
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
29 changes: 21 additions & 8 deletions libr/cons/grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ R_API void r_cons_grep_expression(const char *str) {
}
if (len > 0 && str[len] == '?') {
grep->counter = 1;
r_str_ncpy (buf, str, R_MIN (len, sizeof (buf) - 1));
r_str_ncpy (buf, str, sizeof (buf) - 1);
buf[len] = 0;
len--;
} else {
Expand Down Expand Up @@ -1011,9 +1011,12 @@ R_API void r_cons_grepbuf(void) {
}
}

const int ob_len = r_strbuf_length (ob);
int ob_len = r_strbuf_length (ob);
cons->context->buffer_len = ob_len;
if (grep->counter) {

// count before uniq
// XXX dupe from the code below
if (grep->counter && !grep->sort_uniq) {
int cnt = grep->charCounter? strlen (cons->context->buffer): cons->lines;
free (cons->context->buffer);
char *cntstr = r_str_newf ("%d\n", cnt);
Expand All @@ -1025,16 +1028,15 @@ R_API void r_cons_grepbuf(void) {
r_strbuf_free (ob);
return;
}

if (ob_len >= cons->context->buffer_sz) {
cons->context->buffer_sz = ob_len + 1;
cons->context->buffer = r_strbuf_drain (ob);
} else {
memcpy (cons->context->buffer, r_strbuf_getbin (ob, NULL), ob_len);
cons->context->buffer[ob_len] = 0;
r_strbuf_free (ob);
ob = NULL;
}

if (grep->sort != -1 || grep->sort_invert) {
#define INSERT_LINES(list) \
if (list) { \
Expand All @@ -1046,11 +1048,9 @@ R_API void r_cons_grepbuf(void) {
memcpy (ptr + slen, "\n", 2); \
ptr += slen + 1; \
} \
nl++; \
} \
}
RListIter *iter;
int nl = 0;
char *ptr = cons->context->buffer;
char *str;
RConsContext *ctx = cons->context;
Expand All @@ -1067,9 +1067,9 @@ R_API void r_cons_grepbuf(void) {
r_list_uniq_inplace (ctx->sorted_lines, cmpstrings);
r_list_free (ctx->unsorted_lines);
ctx->unsorted_lines = NULL;
nl = 0;
}
}
const int nl = r_list_length (ctx->sorted_lines);
cons->context->buffer_len = 0;
INSERT_LINES (ctx->unsorted_lines);
INSERT_LINES (ctx->sorted_lines);
Expand All @@ -1080,6 +1080,19 @@ R_API void r_cons_grepbuf(void) {
r_list_free (ctx->unsorted_lines);
ctx->unsorted_lines = NULL;
}
// count after uniq
if (grep->counter && grep->sort_uniq) {
int cnt = grep->charCounter? strlen (cons->context->buffer): cons->lines;
free (cons->context->buffer);
char *cntstr = r_str_newf ("%d\n", cnt);
size_t cntstr_len = cntstr? strlen (cntstr): 0;
cons->context->buffer = cntstr;
cons->context->buffer_len = cntstr_len;
cons->context->buffer_sz = cntstr_len + 1;
cons->num->value = cons->lines;
r_strbuf_free (ob);
return;
}
}

R_API int r_cons_grep_line(char *buf, int len) {
Expand Down
10 changes: 5 additions & 5 deletions test/db/cmd/cmd_zignature
Original file line number Diff line number Diff line change
Expand Up @@ -1624,15 +1624,15 @@ z/
afl~main?
zi~sign.bytes_func_collision.badname_1?
zi~sign.graph_collision.badname_1?
zi~sign.bytes_func_collision.badname2_1?
zi~sign.graph_collision.badname2_1?
zi~sign.bytes_func_collision.badname_1?
zi~sign.graph_func_collision.badname2_1?
EOF
EXPECT=<<EOF
1
1
0
1
1
1
0
EOF
RUN

Expand Down Expand Up @@ -1672,7 +1672,7 @@ f test_orig_size = `z~?`
zaM
?v test_orig_size - `z~?`
zaF
?v test_orig_size - `z~?`~0xf?
?v test_orig_size - `z~?`~0x0?
EOF
EXPECT=<<EOF
0x0
Expand Down
16 changes: 16 additions & 0 deletions test/db/cmd/cons_grep
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,19 @@ EXPECT=<<EOF
----
EOF
RUN

NAME=count+uniq
FILE=bins/mach0/mac-ls4
ARGS=-a arm -b 64
CMDS=<<EOF
af
pdsfq~?
pdsfq~$$?
pdsfq~?$$
EOF
EXPECT=<<EOF
145
94
94
EOF
RUN
2 changes: 1 addition & 1 deletion test/db/tools/rasm2
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ CMDS=<<EOF
!!rasm2 -L~arm.gnu?
EOF
EXPECT=<<EOF
29
4
1
EOF
RUN
Expand Down

0 comments on commit 8f9bf35

Please sign in to comment.