Skip to contentSkip to conversation

perf probe: convert_name_to_addr() allocated the wrong size buffers for names

PATCH In review Hyeoncheol Lee 2 participants 2 messages Oct 18, 2012 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf probe: convert_name_to_addr() allocated the wrong size buffers for names

The function allocated wrong size buffers for names

Cc Masami Hiramatsu <[email protected]>
Cc Srikar Dronamraju <[email protected]>

---
v2: - Added a comment for MAX_ADDRNAME_LEN

 tools/perf/util/probe-event.c |   17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
tools/perf/util/probe-event.c +13 -4
index 49a256e..661bbdf 100644
@@ -2274,6 +2274,14 @@ int show_available_funcs(const char *target, struct strfilter *_filter,
 }
 
 /*
+ * MAX_ADDRNAME_LEN is the maximum length of a function name for uprobe_events
+ * Because each half byte is encoded in one hex character,
+ * sizeof(type)*2 is the maximum length of hexadecimal number of the type.
+ * Another 3 bytes are for "0x" and a null character
+ */
+#define MAX_ADDRNAME_LEN  (3 + sizeof(unsigned long long) * 2)
+
+/*
  * uprobe_events only accepts address:
  * Convert function and any offset to address
  */
@@ -2332,7 +2340,7 @@ static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
 	if (!pev->group) {
 		char *ptr1, *ptr2, *exec_copy;
 
-		pev->group = zalloc(sizeof(char *) * 64);
+		pev->group = zalloc(sizeof(char) * 64);
 		exec_copy = strdup(exec);
 		if (!exec_copy) {
 			ret = -ENOMEM;
@@ -2352,14 +2360,15 @@ static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
 		free(exec_copy);
 	}
 	free(pp->function);
-	pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
+	pp->function = zalloc(sizeof(char) * MAX_ADDRNAME_LEN);
 	if (!pp->function) {
 		ret = -ENOMEM;
 		pr_warning("Failed to allocate memory by zalloc.\n");
 		goto out;
 	}
-	e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr);
-	ret = 0;
+	ret = e_snprintf(pp->function, MAX_ADDRNAME_LEN, "0x%llx", vaddr);
+	if (ret > 0)
+		ret = 0;
 
 out:
 	if (map) {
Signature · 1 line
1.7.10.4
Acked-by on v2 1/1
#1 lore raw
Quoted 5 lines · Hyeoncheol Lee

The function allocated wrong size buffers for names

Cc Masami Hiramatsu <[email protected]>
Cc Srikar Dronamraju <[email protected]>

Thanks!

Quoted 54 lines

---
v2: - Added a comment for MAX_ADDRNAME_LEN

 tools/perf/util/probe-event.c |   17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
tools/perf/util/probe-event.c +13 -4
index 49a256e..661bbdf 100644
@@ -2274,6 +2274,14 @@ int show_available_funcs(const char *target, struct strfilter *_filter,
 }
 
 /*
+ * MAX_ADDRNAME_LEN is the maximum length of a function name for uprobe_events
+ * Because each half byte is encoded in one hex character,
+ * sizeof(type)*2 is the maximum length of hexadecimal number of the type.
+ * Another 3 bytes are for "0x" and a null character
+ */
+#define MAX_ADDRNAME_LEN  (3 + sizeof(unsigned long long) * 2)
+
+/*
  * uprobe_events only accepts address:
  * Convert function and any offset to address
  */
@@ -2332,7 +2340,7 @@ static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
 	if (!pev->group) {
 		char *ptr1, *ptr2, *exec_copy;
 
-		pev->group = zalloc(sizeof(char *) * 64);
+		pev->group = zalloc(sizeof(char) * 64);
 		exec_copy = strdup(exec);
 		if (!exec_copy) {
 			ret = -ENOMEM;
@@ -2352,14 +2360,15 @@ static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
 		free(exec_copy);
 	}
 	free(pp->function);
-	pp->function = zalloc(sizeof(char *) * MAX_PROBE_ARGS);
+	pp->function = zalloc(sizeof(char) * MAX_ADDRNAME_LEN);
 	if (!pp->function) {
 		ret = -ENOMEM;
 		pr_warning("Failed to allocate memory by zalloc.\n");
 		goto out;
 	}
-	e_snprintf(pp->function, MAX_PROBE_ARGS, "0x%llx", vaddr);
-	ret = 0;
+	ret = e_snprintf(pp->function, MAX_ADDRNAME_LEN, "0x%llx", vaddr);
+	if (ret > 0)
+		ret = 0;
 
 out:
 	if (map) {
Signature · 4 lines
Masami HIRAMATSU
IT Management Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]