Skip to content

Commit b988971

Browse files
committed
tools lib subcmd: Make it an error to pass a signed value to OPTION_UINTEGER
Options marked OPTION_UINTEGER or OPTION_U64 clearly indicates that an unsigned value is expected, so just error out when a negative value is passed, instead of returning something undesired to the tool. E.g.: # perf bench futex hash -t -4 # Running 'futex/hash' benchmark: Error: switch `t' expects an unsigned numerical value Usage: perf bench futex hash <options> -t, --threads <n> Specify amount of threads # Cc: Adrian Hunter <[email protected]> Cc: David Ahern <[email protected]> Cc: Davidlohr Bueso <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Wang Nan <[email protected]> Link: http://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 49b3cd3 commit b988971

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

tools/lib/subcmd/parse-options.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ static int get_value(struct parse_opt_ctx_t *p,
270270
}
271271
if (get_arg(p, opt, flags, &arg))
272272
return -1;
273+
if (arg[0] == '-')
274+
return opterror(opt, "expects an unsigned numerical value", flags);
273275
*(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
274276
if (*s)
275277
return opterror(opt, "expects a numerical value", flags);
@@ -302,6 +304,8 @@ static int get_value(struct parse_opt_ctx_t *p,
302304
}
303305
if (get_arg(p, opt, flags, &arg))
304306
return -1;
307+
if (arg[0] == '-')
308+
return opterror(opt, "expects an unsigned numerical value", flags);
305309
*(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
306310
if (*s)
307311
return opterror(opt, "expects a numerical value", flags);

0 commit comments

Comments
 (0)