Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: Wang Nan <wangnan0@huawei.com>
Acked-by: Namhyung 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.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Wang Nan and committed by
Arnaldo Carvalho de Melo
0c8c2077 303cb89a

+22 -3
+1 -1
tools/perf/builtin-report.c
··· 676 676 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str, 677 677 "width[,width...]", 678 678 "don't try to adjust column width, use these fixed values"), 679 - OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator", 679 + OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator", 680 680 "separator for columns, no spaces will be added between " 681 681 "columns '.' is reserved."), 682 682 OPT_BOOLEAN('U', "hide-unresolved", &report.hide_unresolved,
+19 -2
tools/perf/util/parse-options.c
··· 37 37 { 38 38 const char *s, *arg = NULL; 39 39 const int unset = flags & OPT_UNSET; 40 + int err; 40 41 41 42 if (unset && p->opt) 42 43 return opterror(opt, "takes no value", flags); ··· 115 114 return 0; 116 115 117 116 case OPTION_STRING: 117 + err = 0; 118 118 if (unset) 119 119 *(const char **)opt->value = NULL; 120 120 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) 121 121 *(const char **)opt->value = (const char *)opt->defval; 122 122 else 123 - return get_arg(p, opt, flags, (const char **)opt->value); 124 - return 0; 123 + err = get_arg(p, opt, flags, (const char **)opt->value); 124 + 125 + /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */ 126 + if (opt->flags & PARSE_OPT_NOEMPTY) { 127 + const char *val = *(const char **)opt->value; 128 + 129 + if (!val) 130 + return err; 131 + 132 + /* Similar to unset if we are given an empty string. */ 133 + if (val[0] == '\0') { 134 + *(const char **)opt->value = NULL; 135 + return 0; 136 + } 137 + } 138 + 139 + return err; 125 140 126 141 case OPTION_CALLBACK: 127 142 if (unset)
+2
tools/perf/util/parse-options.h
··· 40 40 PARSE_OPT_LASTARG_DEFAULT = 16, 41 41 PARSE_OPT_DISABLED = 32, 42 42 PARSE_OPT_EXCLUSIVE = 64, 43 + PARSE_OPT_NOEMPTY = 128, 43 44 }; 44 45 45 46 struct option; ··· 123 122 #define OPT_LONG(s, l, v, h) { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, long *), .help = (h) } 124 123 #define OPT_U64(s, l, v, h) { .type = OPTION_U64, .short_name = (s), .long_name = (l), .value = check_vtype(v, u64 *), .help = (h) } 125 124 #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) } 125 + #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} 126 126 #define OPT_DATE(s, l, v, h) \ 127 127 { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb } 128 128 #define OPT_CALLBACK(s, l, v, a, h, f) \