Commit 0c8c2077 authored by Wang Nan's avatar Wang Nan Committed by Arnaldo Carvalho de Melo

perf report: Don't allow empty argument for '-t'.

Without this patch, perf report cause segfault if pass "" as '-t':

  $ perf report -t ""

   # To display the perf.data header info, please use --header/--header-only options.
   #
   # Samples: 37  of event 'syscalls:sys_enter_write'
   # Event count (approx.): 37
   #
   # Children    SelfCommand   Shared Object         Symbol
   Segmentation fault

Since -t is used to add field-separator for generate table, -t "" is
actually meanless. This patch defines a new OPT_STRING_NOEMPTY() option
generator to ensure user never pass empty string to that option.
Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
Acked-by: default avatarNamhyung Kim <namhyung@kernel.org>
Cc: pi3orama@163.com
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Zefan Li <lizefan@huawei.com>
Link: http://lkml.kernel.org/r/1426251114-198991-1-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 303cb89a
...@@ -676,7 +676,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused) ...@@ -676,7 +676,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str, OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
"width[,width...]", "width[,width...]",
"don't try to adjust column width, use these fixed values"), "don't try to adjust column width, use these fixed values"),
OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator", OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
"separator for columns, no spaces will be added between " "separator for columns, no spaces will be added between "
"columns '.' is reserved."), "columns '.' is reserved."),
OPT_BOOLEAN('U', "hide-unresolved", &report.hide_unresolved, OPT_BOOLEAN('U', "hide-unresolved", &report.hide_unresolved,
......
...@@ -37,6 +37,7 @@ static int get_value(struct parse_opt_ctx_t *p, ...@@ -37,6 +37,7 @@ static int get_value(struct parse_opt_ctx_t *p,
{ {
const char *s, *arg = NULL; const char *s, *arg = NULL;
const int unset = flags & OPT_UNSET; const int unset = flags & OPT_UNSET;
int err;
if (unset && p->opt) if (unset && p->opt)
return opterror(opt, "takes no value", flags); return opterror(opt, "takes no value", flags);
...@@ -114,13 +115,29 @@ static int get_value(struct parse_opt_ctx_t *p, ...@@ -114,13 +115,29 @@ static int get_value(struct parse_opt_ctx_t *p,
return 0; return 0;
case OPTION_STRING: case OPTION_STRING:
err = 0;
if (unset) if (unset)
*(const char **)opt->value = NULL; *(const char **)opt->value = NULL;
else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
*(const char **)opt->value = (const char *)opt->defval; *(const char **)opt->value = (const char *)opt->defval;
else else
return get_arg(p, opt, flags, (const char **)opt->value); err = get_arg(p, opt, flags, (const char **)opt->value);
/* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
if (opt->flags & PARSE_OPT_NOEMPTY) {
const char *val = *(const char **)opt->value;
if (!val)
return err;
/* Similar to unset if we are given an empty string. */
if (val[0] == '\0') {
*(const char **)opt->value = NULL;
return 0; return 0;
}
}
return err;
case OPTION_CALLBACK: case OPTION_CALLBACK:
if (unset) if (unset)
......
...@@ -40,6 +40,7 @@ enum parse_opt_option_flags { ...@@ -40,6 +40,7 @@ enum parse_opt_option_flags {
PARSE_OPT_LASTARG_DEFAULT = 16, PARSE_OPT_LASTARG_DEFAULT = 16,
PARSE_OPT_DISABLED = 32, PARSE_OPT_DISABLED = 32,
PARSE_OPT_EXCLUSIVE = 64, PARSE_OPT_EXCLUSIVE = 64,
PARSE_OPT_NOEMPTY = 128,
}; };
struct option; struct option;
...@@ -122,6 +123,7 @@ struct option { ...@@ -122,6 +123,7 @@ struct option {
#define OPT_LONG(s, l, v, h) { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, long *), .help = (h) } #define OPT_LONG(s, l, v, h) { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, long *), .help = (h) }
#define OPT_U64(s, l, v, h) { .type = OPTION_U64, .short_name = (s), .long_name = (l), .value = check_vtype(v, u64 *), .help = (h) } #define OPT_U64(s, l, v, h) { .type = OPTION_U64, .short_name = (s), .long_name = (l), .value = check_vtype(v, u64 *), .help = (h) }
#define OPT_STRING(s, l, v, a, h) { .type = OPTION_STRING, .short_name = (s), .long_name = (l), .value = check_vtype(v, const char **), (a), .help = (h) } #define OPT_STRING(s, l, v, a, h) { .type = OPTION_STRING, .short_name = (s), .long_name = (l), .value = check_vtype(v, const char **), (a), .help = (h) }
#define OPT_STRING_NOEMPTY(s, l, v, a, h) { .type = OPTION_STRING, .short_name = (s), .long_name = (l), .value = check_vtype(v, const char **), (a), .help = (h), .flags = PARSE_OPT_NOEMPTY}
#define OPT_DATE(s, l, v, h) \ #define OPT_DATE(s, l, v, h) \
{ .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb } { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb }
#define OPT_CALLBACK(s, l, v, a, h, f) \ #define OPT_CALLBACK(s, l, v, a, h, f) \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment