mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Palmer <daniel@thingy.jp>
To: w@1wt.eu, linux@weissschuh.net
Cc: linux-kernel@vger.kernel.org, Daniel Palmer <daniel@thingy.jp>
Subject: [PATCH] tools/nolibc: getopt: Fix potential out of bounds access
Date: Wed, 20 May 2026 20:19:31 +0900	[thread overview]
Message-ID: <20260520111931.1027758-1-daniel@thingy.jp> (raw)

Running clang-tidy on a program that uses getopt() from nolibc
this warning appears:

getopt.h:80:6: warning: Out of bound access to memory after the end of the string literal [clang-analyzer-security.ArrayBound]
80 |         if (optstring[i] == ':') {

Claude was asked for a reproducer and the human fixed up version
looks like this:

int main(int argc, char **argv, char **envp)
{
        char arg[]  = "-ab";
        char *_argv[] = { "prog", arg, NULL };

        char *optstring = "ab";

        int c = getopt(2, _argv, optstring);
        printf("call 1: '%c'\n", c);

        arg[2] = '\0';

        c = getopt(2, _argv, optstring);
        printf("call 2: '%c'\n", c);

        return 0;
}

This looks like a very unlikely case that an argument
inside of argv is being changed between getopt() calls.

Claude suggests using `-fsanitize=address` to detect the issue
but that doesn't work for nolibc so lets do it manually with
gdb:

Breakpoint 1, getopt (argc=2, argv=0x7fffffffdb20, optstring=0x55555555c0ac "ab") at ./../cleantrees/linux-nolibc/tools/include/nolibc/getopt.h:80
80              if (optstring[i] == ':') {
(gdb) print i
$2 = 3

The length of optstring is 3 and we are accessing the 4th byte.

Adding a check for d becoming 0 in the guard after the loop
stops getopt() getting far enough to access beyond the end
of the array and seems to correct the issue.

This probably isn't fixing a real world issue but it stops
people seeing the scary warning from clang-tidy.

Assisted-by: Claude:claude-4.6-sonnet # reproducer
Signed-off-by: Daniel Palmer <daniel@thingy.jp>
---

While test the other things I am working on with nolibc
I found this. It doesn't look like it needs a name and its
own website but seems to be a valid bug.

 tools/include/nolibc/getopt.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/include/nolibc/getopt.h b/tools/include/nolibc/getopt.h
index 87565e3b6a33..3ad140f692df 100644
--- a/tools/include/nolibc/getopt.h
+++ b/tools/include/nolibc/getopt.h
@@ -71,7 +71,7 @@ int getopt(int argc, char * const argv[], const char *optstring)
 		d = optstring[i++];
 	} while (d && d != c);
 
-	if (d != c || c == ':') {
+	if (!d || d != c || c == ':') {
 		optopt = c;
 		if (optstring[0] != ':' && opterr)
 			fprintf(stderr, "%s: unrecognized option: %c\n", argv[0], *optchar);
-- 
2.53.0


             reply	other threads:[~2026-05-20 11:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-20 11:19 Daniel Palmer [this message]
2026-05-20 16:40 ` Thomas Weißschuh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260520111931.1027758-1-daniel@thingy.jp \
    --to=daniel@thingy.jp \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=w@1wt.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®