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.
This commit is contained in:
Дмитрий Левин 2017-06-26 22:30:19 +00:00
parent 4a2a9a7cb1
commit 11621507fb
3 changed files with 8 additions and 4 deletions

View File

@ -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;

View File

@ -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

9
util.c
View File

@ -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);
}