Skip to contentSkip to conversation

tracing: use strlcpy instead of strncpy

PATCH In review Lai Jiangshan 4 participants 6 messages May 18, 2009 – May 20, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: use strlcpy instead of strncpy

strlcpy() will add '\0' for the copied string.

[Impact] cleanup

---

kernel/trace/blktrace.c +1 -2
index 05b4747..9359e85 100644
@@ -434,8 +434,7 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
 	if (!buts->buf_size || !buts->buf_nr)
 		return -EINVAL;
 
-	strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
-	buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
+	strlcpy(buts->name, name, BLKTRACE_BDEV_SIZE);
 
 	/*
 	 * some device names have larger paths - convert the slashes
kernel/trace/trace.c +1 -1
index 665a915..2484555 100644
@@ -133,7 +133,7 @@ static char *default_bootup_tracer;
 
 static int __init set_ftrace(char *str)
 {
-	strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
+	strlcpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
 	default_bootup_tracer = bootup_tracer_buf;
 	/* We are using ftrace early, expand it */
 	ring_buffer_expanded = 1;
kernel/trace/trace_branch.c +2 -4
index 7a7a9fd..a84e036 100644
@@ -67,10 +67,8 @@ probe_likely_condition(struct ftrace_branch_data *f, int val, int expect)
 		p--;
 	p++;
 
-	strncpy(entry->func, f->func, TRACE_FUNC_SIZE);
-	strncpy(entry->file, p, TRACE_FILE_SIZE);
-	entry->func[TRACE_FUNC_SIZE] = 0;
-	entry->file[TRACE_FILE_SIZE] = 0;
+	strlcpy(entry->func, f->func, TRACE_FUNC_SIZE);
+	strlcpy(entry->file, p, TRACE_FILE_SIZE);
 	entry->line = f->line;
 	entry->correct = val == expect;
 
#1 lore raw
Quoted 3 lines · Lai Jiangshan

strlcpy() will add '\0' for the copied string.

[Impact] cleanup

...

kernel/trace/trace_branch.c probe_likely_condition(struct ftrace_branch_data *f, int val, int expect) · line 71

Quoted 12 lines
kernel/trace/trace_branch.c +2 -4
@@ -67,10 +67,8 @@ probe_likely_condition(struct ftrace_branch_data *f, int val, int expect)
 		p--;
 	p++;
 
-	strncpy(entry->func, f->func, TRACE_FUNC_SIZE);
-	strncpy(entry->file, p, TRACE_FILE_SIZE);
-	entry->func[TRACE_FUNC_SIZE] = 0;
-	entry->file[TRACE_FILE_SIZE] = 0;
+	strlcpy(entry->func, f->func, TRACE_FUNC_SIZE);
+	strlcpy(entry->file, p, TRACE_FILE_SIZE);

Hmm, this is not the same. +1 is missing here.

#2 lore raw

strlcpy() will add '\0' for the copied string.

[Impact] cleanup

---

kernel/trace/blktrace.c +1 -2
index 05b4747..9359e85 100644
@@ -434,8 +434,7 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
 	if (!buts->buf_size || !buts->buf_nr)
 		return -EINVAL;
 
-	strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
-	buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
+	strlcpy(buts->name, name, BLKTRACE_BDEV_SIZE);
 
 	/*
 	 * some device names have larger paths - convert the slashes
kernel/trace/trace.c +1 -1
index 665a915..2484555 100644
@@ -133,7 +133,7 @@ static char *default_bootup_tracer;
 
 static int __init set_ftrace(char *str)
 {
-	strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
+	strlcpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
 	default_bootup_tracer = bootup_tracer_buf;
 	/* We are using ftrace early, expand it */
 	ring_buffer_expanded = 1;
kernel/trace/trace.h +2 -2
index 6e735d4..e08c952 100644
@@ -155,8 +155,8 @@ struct trace_boot_ret {
 struct trace_branch {
 	struct trace_entry	ent;
 	unsigned	        line;
-	char			func[TRACE_FUNC_SIZE+1];
-	char			file[TRACE_FILE_SIZE+1];
+	char			func[TRACE_FUNC_SIZE];
+	char			file[TRACE_FILE_SIZE];
 	char			correct;
 };
 
kernel/trace/trace_branch.c +2 -4
index 7a7a9fd..a84e036 100644
@@ -67,10 +67,8 @@ probe_likely_condition(struct ftrace_branch_data *f, int val, int expect)
 		p--;
 	p++;
 
-	strncpy(entry->func, f->func, TRACE_FUNC_SIZE);
-	strncpy(entry->file, p, TRACE_FILE_SIZE);
-	entry->func[TRACE_FUNC_SIZE] = 0;
-	entry->file[TRACE_FILE_SIZE] = 0;
+	strlcpy(entry->func, f->func, TRACE_FUNC_SIZE);
+	strlcpy(entry->file, p, TRACE_FILE_SIZE);
 	entry->line = f->line;
 	entry->correct = val == expect;
 
kernel/trace/trace_event_types.h +4 -4
index 5e32e37..2ae6679 100644
@@ -122,10 +122,10 @@ TRACE_EVENT_FORMAT(print, TRACE_PRINT, print_entry, ignore,
 TRACE_EVENT_FORMAT(branch, TRACE_BRANCH, trace_branch, ignore,
 	TRACE_STRUCT(
 		TRACE_FIELD(unsigned int, line, line)
-		TRACE_FIELD_SPECIAL(char func[TRACE_FUNC_SIZE+1], func,
-				    TRACE_FUNC_SIZE+1, func)
-		TRACE_FIELD_SPECIAL(char file[TRACE_FUNC_SIZE+1], file,
-				    TRACE_FUNC_SIZE+1, file)
+		TRACE_FIELD_SPECIAL(char func[TRACE_FUNC_SIZE], func,
+				    TRACE_FUNC_SIZE, func)
+		TRACE_FIELD_SPECIAL(char file[TRACE_FUNC_SIZE], file,
+				    TRACE_FUNC_SIZE, file)
 		TRACE_FIELD(char, correct, correct)
 	),
 	TP_RAW_FMT("%u:%s:%s (%u)")
#3 lore raw
Quoted 1 line · Lai Jiangshan

[Impact] cleanup

Well, not entirely.

kernel/trace/trace.h struct trace_boot_ret { · line 161

Quoted 12 lines
kernel/trace/trace.h +2 -2
@@ -155,8 +155,8 @@ struct trace_boot_ret {
 struct trace_branch {
 	struct trace_entry	ent;
 	unsigned	        line;
-	char			func[TRACE_FUNC_SIZE+1];
-	char			file[TRACE_FILE_SIZE+1];
+	char			func[TRACE_FUNC_SIZE];
+	char			file[TRACE_FILE_SIZE];
 	char			correct;
 };

(without change of the TRACE_????_SIZE definition)

Better avoid a categorization of a patch if the change's scope exceeds that of the categorization.

Signature · 3 lines
Stefan Richter
-=====-=-=== -=-= -==-=
http://arcgraph.de/sr/
#5 lore raw
In reply to Lai Jiangshan
Quoted 3 lines · Lai Jiangshan

strlcpy() will add '\0' for the copied string.

[Impact] cleanup

As Stefan said, the impact line and the changelog is omitting a possible side effect.

Quoted 35 lines

---

kernel/trace/blktrace.c +1 -2
index 05b4747..9359e85 100644
@@ -434,8 +434,7 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
 	if (!buts->buf_size || !buts->buf_nr)
 		return -EINVAL;
 
-	strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
-	buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
+	strlcpy(buts->name, name, BLKTRACE_BDEV_SIZE);
 
 	/*
 	 * some device names have larger paths - convert the slashes
kernel/trace/trace.c +1 -1
index 665a915..2484555 100644
@@ -133,7 +133,7 @@ static char *default_bootup_tracer;
 
 static int __init set_ftrace(char *str)
 {
-	strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
+	strlcpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
 	default_bootup_tracer = bootup_tracer_buf;
 	/* We are using ftrace early, expand it */
 	ring_buffer_expanded = 1;
kernel/trace/trace.h +0 -0
index 6e735d4..e08c952 100644

kernel/trace/trace.h struct trace_boot_ret { · line 159

Quoted 7 lines
kernel/trace/trace.h +2 -2
@@ -155,8 +155,8 @@ struct trace_boot_ret {
 struct trace_branch {
 	struct trace_entry	ent;
 	unsigned	        line;
-	char			func[TRACE_FUNC_SIZE+1];
-	char			file[TRACE_FILE_SIZE+1];
+	char			func[TRACE_FUNC_SIZE];
+	char			file[TRACE_FILE_SIZE];

Although I don't think this is that much risky, this is the possible side effect: we are loosing a character.

I guess it's fine, the CPP constants here seem to be just magic assumptions (func_size = 30, file_size = 20), it just needs to be mentioned in the changelog for future possible bug tracking.

Thanks,
Frederic.

Quoted 45 lines
kernel/trace/trace.h +0 -0
 	char			correct;
 };
 
kernel/trace/trace_branch.c +2 -4
index 7a7a9fd..a84e036 100644
@@ -67,10 +67,8 @@ probe_likely_condition(struct ftrace_branch_data *f, int val, int expect)
 		p--;
 	p++;
 
-	strncpy(entry->func, f->func, TRACE_FUNC_SIZE);
-	strncpy(entry->file, p, TRACE_FILE_SIZE);
-	entry->func[TRACE_FUNC_SIZE] = 0;
-	entry->file[TRACE_FILE_SIZE] = 0;
+	strlcpy(entry->func, f->func, TRACE_FUNC_SIZE);
+	strlcpy(entry->file, p, TRACE_FILE_SIZE);
 	entry->line = f->line;
 	entry->correct = val == expect;
 
kernel/trace/trace_event_types.h +4 -5
index 5e32e37..2ae6679 100644
@@ -122,10 +122,10 @@ TRACE_EVENT_FORMAT(print, TRACE_PRINT, print_entry, ignore,
 TRACE_EVENT_FORMAT(branch, TRACE_BRANCH, trace_branch, ignore,
 	TRACE_STRUCT(
 		TRACE_FIELD(unsigned int, line, line)
-		TRACE_FIELD_SPECIAL(char func[TRACE_FUNC_SIZE+1], func,
-				    TRACE_FUNC_SIZE+1, func)
-		TRACE_FIELD_SPECIAL(char file[TRACE_FUNC_SIZE+1], file,
-				    TRACE_FUNC_SIZE+1, file)
+		TRACE_FIELD_SPECIAL(char func[TRACE_FUNC_SIZE], func,
+				    TRACE_FUNC_SIZE, func)
+		TRACE_FIELD_SPECIAL(char file[TRACE_FUNC_SIZE], file,
+				    TRACE_FUNC_SIZE, file)
 		TRACE_FIELD(char, correct, correct)
 	),
 	TP_RAW_FMT("%u:%s:%s (%u)")
 
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#4 lore raw
In reply to Stefan Richter
Quoted 20 lines · Stefan Richter
Quoted 1 line · Lai Jiangshan

[Impact] cleanup

Well, not entirely.

kernel/trace/trace.h struct trace_boot_ret { · line 161

Quoted 12 lines
kernel/trace/trace.h +2 -2
@@ -155,8 +155,8 @@ struct trace_boot_ret {
 struct trace_branch {
     struct trace_entry    ent;
     unsigned            line;
-    char            func[TRACE_FUNC_SIZE+1];
-    char            file[TRACE_FILE_SIZE+1];
+    char            func[TRACE_FUNC_SIZE];
+    char            file[TRACE_FILE_SIZE];
     char            correct;
 };

(without change of the TRACE_????_SIZE definition)

Quoted 2 lines

Better avoid a categorization of a patch if the change's scope exceeds that of the categorization.

I think it's make no sense to change the magic number TRACE_????_SIZE, I will rewrite my changelog as Frederic suggests.

Thank you very much.

Lai.