Commit 38efb539 authored by Robert Richter's avatar Robert Richter Committed by Arnaldo Carvalho de Melo

perf script: Fix mem leaks and NULL pointer checks around strdup()s

Fix mem leaks and missing NULL pointer checks after strdup().

And get_script_path() did not free __script_root in case of continue.

Introduce a helper function get_script_root().

Cc: Ingo Molnar <mingo@elte.hu>
Link: http://lkml.kernel.org/r/1322217520-3287-1-git-send-email-robert.richter@amd.comSigned-off-by: default avatarRobert Richter <robert.richter@amd.com>
Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent b3d9468a
...@@ -693,7 +693,8 @@ static int parse_output_fields(const struct option *opt __used, ...@@ -693,7 +693,8 @@ static int parse_output_fields(const struct option *opt __used,
type = PERF_TYPE_RAW; type = PERF_TYPE_RAW;
else { else {
fprintf(stderr, "Invalid event type in field string.\n"); fprintf(stderr, "Invalid event type in field string.\n");
return -EINVAL; rc = -EINVAL;
goto out;
} }
if (output[type].user_set) if (output[type].user_set)
...@@ -935,6 +936,24 @@ static int read_script_info(struct script_desc *desc, const char *filename) ...@@ -935,6 +936,24 @@ static int read_script_info(struct script_desc *desc, const char *filename)
return 0; return 0;
} }
static char *get_script_root(struct dirent *script_dirent, const char *suffix)
{
char *script_root, *str;
script_root = strdup(script_dirent->d_name);
if (!script_root)
return NULL;
str = (char *)ends_with(script_root, suffix);
if (!str) {
free(script_root);
return NULL;
}
*str = '\0';
return script_root;
}
static int list_available_scripts(const struct option *opt __used, static int list_available_scripts(const struct option *opt __used,
const char *s __used, int unset __used) const char *s __used, int unset __used)
{ {
...@@ -946,7 +965,6 @@ static int list_available_scripts(const struct option *opt __used, ...@@ -946,7 +965,6 @@ static int list_available_scripts(const struct option *opt __used,
struct script_desc *desc; struct script_desc *desc;
char first_half[BUFSIZ]; char first_half[BUFSIZ];
char *script_root; char *script_root;
char *str;
snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
...@@ -962,18 +980,16 @@ static int list_available_scripts(const struct option *opt __used, ...@@ -962,18 +980,16 @@ static int list_available_scripts(const struct option *opt __used,
continue; continue;
for_each_script(lang_path, lang_dir, script_dirent, script_next) { for_each_script(lang_path, lang_dir, script_dirent, script_next) {
script_root = strdup(script_dirent.d_name); script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
str = (char *)ends_with(script_root, REPORT_SUFFIX); if (script_root) {
if (str) {
*str = '\0';
desc = script_desc__findnew(script_root); desc = script_desc__findnew(script_root);
snprintf(script_path, MAXPATHLEN, "%s/%s", snprintf(script_path, MAXPATHLEN, "%s/%s",
lang_path, script_dirent.d_name); lang_path, script_dirent.d_name);
read_script_info(desc, script_path); read_script_info(desc, script_path);
}
free(script_root); free(script_root);
} }
} }
}
fprintf(stdout, "List of available trace scripts:\n"); fprintf(stdout, "List of available trace scripts:\n");
list_for_each_entry(desc, &script_descs, node) { list_for_each_entry(desc, &script_descs, node) {
...@@ -993,8 +1009,7 @@ static char *get_script_path(const char *script_root, const char *suffix) ...@@ -993,8 +1009,7 @@ static char *get_script_path(const char *script_root, const char *suffix)
char script_path[MAXPATHLEN]; char script_path[MAXPATHLEN];
DIR *scripts_dir, *lang_dir; DIR *scripts_dir, *lang_dir;
char lang_path[MAXPATHLEN]; char lang_path[MAXPATHLEN];
char *str, *__script_root; char *__script_root;
char *path = NULL;
snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
...@@ -1010,23 +1025,18 @@ static char *get_script_path(const char *script_root, const char *suffix) ...@@ -1010,23 +1025,18 @@ static char *get_script_path(const char *script_root, const char *suffix)
continue; continue;
for_each_script(lang_path, lang_dir, script_dirent, script_next) { for_each_script(lang_path, lang_dir, script_dirent, script_next) {
__script_root = strdup(script_dirent.d_name); __script_root = get_script_root(&script_dirent, suffix);
str = (char *)ends_with(__script_root, suffix); if (__script_root && !strcmp(script_root, __script_root)) {
if (str) { free(__script_root);
*str = '\0';
if (strcmp(__script_root, script_root))
continue;
snprintf(script_path, MAXPATHLEN, "%s/%s", snprintf(script_path, MAXPATHLEN, "%s/%s",
lang_path, script_dirent.d_name); lang_path, script_dirent.d_name);
path = strdup(script_path); return strdup(script_path);
free(__script_root);
break;
} }
free(__script_root); free(__script_root);
} }
} }
return path; return NULL;
} }
static bool is_top_script(const char *script_path) static bool is_top_script(const char *script_path)
......
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