Fix a crash (found when I accidentally had a symlink to a toybox that
didn't support the command in question), and add disambiguating quotes
to the error message.

Before:

  $ ./toybox unknown
  munmap_chunk(): invalid pointer
  Aborted

After:

  $ ./toybox unknown
  toybox: Unknown command 'unknown'
---
 main.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
From 72130f591d54a9885935ffc0dd4bc1c649ecbe19 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Thu, 22 Aug 2019 13:06:03 -0700
Subject: [PATCH] main.c: fix unknown command handling.

Fix a crash (found when I accidentally had a symlink to a toybox that
didn't support the command in question), and add disambiguating quotes
to the error message.

Before:

  $ ./toybox unknown
  munmap_chunk(): invalid pointer
  Aborted

After:

  $ ./toybox unknown
  toybox: Unknown command 'unknown'
---
 main.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/main.c b/main.c
index 2b1fdd61..7d5318cd 100644
--- a/main.c
+++ b/main.c
@@ -30,6 +30,13 @@ struct toy_context toys;
 union global_union this;
 char toybuf[4096], libbuf[4096];
 
+static void unknown(char *name)
+{
+  toys.exitval = 127;
+  toys.which = toy_list;
+  error_exit("Unknown command '%s'", name);
+}
+
 struct toy_list *toy_find(char *name)
 {
   int top, bottom, middle;
@@ -57,6 +64,14 @@ struct toy_list *toy_find(char *name)
   }
 }
 
+struct toy_list *xtoy_find(char *name)
+{
+  struct toy_list *toy = toy_find(name);
+
+  if (!toy) unknown(name);
+  return toy;
+}
+
 // Figure out whether or not anything is using the option parsing logic,
 // because the compiler can't figure out whether or not to optimize it away
 // on its' own.  NEED_OPTIONS becomes a constant allowing if() to optimize
@@ -70,13 +85,6 @@ static const int NEED_OPTIONS =
 #include "generated/newtoys.h"
 0;  // Ends the opts || opts || opts...
 
-static void unknown(char *name)
-{
-  toys.exitval = 127;
-  toys.which = toy_list;
-  error_exit("Unknown command %s", name);
-}
-
 // Setup toybox global state for this command.
 static void toy_singleinit(struct toy_list *which, char *argv[])
 {
@@ -90,7 +98,7 @@ static void toy_singleinit(struct toy_list *which, char *argv[])
   if (CFG_TOYBOX_HELP_DASHDASH && !(which->flags & TOYFLAG_NOHELP) && argv[1]) {
     if (!strcmp(argv[1], "--help")) {
       if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2])
-        if (!(toys.which = toy_find(toys.argv[2]))) unknown(toys.argv[2]);
+        toys.which = xtoy_find(toys.argv[2]);
       show_help(stdout);
       xexit();
     }
@@ -174,7 +182,7 @@ void toy_exec_which(struct toy_list *which, char *argv[])
 // Lookup internal toybox command to run via argv[0]
 void toy_exec(char *argv[])
 {
-  toy_exec_which(toy_find(basename(*argv)), argv);
+  toy_exec_which(xtoy_find(basename(*argv)), argv);
 }
 
 // Multiplexer command, first argument is command to run, rest are args to that.
@@ -190,7 +198,7 @@ void toybox_main(void)
   if (toys.argv[1]) {
     toy_exec(toys.argv+1);
     if (0<readlink(toys.argv[1], libbuf, sizeof(libbuf)))
-      toy_exec_which(toy_find(basename(libbuf)), toys.argv);
+      toy_exec_which(xtoy_find(basename(libbuf)), toys.argv);
   }
 
   // For early error reporting
-- 
2.23.0.187.g17f5b7556c-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to