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 <martin.lewis....@gmail.com>
---
 libbb/compare_string_array.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

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;
 }
-- 
2.11.0

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to