Skip to contentSkip to conversation

libtracefs: Do not discard const ename for updating it

PATCH New Steven Rostedt 1 participant 1 message Nov 8

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libtracefs: Do not discard const ename for updating it

From: Steven Rostedt <[email protected]>

The const char *ename is set as the event name that is passed into kprobe_alloc(). If the event isn't defined, then it uses the address instead. It creates a copy of the addr and zeros out a ':' if it exists. Currently, this is done by pointing ename at the newly allocated addr copy and it is modified by a temp variable. But the compiler complains that the temp variable discards the const (which it does).

Instead of playing games with the const ename, use a new char *aname variable to copy the addr with and do the updates. Then set ename to point to this variable when the event name doesn't exist.

By initializing aname to NULL it can always be freed without having to test if event is defined or not. This removes having to typecast ename to free it too.

---
 src/tracefs-kprobes.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
src/tracefs-kprobes.c +6 -5
index 09b09d7ace04..2297d078b330 100644
@@ -27,6 +27,7 @@ kprobe_alloc(enum tracefs_dynevent_type type, const char *system, const char *ev
 	struct tracefs_dynevent *kp;
 	const char *sys = system;
 	const char *ename = event;
+	char *aname = NULL;
 	char *tmp;
 
 	if (!addr) {
@@ -37,17 +38,17 @@ kprobe_alloc(enum tracefs_dynevent_type type, const char *system, const char *ev
 		sys = KPROBE_DEFAULT_GROUP;
 
 	if (!event) {
-		ename = strdup(addr);
-		if (!ename)
+		aname = strdup(addr);
+		if (!aname)
 			return NULL;
-		tmp = strchr(ename, ':');
+		tmp = strchr(aname, ':');
 		if (tmp)
 			*tmp = '\0';
+		ename = aname;
 	}
 
 	kp = tfs_dynevent_alloc(type, sys, ename, addr, format);
-	if (!event)
-		free((char *)ename);
+	free(aname);
 
 	return kp;
 }
Signature · 1 line
2.53.0