Skip to contentSkip to conversation

kconfig/nconf: Ensure null termination where strncpy is used

PATCH In review Shankari Anand 2 participants 2 messages Jun 25, 2025 – Jun 27, 2025

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

PATCH v3 1/1 kconfig/nconf: Ensure null termination where strncpy is used

strncpy() does not guarantee null-termination
if the source string is longer than the destination buffer. Ensure the buffer is explicitly null-terminated to prevent potential string overflows or undefined behavior.

---
v2 -> v3: Remove the replacement to snprint and instead terminate the buffer explicitly.

---
 scripts/kconfig/nconf.c     | 2 ++
 scripts/kconfig/nconf.gui.c | 3 ++-
 2 files changed, 4 insertions(+), 1 deletion(-)
scripts/kconfig/nconf.c +2 -0
index c0b2dabf6c89..cfa4d395dcd4 100644
@@ -592,6 +592,8 @@ static void item_add_str(const char *fmt, ...)
 	strncpy(k_menu_items[index].str,
 		tmp_str,
 		sizeof(k_menu_items[index].str));
+
+	k_menu_items[index].str[sizeof(k_menu_items[index].str) - 1] = '\0';
 
 	free_item(curses_menu_items[index]);
 	curses_menu_items[index] = new_item(
scripts/kconfig/nconf.gui.c +2 -1
index 4bfdf8ac2a9a..475a403ab8ba 100644
@@ -177,7 +177,7 @@ void fill_window(WINDOW *win, const char *text)
 		const char *line = get_line(text, i);
 		int len = get_line_length(line);
 		strncpy(tmp, line, min(len, x));
-		tmp[len] = '\0';
+		tmp[sizeof(tmp) - 1] = '\0';
 		mvwprintw(win, i, 0, "%s", tmp);
 	}
 }
@@ -359,6 +359,7 @@ int dialog_inputbox(WINDOW *main_window,
 	x = (columns-win_cols)/2;
 
 	strncpy(result, init, *result_len);
+	result[*result_len - 1] = '\0';
 
 	/* create the windows */
 	win = newwin(win_lines, win_cols, y, x);

base-commit: c4dce0c094a89b1bc8fde1163342bd6fe29c0370

Signature · 1 line
2.34.1
#1 lore raw
Quoted 9 lines · Shankari Anand

strncpy() does not guarantee null-termination
if the source string is longer than the destination buffer. Ensure the buffer is explicitly null-terminated to prevent potential string overflows or undefined behavior.

---
v2 -> v3: Remove the replacement to snprint and instead terminate the buffer explicitly.

Applied to linux-kbuild.
Thanks.

Quoted 44 lines
---
 scripts/kconfig/nconf.c     | 2 ++
 scripts/kconfig/nconf.gui.c | 3 ++-
 2 files changed, 4 insertions(+), 1 deletion(-)
scripts/kconfig/nconf.c +2 -0
index c0b2dabf6c89..cfa4d395dcd4 100644
@@ -592,6 +592,8 @@ static void item_add_str(const char *fmt, ...)
        strncpy(k_menu_items[index].str,
                tmp_str,
                sizeof(k_menu_items[index].str));
+
+       k_menu_items[index].str[sizeof(k_menu_items[index].str) - 1] = '\0';
 
        free_item(curses_menu_items[index]);
        curses_menu_items[index] = new_item(
scripts/kconfig/nconf.gui.c +2 -1
index 4bfdf8ac2a9a..475a403ab8ba 100644
@@ -177,7 +177,7 @@ void fill_window(WINDOW *win, const char *text)
                const char *line = get_line(text, i);
                int len = get_line_length(line);
                strncpy(tmp, line, min(len, x));
-               tmp[len] = '\0';
+               tmp[sizeof(tmp) - 1] = '\0';
                mvwprintw(win, i, 0, "%s", tmp);
        }
 }
@@ -359,6 +359,7 @@ int dialog_inputbox(WINDOW *main_window,
        x = (columns-win_cols)/2;
 
        strncpy(result, init, *result_len);
+       result[*result_len - 1] = '\0';
 
        /* create the windows */
        win = newwin(win_lines, win_cols, y, x);

base-commit: c4dce0c094a89b1bc8fde1163342bd6fe29c0370 --
2.34.1

Signature · 2 lines
Best Regards
Masahiro Yamada