]> granicus.if.org Git - strace/commitdiff
Check -s argument early
authorDmitry V. Levin <ldv@altlinux.org>
Mon, 26 Jun 2017 22:30:19 +0000 (22:30 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Mon, 26 Jun 2017 22:30:19 +0000 (22:30 +0000)
* util.c (printstr_ex): Move the check that -s argument
does not exceed -1U / 4 ...
* strace.c (init): ... here.
* tests/options-syntax.test: Check it.

strace.c
tests/options-syntax.test
util.c

index 614ab9ec4c350a0aab8094b9b931efb6ce77a01d..3d5528afbfd43c18f94117246e5263737df50e2d 100644 (file)
--- a/strace.c
+++ b/strace.c
@@ -1733,7 +1733,7 @@ init(int argc, char *argv[])
                        break;
                case 's':
                        i = string_to_uint(optarg);
-                       if (i < 0)
+                       if (i < 0 || (unsigned int) i > -1U / 4)
                                error_opt_arg(c, optarg);
                        max_strlen = i;
                        break;
index 4c778b31b7626ad513e30c585f106e3a5030735e..c187956a10fda64f65d802c5185cc1f5b5692622 100755 (executable)
@@ -127,6 +127,7 @@ check_h 'piping the output and -ff are mutually exclusive' -o '!' -ff true
 check_h "invalid -a argument: '-42'" -a -42
 check_h "invalid -O argument: '-42'" -O -42
 check_h "invalid -s argument: '-42'" -s -42
+check_h "invalid -s argument: '1073741824'" -s 1073741824
 check_h "invalid -I argument: '5'" -I 5
 
 if [ -n "${UID-}" ]; then
diff --git a/util.c b/util.c
index 0167e88193a6e9ca41bbcbbd4f8edb6254174dd6..2ccfe4fe32438199a43936872d8478c68010e305 100644 (file)
--- a/util.c
+++ b/util.c
@@ -759,10 +759,13 @@ printstr_ex(struct tcb *const tcp, const kernel_ulong_t addr,
        }
        /* Allocate static buffers if they are not allocated yet. */
        if (!str) {
-               unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
+               const unsigned int outstr_size =
+                       4 * max_strlen + /* for quotes and NUL */ 3;
+               /*
+                * We can assume that outstr_size / 4 == max_strlen
+                * since we have a guarantee that max_strlen <= -1U / 4.
+                */
 
-               if (outstr_size / 4 != max_strlen)
-                       die_out_of_memory();
                str = xmalloc(max_strlen + 1);
                outstr = xmalloc(outstr_size);
        }