Fix warnings by using more r_str_ncpy

* Fix wrong logic in visual_search

When the string to be searched is wrapped in " ", you want to remove the
first and last quotes and look only for the internal string.

* Increase size to prevent string truncation (and fix compiler warning)
* Fix "destination unchanged after copying no bytes" warning
* Move msp430_register_names where it belongs to
* Fix Wstringop-truncation warning
This commit is contained in:
Riccardo Schirone 2018-12-19 12:07:49 +01:00 committed by radare
parent d95f8ac768
commit 738bc9cb19
9 changed files with 44 additions and 40 deletions

View File

@ -59,7 +59,7 @@ RAnalReilArg *reil_pop_arg(RAnalEsil *esil) {
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (buf[i] == ':') { if (buf[i] == ':') {
tmp_buf[j] = '\0'; tmp_buf[j] = '\0';
strncpy (op->name, tmp_buf, sizeof (op->name) - 1); r_str_ncpy (op->name, tmp_buf, sizeof (op->name));
memset (tmp_buf, 0, sizeof (tmp_buf)); memset (tmp_buf, 0, sizeof (tmp_buf));
j = 0; j = 0;
flag = 1; flag = 1;
@ -76,8 +76,8 @@ RAnalReilArg *reil_pop_arg(RAnalEsil *esil) {
// If we have not encountered a ':' we don't know the size yet. // If we have not encountered a ':' we don't know the size yet.
if (!flag) { if (!flag) {
strncpy(op->name, tmp_buf, sizeof(op->name) - 1); r_str_ncpy (op->name, tmp_buf, sizeof (op->name));
op->type = reil_get_arg_type(esil, op->name); op->type = reil_get_arg_type (esil, op->name);
if (op->type == ARG_REG) { if (op->type == ARG_REG) {
op->size = esil_internal_sizeof_reg(esil, op->name); op->size = esil_internal_sizeof_reg(esil, op->name);
} else if (op->type == ARG_CONST) { } else if (op->type == ARG_CONST) {

View File

@ -665,8 +665,7 @@ static st32 getlistmask(char *input) {
for (i = 0; input[i] != ',' && input[i] != '\0'; i++) { for (i = 0; input[i] != ',' && input[i] != '\0'; i++) {
; ;
} }
strncpy (temp, input, i); r_str_ncpy (temp, input, i + 1);
temp[i] = 0;
input += i; input += i;
if (*input != '\0') { if (*input != '\0') {

View File

@ -3,6 +3,25 @@
#include "msp430_disas.h" #include "msp430_disas.h"
static const char *msp430_register_names[] = {
"pc",
"sp",
"sr",
"cg",
"r4",
"r5",
"r6",
"r7",
"r8",
"r9",
"r10",
"r11",
"r12",
"r13",
"r14",
"r15",
};
static const char *two_op_instrs[] = { static const char *two_op_instrs[] = {
[MSP430_MOV] = "mov", [MSP430_MOV] = "mov",
[MSP430_ADD] = "add", [MSP430_ADD] = "add",

View File

@ -72,25 +72,6 @@ enum msp430_registers {
MSP430_R15, MSP430_R15,
}; };
static const char *msp430_register_names[] = {
"pc",
"sp",
"sr",
"cg",
"r4",
"r5",
"r6",
"r7",
"r8",
"r9",
"r10",
"r11",
"r12",
"r13",
"r14",
"r15",
};
struct msp430_cmd { struct msp430_cmd {
ut8 type; ut8 type;
ut8 opcode; ut8 opcode;

View File

@ -771,23 +771,22 @@ static void findPrevWord(RCore *core) {
static void visual_search(RCore *core) { static void visual_search(RCore *core) {
const ut8 *p; const ut8 *p;
int len, d = core->print->cur; int len, d = core->print->cur;
char str[128], buf[258]; char str[128], buf[sizeof (str) * 2 + 1];
r_line_set_prompt ("search byte/string in block: "); r_line_set_prompt ("search byte/string in block: ");
r_cons_fgets (str, sizeof (str), 0, NULL); r_cons_fgets (str, sizeof (str), 0, NULL);
len = r_hex_str2bin (str, (ut8 *) buf); len = r_hex_str2bin (str, (ut8 *) buf);
if (*str == '"') { if (*str == '"') {
char *e = strncpy (buf + 1, str + 1, sizeof (buf) - 1); r_str_ncpy (buf, str + 1, sizeof (buf));
if (e) {
e--;
if (*e == '"') {
*e = 0;
}
}
len = strlen (buf); len = strlen (buf);
char *e = buf + len - 1;
if (e > buf && *e == '"') {
*e = 0;
len--;
}
} else if (len < 1) { } else if (len < 1) {
strncpy (buf, str, sizeof (buf) - 1); r_str_ncpy (buf, str, sizeof (buf));
len = strlen (str); len = strlen (buf);
} }
p = r_mem_mem (core->block + d, core->blocksize - d, p = r_mem_mem (core->block + d, core->blocksize - d,
(const ut8 *) buf, len); (const ut8 *) buf, len);

View File

@ -219,7 +219,7 @@ R_API int r_debug_reg_list(RDebug *dbg, int type, int size, int rad, const char
case 2: case 2:
{ {
int len, highlight = use_color && pr && pr->cur_enabled && itmidx == pr->cur; int len, highlight = use_color && pr && pr->cur_enabled && itmidx == pr->cur;
char *str, whites[32], content[128]; char *str, whites[32], content[300];
const char *a = "", *b = ""; const char *a = "", *b = "";
if (highlight) { if (highlight) {
a = Color_INVERT; a = Color_INVERT;

View File

@ -118,7 +118,7 @@ R_API int r_fs_shell_prompt(RFSShell* shell, RFS* fs, const char* root) {
eprintf ("%s\n", path); eprintf ("%s\n", path);
} else if (!memcmp (buf, "cd ", 3)) { } else if (!memcmp (buf, "cd ", 3)) {
char opath[PROMPT_PATH_BUFSIZE]; char opath[PROMPT_PATH_BUFSIZE];
strncpy (opath, path, sizeof (opath) - 1); r_str_ncpy (opath, path, sizeof (opath));
input = buf + 3; input = buf + 3;
while (*input == ' ') { while (*input == ' ') {
input++; input++;

View File

@ -39,10 +39,10 @@ R_API bool r_str_range_in(const char *r, ut64 addr);
R_API int r_str_len_utf8(const char *s); R_API int r_str_len_utf8(const char *s);
R_API int r_str_len_utf8char(const char *s, int left); R_API int r_str_len_utf8char(const char *s, int left);
R_API void r_str_filter_zeroline(char *str, int len); R_API void r_str_filter_zeroline(char *str, int len);
R_API int r_str_utf8_codepoint (const char* s, int left); R_API int r_str_utf8_codepoint(const char *s, int left);
R_API bool r_str_char_fullwidth (const char* s, int left); R_API bool r_str_char_fullwidth(const char *s, int left);
R_API int r_str_write(int fd, const char *b); R_API int r_str_write(int fd, const char *b);
R_API void r_str_ncpy(char *dst, const char *src, int n); R_API void r_str_ncpy(char *dst, const char *src, size_t n);
R_API void r_str_sanitize(char *c); R_API void r_str_sanitize(char *c);
R_API const char *r_str_casestr(const char *a, const char *b); R_API const char *r_str_casestr(const char *a, const char *b);
R_API const char *r_str_firstbut(const char *s, char ch, const char *but); R_API const char *r_str_firstbut(const char *s, char ch, const char *but);

View File

@ -744,8 +744,14 @@ R_API char *r_str_newf(const char *fmt, ...) {
} }
// Secure string copy with null terminator (like strlcpy or strscpy but ours // Secure string copy with null terminator (like strlcpy or strscpy but ours
R_API void r_str_ncpy(char *dst, const char *src, int n) { R_API void r_str_ncpy(char *dst, const char *src, size_t n) {
int i; int i;
// do not do anything if n is 0
if (n == 0) {
return;
}
n--; n--;
for (i = 0; src[i] && n > 0; i++, n--) { for (i = 0; src[i] && n > 0; i++, n--) {
dst[i] = src[i]; dst[i] = src[i];