Inline r_str_ncpy and define a limit for RStr.var()

This commit is contained in:
pancake 2023-10-06 11:47:20 +02:00 committed by pancake
parent 6bc9d58fce
commit f31fcacf8e
6 changed files with 27 additions and 20 deletions

View File

@ -26,17 +26,16 @@ typedef struct {
static bool cblist(void *user, const ut64 offset, const void *val) {
RVecBacktrace *bt = (RVecBacktrace*)val;
Args *args = (Args *)user;
ut64 *addr;
if (args->opt == 'x') {
ut64 *addr;
R_VEC_FOREACH (bt, addr) {
r_cons_printf ("ax 0x%08"PFMT64x" 0x%08"PFMT64x"\n", offset, *addr);
}
return true;
}
r_cons_printf ("-> 0x%08"PFMT64x"\n", offset);
ut64 addr;
R_VEC_FOREACH (bt, addr) {
r_cons_printf (" `-> 0x%08"PFMT64x"\n", addr);
} else {
r_cons_printf ("-> 0x%08"PFMT64x"\n", offset);
R_VEC_FOREACH (bt, addr) {
r_cons_printf (" `-> 0x%08"PFMT64x"\n", *addr);
}
}
return true;
}

View File

@ -12896,7 +12896,7 @@ static int cmd_anal_all(RCore *core, const char *input) {
if (strchr (input, '?')) {
r_core_cmd_help (core, help_msg_aaa);
} else {
r_str_var (asm_arch, r_config_get (core->config, "asm.arch"));
r_str_var (asm_arch, 32, r_config_get (core->config, "asm.arch"));
bool didAap = false;
char *dh_orig = NULL;
if (r_str_startswith (input, "aaaaa")) {

View File

@ -43,13 +43,9 @@ typedef int (*RStrRangeCallback) (void *, int);
// can be replaced with RString
#define r_strf_buffer(s) char strbuf[s]
#define r_strf_var(n,s, f, ...) char n[s]; snprintf (n, s, f, __VA_ARGS__);
#define r_strf_var(n,s,f, ...) char n[s]; snprintf (n, s, f, __VA_ARGS__);
#define r_strf(s,...) (snprintf (strbuf, sizeof (strbuf), s, __VA_ARGS__)? strbuf: strbuf)
#ifdef _MSC_VER
#define r_str_var(n, s) char n[64]; r_str_ncpy (n, s, 64);
#else
#define r_str_var(n, s) const size_t n##_len = strlen (s) + 1; char n[n##_len]; strncpy (n, s, n##_len);
#endif
#define r_str_var(n,s,f) char n[s]; r_str_ncpy (n, f, s);
typedef struct r_charset_rune_t {
ut8 *ch;
@ -111,7 +107,16 @@ R_API void r_str_filter_zeroline(char *str, int len);
R_API size_t r_str_utf8_codepoint(const char *s, size_t left);
R_API bool r_str_char_fullwidth(const char *s, size_t left);
R_API int r_str_write(int fd, const char *b);
R_API size_t r_str_ncpy(char *dst, const char *src, size_t n);
static inline size_t r_str_ncpy(char *x, const char *y, int z) {
if (z > 0) {
size_t ylen = strlen (y) + 1;
size_t flen = R_MIN (ylen, z);
memcpy (x, y, flen);
x[flen - 1] = 0;
return ylen;
}
return 0;
}
R_API void r_str_sanitize(char *c);
R_API char *r_str_sanitize_sdb_key(const char *s);
R_API const char *r_str_casestr(const char *a, const char *b);

View File

@ -333,7 +333,6 @@ err:
free (efile);
r_io_free (io);
r_search_free (rs);
rafind_options_fini (ro);
return result;
}

View File

@ -212,10 +212,10 @@ R_API size_t r_charset_encode_str(RCharset *rc, ut8 *out, size_t out_len, const
}
char k[32];
char *o = (char*)out;
size_t i;
size_t i, oi;
char *o_end = o + out_len;
bool fine = false;
for (i = 0; i < in_len && o < o_end; i++) {
for (i = oi = 0; i < in_len && o < o_end; i++) {
ut8 ch_in = in[i];
snprintf (k, sizeof (k), "0x%02x", ch_in);
const char *v = sdb_const_get (rc->db, k, 0);
@ -229,10 +229,12 @@ R_API size_t r_charset_encode_str(RCharset *rc, ut8 *out, size_t out_len, const
fine = true;
r_str_unescape (res);
// memcpy (o, res, out_len - i);
r_str_ncpy (o, res, out_len - i);
r_str_ncpy (o, res, out_len - oi);
free (res);
}
o += strlen (o);
size_t di = strlen (o);
oi += di;
o += di;
}
if (!fine) {
return 0;

View File

@ -764,6 +764,7 @@ R_API char *r_str_newf(const char *fmt, ...) {
return p;
}
#if 0
// Secure string copy with null terminator (like strlcpy or strscpy but ours
R_API size_t r_str_ncpy(char *dst, const char *src, size_t n) {
size_t i;
@ -780,6 +781,7 @@ R_API size_t r_str_ncpy(char *dst, const char *src, size_t n) {
dst[i] = 0;
return i;
}
#endif
/* memccmp ("foo.bar", "foo.cow, '.') == 0 */
// Returns 1 if src and dst are equal up until the first instance of ch in src.