From c9fc15359ef8fe5aa98ab0308c1563d9bcf99bb8 Mon Sep 17 00:00:00 2001 From: Martin Lewis Date: Thu, 11 Jun 2020 15:45:58 -0500 Subject: compare_string_array: code shrink Code shrink and prevention of possible out of bounds access. function old new delta nth_string 36 26 -10 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-10) Total: -10 bytes text data bss dec hex filename 981342 16915 1872 1000129 f42c1 busybox_old 981332 16915 1872 1000119 f42b7 busybox_unstripped Signed-off-by: Martin Lewis Signed-off-by: Denys Vlasenko --- libbb/compare_string_array.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'libbb') diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c index 01a9df0e2..a06e57d3d 100644 --- a/libbb/compare_string_array.c +++ b/libbb/compare_string_array.c @@ -117,8 +117,11 @@ int FAST_FUNC index_in_substrings(const char *strings, const char *key) const char* FAST_FUNC nth_string(const char *strings, int n) { while (n) { - n--; - strings += strlen(strings) + 1; + if (*strings++ == '\0') { + if (*strings == '\0') /* reached end of strings */ + break; + n--; + } } return strings; } -- cgit v1.2.3