From 561555a595d5be112dd7bd39e09bea7f521cb771 Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 5 Nov 2014 00:32:55 +0100 Subject: [PATCH] Fix r_str_nlen for trashed symbols --- libr/bin/format/elf/elf.c | 11 +---------- libr/util/str.c | 9 +++++++-- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/libr/bin/format/elf/elf.c b/libr/bin/format/elf/elf.c index 8f580c6c46..6543f76d58 100644 --- a/libr/bin/format/elf/elf.c +++ b/libr/bin/format/elf/elf.c @@ -9,15 +9,6 @@ static ut64 Elf_(r_bin_elf_get_section_offset)(struct Elf_(r_bin_elf_obj_t) *bin, const char *section_name); -static inline int __strnlen(const char *str, int len) { - int l = 0; - while (*str && --len) { - str++; - l++; - } - return l+1; -} - static int Elf_(r_bin_elf_init_ehdr)(struct Elf_(r_bin_elf_obj_t) *bin) { ut8 e_ident[EI_NIDENT]; int len; @@ -1184,7 +1175,7 @@ if ( free (strtab); return NULL; } - len = __strnlen (&strtab[sym[k].st_name], ELF_STRING_LENGTH-1); + len = r_str_nlen (strtab+sym[k].st_name, ELF_STRING_LENGTH-1); memcpy (ret[ret_ctr].name, &strtab[sym[k].st_name], len); ret[ret_ctr].ordinal = k; ret[ret_ctr].name[ELF_STRING_LENGTH-2] = '\0'; diff --git a/libr/util/str.c b/libr/util/str.c index ec3db150fe..a4f3ee7c08 100644 --- a/libr/util/str.c +++ b/libr/util/str.c @@ -894,8 +894,13 @@ R_API int r_str_ansi_len(const char *str) { // TODO: support wide char strings R_API int r_str_nlen(const char *str, int n) { int len = 0; - while (*str++ && n--) - len++; + if (str) { + while (IS_PRINTABLE (*str) && n>0) { + len++; + str++; + n--; + } + } return len; }