aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/builtin-script.c
diff options
context:
space:
mode:
authorRobert Richter <robert.richter@amd.com>2011-11-25 05:38:40 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2011-12-12 05:44:00 -0500
commit38efb539c13f8f173e381435cdd40463ab5d38de (patch)
treebbb838b6a52b20965b197a01807392e50193f321 /tools/perf/builtin-script.c
parentb3d9468a8bd218a695e3a0ff112cd4efd27b670a (diff)
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.com Signed-off-by: Robert Richter <robert.richter@amd.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/builtin-script.c')
-rw-r--r--tools/perf/builtin-script.c48
1 files changed, 29 insertions, 19 deletions
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 619d6dcaa1d9..ccbfd56d7549 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -693,7 +693,8 @@ static int parse_output_fields(const struct option *opt __used,
693 type = PERF_TYPE_RAW; 693 type = PERF_TYPE_RAW;
694 else { 694 else {
695 fprintf(stderr, "Invalid event type in field string.\n"); 695 fprintf(stderr, "Invalid event type in field string.\n");
696 return -EINVAL; 696 rc = -EINVAL;
697 goto out;
697 } 698 }
698 699
699 if (output[type].user_set) 700 if (output[type].user_set)
@@ -935,6 +936,24 @@ static int read_script_info(struct script_desc *desc, const char *filename)
935 return 0; 936 return 0;
936} 937}
937 938
939static char *get_script_root(struct dirent *script_dirent, const char *suffix)
940{
941 char *script_root, *str;
942
943 script_root = strdup(script_dirent->d_name);
944 if (!script_root)
945 return NULL;
946
947 str = (char *)ends_with(script_root, suffix);
948 if (!str) {
949 free(script_root);
950 return NULL;
951 }
952
953 *str = '\0';
954 return script_root;
955}
956
938static int list_available_scripts(const struct option *opt __used, 957static int list_available_scripts(const struct option *opt __used,
939 const char *s __used, int unset __used) 958 const char *s __used, int unset __used)
940{ 959{
@@ -946,7 +965,6 @@ static int list_available_scripts(const struct option *opt __used,
946 struct script_desc *desc; 965 struct script_desc *desc;
947 char first_half[BUFSIZ]; 966 char first_half[BUFSIZ];
948 char *script_root; 967 char *script_root;
949 char *str;
950 968
951 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); 969 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
952 970
@@ -962,16 +980,14 @@ static int list_available_scripts(const struct option *opt __used,
962 continue; 980 continue;
963 981
964 for_each_script(lang_path, lang_dir, script_dirent, script_next) { 982 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
965 script_root = strdup(script_dirent.d_name); 983 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
966 str = (char *)ends_with(script_root, REPORT_SUFFIX); 984 if (script_root) {
967 if (str) {
968 *str = '\0';
969 desc = script_desc__findnew(script_root); 985 desc = script_desc__findnew(script_root);
970 snprintf(script_path, MAXPATHLEN, "%s/%s", 986 snprintf(script_path, MAXPATHLEN, "%s/%s",
971 lang_path, script_dirent.d_name); 987 lang_path, script_dirent.d_name);
972 read_script_info(desc, script_path); 988 read_script_info(desc, script_path);
989 free(script_root);
973 } 990 }
974 free(script_root);
975 } 991 }
976 } 992 }
977 993
@@ -993,8 +1009,7 @@ static char *get_script_path(const char *script_root, const char *suffix)
993 char script_path[MAXPATHLEN]; 1009 char script_path[MAXPATHLEN];
994 DIR *scripts_dir, *lang_dir; 1010 DIR *scripts_dir, *lang_dir;
995 char lang_path[MAXPATHLEN]; 1011 char lang_path[MAXPATHLEN];
996 char *str, *__script_root; 1012 char *__script_root;
997 char *path = NULL;
998 1013
999 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); 1014 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1000 1015
@@ -1010,23 +1025,18 @@ static char *get_script_path(const char *script_root, const char *suffix)
1010 continue; 1025 continue;
1011 1026
1012 for_each_script(lang_path, lang_dir, script_dirent, script_next) { 1027 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1013 __script_root = strdup(script_dirent.d_name); 1028 __script_root = get_script_root(&script_dirent, suffix);
1014 str = (char *)ends_with(__script_root, suffix); 1029 if (__script_root && !strcmp(script_root, __script_root)) {
1015 if (str) { 1030 free(__script_root);
1016 *str = '\0';
1017 if (strcmp(__script_root, script_root))
1018 continue;
1019 snprintf(script_path, MAXPATHLEN, "%s/%s", 1031 snprintf(script_path, MAXPATHLEN, "%s/%s",
1020 lang_path, script_dirent.d_name); 1032 lang_path, script_dirent.d_name);
1021 path = strdup(script_path); 1033 return strdup(script_path);
1022 free(__script_root);
1023 break;
1024 } 1034 }
1025 free(__script_root); 1035 free(__script_root);
1026 } 1036 }
1027 } 1037 }
1028 1038
1029 return path; 1039 return NULL;
1030} 1040}
1031 1041
1032static bool is_top_script(const char *script_path) 1042static bool is_top_script(const char *script_path)