]> granicus.if.org Git - strace/commitdiff
Fix error diagnostics in case of zero argc
authorDmitry V. Levin <ldv@altlinux.org>
Sun, 2 Jul 2017 10:01:22 +0000 (10:01 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Sun, 2 Jul 2017 10:01:22 +0000 (10:01 +0000)
* strace.c (init): Adjust argc along with argv,
check that adjusted argc is sane.
* tests/zeroargc.c: New file.
* tests/.gitignore: Add zeroargc.
* tests/Makefile.am (check_PROGRAMS): Likewise.
* tests/options-syntax.test: Check strace error diagnostics
in case of zero argc.

strace.c
tests/.gitignore
tests/Makefile.am
tests/options-syntax.test
tests/zeroargc.c [new file with mode: 0644]

index 5ece0247648c874752962ade1cd9433ae4c66aa0..955a1c9f5e0b2ac42ba01f3a8aee5a2f1c2058a1 100644 (file)
--- a/strace.c
+++ b/strace.c
@@ -1772,14 +1772,11 @@ init(int argc, char *argv[])
                        break;
                }
        }
-       argv += optind;
-       /* argc -= optind; - no need, argc is not used below */
 
-       acolumn_spaces = xmalloc(acolumn + 1);
-       memset(acolumn_spaces, ' ', acolumn);
-       acolumn_spaces[acolumn] = '\0';
+       argv += optind;
+       argc -= optind;
 
-       if (!argv[0] && !nprocs) {
+       if (argc < 0 || (!argv[0] && !nprocs)) {
                error_msg_and_help("must have PROG [ARGS] or -p PID");
        }
 
@@ -1821,6 +1818,10 @@ init(int argc, char *argv[])
                tflag = 1;
        }
 
+       acolumn_spaces = xmalloc(acolumn + 1);
+       memset(acolumn_spaces, ' ', acolumn);
+       acolumn_spaces[acolumn] = '\0';
+
        sigprocmask(SIG_SETMASK, NULL, &start_set);
        memcpy(&blocked_set, &start_set, sizeof(blocked_set));
 
index cca763fcbd1fc0612e1523f3d3afafeccdd9862c..62de90b53bcdde1ef214b61c6a57bed2b21e961b 100644 (file)
@@ -413,3 +413,4 @@ xetitimer
 xetpgid
 xetpriority
 xettimeofday
+zeroargc
index fd07b03c582fb741187d7f5fc5572dad363985ef..19b7400f97a10adccfcee4f2402ea0449f3b1775 100644 (file)
@@ -136,6 +136,7 @@ check_PROGRAMS = $(PURE_EXECUTABLES) \
        vfork-f \
        wait4-v \
        waitid-v \
+       zeroargc \
        # end of check_PROGRAMS
 
 attach_f_p_LDADD = -lrt -lpthread $(LDADD)
index c187956a10fda64f65d802c5185cc1f5b5692622..7cfc579b5defa42c2eb8c836e82eaddd9c8ff186 100755 (executable)
@@ -130,6 +130,17 @@ check_h "invalid -s argument: '-42'" -s -42
 check_h "invalid -s argument: '1073741824'" -s 1073741824
 check_h "invalid -I argument: '5'" -I 5
 
+cat > "$EXP" << '__EOF__'
+strace: must have PROG [ARGS] or -p PID
+Try 'strace -h' for more information.
+__EOF__
+../zeroargc "$strace_exp" /bin/true 2> "$LOG" &&
+       dump_log_and_fail_with \
+               'zeroargc strace failed to handle the error properly'
+match_diff "$LOG" "$EXP" ||
+       dump_log_and_fail_with \
+               'zeroargc strace failed to print expected diagnostics'
+
 if [ -n "${UID-}" ]; then
        if [ "${UID-}" = 0 ]; then
                umsg="Cannot find user ':nosuchuser:'"
diff --git a/tests/zeroargc.c b/tests/zeroargc.c
new file mode 100644 (file)
index 0000000..05d47dc
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Execute an executable with zero argc and specified anvironment.
+ *
+ * Copyright (c) 2017 Dmitry V. Levin <ldv@altlinux.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "tests.h"
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(const int ac, char **const av)
+{
+       if (ac < 2)
+               error_msg_and_fail("missing operand");
+       const char *const path = av[1];
+       av[1] = 0;
+       execve(path, av + 1, av + 2);
+       perror_msg_and_fail("execve: %s", path);
+}