This patch fixes a use after free warning in the select statement processing

Since my patch for the test suite did not modify sh.c this patch and the last 
one I submitted
do not conflict

-   Oliver Webb <aquahobby...@proton.me>
From b99d35655ce9c0fb9120c9a09bb05472d620bc80 Mon Sep 17 00:00:00 2001
From: Oliver Webb <aquahobby...@proton.me>
Date: Fri, 8 Mar 2024 16:57:04 -0600
Subject: [PATCH] Shut up -Wuse-after-free warning in select processing

---
 toys/pending/sh.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/toys/pending/sh.c b/toys/pending/sh.c
index 2ca2aa01..e331c6f8 100644
--- a/toys/pending/sh.c
+++ b/toys/pending/sh.c
@@ -3930,13 +3930,16 @@ do_then:
             TT.ff->pl = pop_block();
             printf("\n");
           } else {
-            match = atoi(ss);
-            free(ss);
             if (!*ss) {
               TT.ff->pl = blk->start;
+              free(ss);
               continue;
-            } else setvarval(blk->fvar, (match<1 || match>blk->farg.c)
-                                        ? "" : blk->farg.v[match-1]);
+            } else {
+              match = atoi(ss);
+              setvarval(blk->fvar,
+                  (match<1 || match>blk->farg.c) ? "" : blk->farg.v[match-1]);
+            }
+            free(ss);
           }
         } else if (blk->loop >= blk->farg.c) TT.ff->pl = pop_block();
         else if (!strncmp(blk->fvar, "((", 2)) {
-- 
2.44.0

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to