Re: [PATCH 1/2] kconfig: preserve the final answer when input has no newline

From: Julian Braha

Date: Sat Sep 05 2026 - 10:12:03 EST


Hi Erkan,

I see that this is your first patch submission to the kernel. Welcome!

First thing, if you run the ./scripts/get_maintainer.pl check on this
patch, it should list a few more people to CC.

On 9/5/26 13:32, Erkan Erdem wrote:
> conf_string() unconditionally removes the last character returned by
> fgets(), assuming that it is a newline. When redirected input ends
> without a newline, the last character is part of the answer instead.
> For example, feeding 42 to an integer prompt stores 4, and feeding
> 0xff to a hexadecimal prompt stores 0xf. Both commands succeed despite
> silently changing the supplied value.
>
> Strip the newline with strcspn() so that a complete answer at EOF is
> preserved. Keep the existing handling of newline-terminated and empty
> answers unchanged.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Assisted-by: LLM
> Signed-off-by: Erkan Erdem <hexvalid@xxxxxxxxx>
> ---
>
> An AI coding assistant found the issue, prepared the fix and changelog,
> and ran the verification below after a request to find reproducible
> functional bugs in Linux development tools.
>
> Validation:
> - Built conf with Clang on macOS and GCC in an x86_64 Linux container,
> using -Wall -Wmissing-prototypes -Wstrict-prototypes -Werror.
> - Ran 60 before/after executions on each platform, covering oldaskconfig
> and oldconfig with string, int and hex answers. Unterminated answers
> retain their last character. Newline, empty-answer, empty-input and
> CRLF controls keep the same results.
> - The 21 existing Kconfig tests pass on macOS.
> - This tests the host configuration tool; a complete kernel was not built.

Since you already have some tests, it would be good to add a basic test
for this in scripts/kconfig/tests.

Kconfig doesn't have as many tests as it should, and it's been
complained about by some core maintainers in the past [1].

>
> scripts/kconfig/conf.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/scripts/kconfig/conf.c b/scripts/kconfig/conf.c
> index fe8ba09b..46c9ca64 100644
> --- a/scripts/kconfig/conf.c
> +++ b/scripts/kconfig/conf.c
> @@ -343,7 +343,7 @@ static int conf_string(struct menu *menu)
> }
> /* fall through */
> default:
> - line[strlen(line)-1] = 0;
> + line[strcspn(line, "\n")] = 0;
> def = line;
> }
> if (def && sym_set_string_value(sym, def))
>
> base-commit: 4d7d9486c04d917265f64c55bd23b2cc4fe7749c

Otherwise this change looks good to me (and is more readable).

Link: https://lore.kernel.org/all/20180206093803.GC31558@xxxxxxxxx/ [1]

- Julian Braha