From: Dmitry V. Levin Date: Sun, 2 Jul 2017 10:01:22 +0000 (+0000) Subject: Fix error diagnostics in case of zero argc X-Git-Tag: v4.18~8 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=373265e69957cad0240ccbadb7d17ff448ad581b;p=strace Fix error diagnostics in case of zero argc * 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. --- diff --git a/strace.c b/strace.c index 5ece0247..955a1c9f 100644 --- 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)); diff --git a/tests/.gitignore b/tests/.gitignore index cca763fc..62de90b5 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -413,3 +413,4 @@ xetitimer xetpgid xetpriority xettimeofday +zeroargc diff --git a/tests/Makefile.am b/tests/Makefile.am index fd07b03c..19b7400f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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) diff --git a/tests/options-syntax.test b/tests/options-syntax.test index c187956a..7cfc579b 100755 --- a/tests/options-syntax.test +++ b/tests/options-syntax.test @@ -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 index 00000000..05d47dc3 --- /dev/null +++ b/tests/zeroargc.c @@ -0,0 +1,43 @@ +/* + * Execute an executable with zero argc and specified anvironment. + * + * Copyright (c) 2017 Dmitry V. Levin + * 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 +#include + +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); +}