Skip to contentSkip to conversation

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

PATCH Superseded Hyeoncheol Lee 3 participants 4 messages Oct 17, 2012 – Oct 18, 2012 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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]>
---
 tools/perf/util/probe-event.c |   11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/perf/util/probe-event.c +7 -4
index 49a256e..5cd7c42 100644
@@ -2273,6 +2273,8 @@ int show_available_funcs(const char *target, struct strfilter *_filter,
 	return available_user_funcs(target);
 }
 
+#define MAX_ADDRNAME_LEN  (3 + sizeof(unsigned long long) * 2)
+
 /*
  * uprobe_events only accepts address:
  * Convert function and any offset to address
@@ -2332,7 +2334,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 +2354,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
#1 lore raw
Quoted 14 lines · Hyeoncheol Lee

The function allocated wrong size buffers for names

Cc Masami Hiramatsu <[email protected]>
Cc Srikar Dronamraju <[email protected]>
---
 tools/perf/util/probe-event.c |   11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/perf/util/probe-event.c +0 -0
index 49a256e..5cd7c42 100644

tools/perf/util/probe-event.c int show_available_funcs(const char *target, struct strfilter *_filter, · line 2275

Quoted 3 lines
tools/perf/util/probe-event.c +0 -0
@@ -2273,6 +2273,8 @@ int show_available_funcs(const char *target, struct strfilter *_filter,
 	return available_user_funcs(target);
 }
 

Please add a comment like below on the code.

/*
* sizeof(type) * 2 returns the maximum length of hexadecimal number
* of type, because each half byte is encoding in one hex character.
* Another 3 bytes are for "0x" and null character.
*/

Others are OK for me :)

Thanks!

Quoted 34 lines
tools/perf/util/probe-event.c +7 -4
+#define MAX_ADDRNAME_LEN  (3 + sizeof(unsigned long long) * 2)
+
 /*
  * uprobe_events only accepts address:
  * Convert function and any offset to address
@@ -2332,7 +2334,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 +2354,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]
maintainer
#2 lore raw

Hi,

Quoted 40 lines · Masami Hiramatsu
Quoted 14 lines · Hyeoncheol Lee

The function allocated wrong size buffers for names

Cc Masami Hiramatsu <[email protected]>
Cc Srikar Dronamraju <[email protected]>
---
 tools/perf/util/probe-event.c |   11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/perf/util/probe-event.c +0 -0
index 49a256e..5cd7c42 100644

tools/perf/util/probe-event.c int show_available_funcs(const char *target, struct strfilter *_filter, · line 2275

Quoted 3 lines
tools/perf/util/probe-event.c +0 -0
@@ -2273,6 +2273,8 @@ int show_available_funcs(const char *target, struct strfilter *_filter,
 	return available_user_funcs(target);
 }
 

Please add a comment like below on the code.

/*
* sizeof(type) * 2 returns the maximum length of hexadecimal number
* of type, because each half byte is encoding in one hex character.
* Another 3 bytes are for "0x" and null character.
*/

Others are OK for me :)

Thanks!

Quoted 9 lines
tools/perf/util/probe-event.c +2 -0
+#define MAX_ADDRNAME_LEN  (3 + sizeof(unsigned long long) * 2)
+
 /*
  * uprobe_events only accepts address:
  * Convert function and any offset to address
@@ -2332,7 +2334,7 @@ static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
 	if (!pev->group) {
 		char *ptr1, *ptr2, *exec_copy;
 
Quoted 2 lines

tools/perf/util/probe-event.c static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec) · line 2337

Quoted 2 lines
tools/perf/util/probe-event.c +1 -1
-		pev->group = zalloc(sizeof(char *) * 64);
+		pev->group = zalloc(sizeof(char) * 64);

I guess we don't need this 'sizeof(char)' part?

Quoted 9 lines

tools/perf/util/probe-event.c static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec) · line 2357

Quoted 9 lines
tools/perf/util/probe-event.c +1 -1
 		exec_copy = strdup(exec);
 		if (!exec_copy) {
 			ret = -ENOMEM;
@@ -2352,14 +2354,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);

Ditto.

Thanks,
Namhyung

Quoted 14 lines
Quoted 14 lines
tools/perf/util/probe-event.c +3 -2
 	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) {
#3 lore raw
Quoted 43 lines · Namhyung Kim

Hi,

Quoted 40 lines · Masami Hiramatsu
Quoted 14 lines · Hyeoncheol Lee

The function allocated wrong size buffers for names

Cc Masami Hiramatsu <[email protected]>
Cc Srikar Dronamraju <[email protected]>
---
 tools/perf/util/probe-event.c |   11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/perf/util/probe-event.c +0 -0
index 49a256e..5cd7c42 100644

tools/perf/util/probe-event.c int show_available_funcs(const char *target, struct strfilter *_filter, · line 2275

Quoted 3 lines
tools/perf/util/probe-event.c +0 -0
@@ -2273,6 +2273,8 @@ int show_available_funcs(const char *target, struct strfilter *_filter,
 	return available_user_funcs(target);
 }
 

Please add a comment like below on the code.

/*
* sizeof(type) * 2 returns the maximum length of hexadecimal number
* of type, because each half byte is encoding in one hex character.
* Another 3 bytes are for "0x" and null character.
*/

Others are OK for me :)

Thanks!

Quoted 9 lines
tools/perf/util/probe-event.c +2 -0
+#define MAX_ADDRNAME_LEN  (3 + sizeof(unsigned long long) * 2)
+
 /*
  * uprobe_events only accepts address:
  * Convert function and any offset to address
@@ -2332,7 +2334,7 @@ static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec)
 	if (!pev->group) {
 		char *ptr1, *ptr2, *exec_copy;
 
Quoted 4 lines
Quoted 2 lines

tools/perf/util/probe-event.c static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec) · line 2337

Quoted 2 lines
tools/perf/util/probe-event.c +1 -1
-		pev->group = zalloc(sizeof(char *) * 64);
+		pev->group = zalloc(sizeof(char) * 64);

I guess we don't need this 'sizeof(char)' part?

Indeed, we don't need it from the viewpoint of C. However I'd like to keep that, because it gives us a hint that this buffer is used for string.

Thank you,

Quoted 32 lines
Quoted 11 lines

tools/perf/util/probe-event.c static int convert_name_to_addr(struct perf_probe_event *pev, const char *exec) · line 2357

Quoted 11 lines
tools/perf/util/probe-event.c +1 -1
 		exec_copy = strdup(exec);
 		if (!exec_copy) {
 			ret = -ENOMEM;
@@ -2352,14 +2354,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);

Ditto.

Thanks,
Namhyung

Quoted 14 lines
Quoted 14 lines
tools/perf/util/probe-event.c +3 -2
 	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]