From 11621507fbbc4b231556c0e0488d853178d1e71f Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Mon, 26 Jun 2017 22:30:19 +0000 Subject: [PATCH] Check -s argument early * 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 | 2 +- tests/options-syntax.test | 1 + util.c | 9 ++++++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/strace.c b/strace.c index 614ab9ec..3d5528af 100644 --- 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; diff --git a/tests/options-syntax.test b/tests/options-syntax.test index 4c778b31..c187956a 100755 --- a/tests/options-syntax.test +++ b/tests/options-syntax.test @@ -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 0167e881..2ccfe4fe 100644 --- 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); } -- 2.40.0