Skip to contentSkip to conversation

kconfig: nconf: NUL-terminate 'line' correctly in fill_window()

PATCH In review Thorsten Blum 3 participants 5 messages Aug 10, 2025 – Aug 11, 2025

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

PATCH 1/1 kconfig: nconf: NUL-terminate 'line' correctly in fill_window()

Use 'min(len, x)' as the index instead of just 'len' to NUL-terminate the copied 'line' string at the correct position.

Add a newline after the local variable declarations to silence a checkpatch warning.

Fixes 692d97c380c6 ("kconfig: new configuration interface (nconfig)")
---
 scripts/kconfig/nconf.gui.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
scripts/kconfig/nconf.gui.c +3 -2
index 7206437e784a..ec021ebd2c52 100644
@@ -175,8 +175,9 @@ void fill_window(WINDOW *win, const char *text)
 	for (i = 0; i < total_lines; i++) {
 		char tmp[x+10];
 		const char *line = get_line(text, i);
-		int len = get_line_length(line);
-		strncpy(tmp, line, min(len, x));
+		int len = min(get_line_length(line), x);
+
+		strncpy(tmp, line, len);
 		tmp[len] = '\0';
 		mvwprintw(win, i, 0, "%s", tmp);
 	}
Signature · 1 line
2.50.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Thorsten Blum

Use 'min(len, x)' as the index instead of just 'len' to NUL-terminate the copied 'line' string at the correct position.

Add a newline after the local variable declarations to silence a checkpatch warning.

Fixes 692d97c380c6 ("kconfig: new configuration interface (nconfig)")

LGTM. Thanks.

Quoted 20 lines
---
 scripts/kconfig/nconf.gui.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
scripts/kconfig/nconf.gui.c +3 -2
index 7206437e784a..ec021ebd2c52 100644
@@ -175,8 +175,9 @@ void fill_window(WINDOW *win, const char *text)
 	for (i = 0; i < total_lines; i++) {
 		char tmp[x+10];
 		const char *line = get_line(text, i);
-		int len = get_line_length(line);
-		strncpy(tmp, line, min(len, x));
+		int len = min(get_line_length(line), x);
+
+		strncpy(tmp, line, len);
 		tmp[len] = '\0';
 		mvwprintw(win, i, 0, "%s", tmp);
 	}
Signature · 1 line
~Randy
#2 lore raw
In reply to Thorsten Blum
Quoted 19 lines · Thorsten Blum

Use 'min(len, x)' as the index instead of just 'len' to NUL-terminate the copied 'line' string at the correct position.

Add a newline after the local variable declarations to silence a checkpatch warning.

Fixes 692d97c380c6 ("kconfig: new configuration interface (nconfig)")
---
  scripts/kconfig/nconf.gui.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
scripts/kconfig/nconf.gui.c +0 -0
index 7206437e784a..ec021ebd2c52 100644
@@ -175,8 +175,9 @@ void fill_window(WINDOW *win, const char *text)
  	for (i = 0; i < total_lines; i++) {

scripts/kconfig/nconf.gui.c void fill_window(WINDOW *win, const char *text) · line 183

Quoted 10 lines
scripts/kconfig/nconf.gui.c +3 -2
  		char tmp[x+10];
  		const char *line = get_line(text, i);
-		int len = get_line_length(line);
-		strncpy(tmp, line, min(len, x));
+		int len = min(get_line_length(line), x);
+
+		strncpy(tmp, line, len);
  		tmp[len] = '\0';
  		mvwprintw(win, i, 0, "%s", tmp);
  	}

Is there a rationale behind the choice to avoid to use snprintf() in these circumstance?
Preferring snprintf() you will not have to take care to compute the position of the NULL terminating character of the string, it's done automatically by this function.

Best regards,

Signature · 1 line
Franco Martelli
#3 lore raw
Quoted 5 lines · Franco Martelli

Is there a rationale behind the choice to avoid to use snprintf() in these circumstance?
Preferring snprintf() you will not have to take care to compute the position of the NULL terminating character of the string, it's done automatically by this function.

I looked into it a bit more and I think we need neither strncpy() nor snprintf() (and no temporary buffer) because this should be sufficient:

	mvwprintw(win, i, 0, "%.*s", len, line);

Unless I'm missing something, I'm happy to send a v2.

Thanks,
Thorsten

#4 lore raw
Quoted 7 lines · Thorsten Blum
Quoted 5 lines · Franco Martelli

Is there a rationale behind the choice to avoid to use snprintf() in these circumstance?
Preferring snprintf() you will not have to take care to compute the position of the NULL terminating character of the string, it's done automatically by this function.

Quoted 6 lines

I looked into it a bit more and I think we need neither strncpy() nor snprintf() (and no temporary buffer) because this should be sufficient:

	mvwprintw(win, i, 0, "%.*s", len, line);

Unless I'm missing something, I'm happy to send a v2.

I submitted this as a new patch instead of a v2:

https://lore.kernel.org/lkml/[email protected]/

Thanks,
Thorsten