Skip to content

Commit 9c3e5d8

Browse files
committed
Remove the grep token hardcoded limits ##cons
1 parent 6b3d53c commit 9c3e5d8

File tree

5 files changed

+60
-62
lines changed

5 files changed

+60
-62
lines changed

libr/cons/grep.c

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ R_API void r_cons_grep_expression(RCons *cons, const char *str) {
102102
}
103103
RConsContext *ctx = cons->context;
104104
RConsGrep *grep = &ctx->grep;
105+
// reset dynamic tokens from previous grep expression
106+
R_FREE (grep->tokens);
107+
grep->tokens_count = 0;
105108

106109
size_t str_len = strlen (str);
107110
size_t buf_len = str_len;
@@ -121,7 +124,14 @@ R_API void r_cons_grep_expression(RCons *cons, const char *str) {
121124
memcpy (buf, str, buf_len);
122125
buf[buf_len] = '\0';
123126

124-
char *ptrs[R_CONS_GREP_COUNT];
127+
// split expression into sub-expressions separated by '~'
128+
size_t ptrs_capacity = buf_len + 1;
129+
char **ptrs = malloc (ptrs_capacity * sizeof (char *));
130+
if (!ptrs) {
131+
R_LOG_ERROR ("r_cons_grep: cannot allocate ptrs");
132+
free (buf);
133+
return;
134+
}
125135
size_t ptrs_length = 1;
126136
ptrs[0] = buf;
127137
char *ptr = buf;
@@ -131,10 +141,7 @@ R_API void r_cons_grep_expression(RCons *cons, const char *str) {
131141
ptrs[ptrs_length]++;
132142
ptr = ptrs[ptrs_length];
133143
ptrs_length++;
134-
if (ptrs_length >= R_CONS_GREP_COUNT) {
135-
R_LOG_ERROR ("too many nested greps");
136-
goto cleanup;
137-
}
144+
// no static limit: ptrs_capacity is sufficient for buf_len+1 splits
138145
}
139146

140147
R_FREE (grep->str);
@@ -297,8 +304,9 @@ R_API void r_cons_grep_expression(RCons *cons, const char *str) {
297304
ptr2++;
298305
for (; ptr2 <= ptr3; ptr2++) {
299306
if (fail) {
300-
ZERO_FILL(grep->tokens);
301-
grep->tokens_used = 0;
307+
// reset dynamic tokens list on failure
308+
R_FREE (grep->tokens);
309+
grep->tokens_count = 0;
302310
break;
303311
}
304312
switch (*ptr2) {
@@ -309,13 +317,16 @@ R_API void r_cons_grep_expression(RCons *cons, const char *str) {
309317
break;
310318
case ']': // fallthrough to handle ']' like ','
311319
case ',':
320+
// add range of token indices dynamically
312321
for (; range_begin <= range_end; range_begin++) {
313-
if (range_begin >= R_CONS_GREP_TOKENS) {
314-
fail = true;
315-
break;
322+
int idx = (int)range_begin;
323+
int *tmp = realloc (grep->tokens, (grep->tokens_count + 1) * sizeof (int));
324+
if (!tmp) {
325+
R_LOG_ERROR ("r_cons_grep: cannot allocate tokens");
326+
goto cleanup;
316327
}
317-
grep->tokens[range_begin] = 1;
318-
grep->tokens_used = 1;
328+
grep->tokens = tmp;
329+
grep->tokens[grep->tokens_count++] = idx;
319330
}
320331
if (*ptr2 == ']' && is_range && !num_is_parsed) {
321332
num_is_parsed = true;
@@ -405,8 +416,9 @@ R_API void r_cons_grep_expression(RCons *cons, const char *str) {
405416
r_list_append (grep->strings, gw);
406417
}
407418
cleanup:
419+
free (ptrs);
408420
free (buf);
409-
}
421+
}
410422

411423
// Finds and returns next intgrep expression, unescapes escaped twiddles
412424
static char *find_next_intgrep(char *cmd, const char *quotes) {
@@ -1120,11 +1132,9 @@ R_API int r_cons_grep_line(char *buf, int len) {
11201132
RConsGrep *grep = &cons->context->grep;
11211133
const char *delims = " |,;=\t";
11221134
char *tok = NULL;
1123-
char *save_ptr = NULL;
11241135
bool hit = true;
11251136
int outlen = 0;
11261137
bool use_tok = false;
1127-
size_t i;
11281138

11291139
char *in = calloc (1, len + 1);
11301140
if (!in) {
@@ -1189,11 +1199,13 @@ R_API int r_cons_grep_line(char *buf, int len) {
11891199
} else {
11901200
use_tok = true;
11911201
}
1192-
if (use_tok && grep->tokens_used) {
1193-
for (i = 0; i < R_CONS_GREP_TOKENS; i++) {
1194-
tok = r_str_tok_r (i? NULL: in, delims, &save_ptr);
1195-
if (tok) {
1196-
if (grep->tokens[i]) {
1202+
if (use_tok && grep->tokens_count > 0) {
1203+
// dynamic tokens selection: pick only specified columns
1204+
char *save_ptr2 = NULL;
1205+
size_t tok_idx = 0;
1206+
while ((tok = r_str_tok_r (tok_idx? NULL: in, delims, &save_ptr2))) {
1207+
for (size_t k = 0; k < grep->tokens_count; k++) {
1208+
if ((size_t)grep->tokens[k] == tok_idx) {
11971209
const size_t toklen = strlen (tok);
11981210
memcpy (out + outlen, tok, toklen);
11991211
memcpy (out + outlen + toklen, " ", 2);
@@ -1203,19 +1215,13 @@ R_API int r_cons_grep_line(char *buf, int len) {
12031215
free (out);
12041216
return -1;
12051217
}
1206-
}
1207-
} else {
1208-
if (*out) {
12091218
break;
12101219
}
1211-
free (in);
1212-
free (out);
1213-
return 0;
12141220
}
1221+
tok_idx++;
12151222
}
1216-
outlen = outlen > 0? outlen - 1: 0;
1217-
if (outlen > len) { // should never happen
1218-
R_LOG_ERROR ("r_cons_grep_line: wtf, how you reach this?");
1223+
outlen = outlen > 0 ? outlen - 1 : 0;
1224+
if (outlen > len) {
12191225
free (in);
12201226
free (out);
12211227
return -1;

libr/cons/kons.inc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ static bool lastMatters(RConsContext *C) {
427427
}
428428
return (C->buffer_len > 0 &&
429429
(C->lastEnabled && !C->filter && r_list_empty (C->grep.strings)) \
430-
&& !C->grep.tokens_used && !C->grep.less \
430+
&& (C->grep.tokens_count == 0) && !C->grep.less \
431431
&& !C->grep.json && !C->is_html);
432432
}
433433

@@ -947,7 +947,7 @@ R_API const char *r_kons_get_buffer(RCons *cons, size_t *buffer_len) {
947947
R_API void r_kons_filter(RCons *cons) {
948948
RConsContext *ctx = cons->context;
949949
/* grep */
950-
if (ctx->filter || ctx->grep.tokens_used \
950+
if (ctx->filter || (ctx->grep.tokens_count > 0) \
951951
|| (ctx->grep.strings && r_list_length (ctx->grep.strings) > 0) \
952952
|| ctx->grep.less || ctx->grep.json) {
953953
(void)r_kons_grepbuf (cons);

libr/include/r_anal.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,15 @@ typedef struct r_anal_function_meta_t {
275275
} RAnalFcnMeta;
276276

277277
typedef struct r_anal_function_t {
278-
// TODO R2_600 Use RBinName here
278+
#if R2_600
279+
RBinName name;
280+
#else
279281
char *name;
280-
char *realname; // R2_590: add realname for the mangled one
282+
char *realname;
283+
#endif
284+
const char *callconv; // calling convention, should come from RAnal.constpool
281285
int bits; // ((> bits 0) (set-bits bits))
282286
int type;
283-
const char *callconv; // calling convention, should come from RAnal.constpool
284287
ut64 addr;
285288
HtUP/*<ut64, char *>*/ *labels;
286289
HtPP/*<char *, ut64 *>*/ *label_addrs;

libr/include/r_cons.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ extern "C" {
5858
#define CONS_PALETTE_SIZE 22
5959
#define CONS_COLORS_SIZE 21
6060

61-
// R2_600 - remove more limits
62-
#define R_CONS_GREP_TOKENS 64
63-
#define R_CONS_GREP_COUNT 10
61+
// Grep token and count limits removed: dynamic allocation used
62+
#undef R_CONS_GREP_TOKENS
63+
#undef R_CONS_GREP_COUNT
6464

6565
R_LIB_VERSION_HEADER(r_cons);
6666

@@ -121,8 +121,9 @@ typedef struct r_cons_grep_t {
121121
bool sort_invert;
122122
int f_line; //first line
123123
int l_line; //last line
124-
int tokens[R_CONS_GREP_TOKENS];
125-
int tokens_used;
124+
// dynamic list of token indices for column selection
125+
int *tokens; // allocated array of token indices
126+
size_t tokens_count; // number of tokens in array
126127
int amp;
127128
int zoom;
128129
int zoomy; // if set then its scaled unproportionally

libr/util/sys.c

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ R_API void r_sys_set_environ(char **e) {
13391339
env = e;
13401340
}
13411341

1342-
R_API char *r_sys_whoami(void) {
1342+
R_API char * R_NONNULL r_sys_whoami(void) {
13431343
#if R2__WINDOWS__
13441344
char buf[256];
13451345
DWORD buf_sz = sizeof (buf);
@@ -1395,7 +1395,7 @@ R_API int r_sys_getpid(void) {
13951395
#endif
13961396
}
13971397

1398-
R_API bool r_sys_tts(const char *txt, bool bg) {
1398+
R_API bool r_sys_tts(const char * R_NONNULL txt, bool bg) {
13991399
int i;
14001400
R_RETURN_VAL_IF_FAIL (txt, false);
14011401
const char *says[] = {
@@ -1442,7 +1442,7 @@ R_API const char *r_sys_prefix(const char *pfx) {
14421442
return prefix;
14431443
}
14441444

1445-
R_API RSysInfo *r_sys_info(void) {
1445+
R_API RSysInfo * R_NULLABLE r_sys_info(void) {
14461446
#if R2__UNIX__
14471447
struct utsname un = {{0}};
14481448
if (uname (&un) != -1) {
@@ -1512,7 +1512,7 @@ R_API RSysInfo *r_sys_info(void) {
15121512
return NULL;
15131513
}
15141514

1515-
R_API void r_sys_info_free(RSysInfo *si) {
1515+
R_API void r_sys_info_free(RSysInfo * R_NULLABLE si) {
15161516
if (si) {
15171517
free (si->sysname);
15181518
free (si->nodename);
@@ -1525,35 +1525,23 @@ R_API void r_sys_info_free(RSysInfo *si) {
15251525

15261526
// R2_590 r_sys_endian_tostring() // endian == R_SYS_ENDIAN_BIG "big" .. R_ARCH_CONFIG_IS_BIG_ENDIAN (core->rasm->config)? "big": "little"
15271527

1528-
R_API R_MUSTUSE char *r_file_home(const char *str) {
1529-
char *dst, *home = r_sys_getenv (R_SYS_HOME);
1530-
size_t length;
1528+
R_API R_MUSTUSE char * R_NULLABLE r_file_home(const char * R_NULLABLE str) {
1529+
char *home = r_sys_getenv (R_SYS_HOME);
15311530
if (!home) {
15321531
home = r_file_tmpdir ();
15331532
if (!home) {
15341533
return NULL;
15351534
}
15361535
}
1537-
length = strlen (home) + 1;
1538-
if (R_STR_ISNOTEMPTY (str)) {
1539-
length += strlen (R_SYS_DIR) + strlen (str);
1536+
if (R_STR_ISEMPTY (str)) {
1537+
return home;
15401538
}
1541-
dst = (char *)calloc (1, length);
1542-
if (!dst) {
1543-
goto fail;
1544-
}
1545-
int home_len = strlen (home);
1546-
memcpy (dst, home, home_len + 1);
1547-
if (R_STR_ISNOTEMPTY (str)) {
1548-
dst[home_len] = R_SYS_DIR[0];
1549-
strcpy (dst + home_len + 1, str);
1550-
}
1551-
fail:
1539+
char *homepath = r_str_newf ("%s%s%s", home, R_SYS_DIR, str);
15521540
free (home);
1553-
return dst;
1541+
return homepath;
15541542
}
15551543

1556-
R_API R_MUSTUSE char *r_file_homef(const char *fmt, ...) {
1544+
R_API R_MUSTUSE char * R_NULLABLE r_file_homef(const char * R_NONNULL fmt, ...) {
15571545
va_list ap;
15581546
va_start (ap, fmt);
15591547
char *r = r_str_newvf (fmt, ap);

0 commit comments

Comments
 (0)