From: Alexey Gladkov <legion@kernel.org>
To: Oleg Nesterov <oleg@redhat.com>
Cc: Chris Li <sparse@chrisli.org>,
Luc Van Oostenryck <lucvoo@kernel.org>,
linux-sparse@vger.kernel.org
Subject: Re: [PATCH 2/2] sparse/semind: robustify parse_cmdline_add()
Date: Sat, 27 Dec 2025 16:58:08 +0100 [thread overview]
Message-ID: <aVACEIgAFSJgUJYj@example.org> (raw)
In-Reply-To: <aU6ceAt7RhlwGJ9-@redhat.com>
On Fri, Dec 26, 2025 at 03:32:24PM +0100, Oleg Nesterov wrote:
> "semind add -ftabstop=8 ..." works as expected, but (for example)
> "semind add --param=dissect-show-all-symbols ..." doesn't, this
> arg is not passed to sparse_initialize().
>
> Because in the latter case getopt_long() increments optind when it
> sees --param=dissect-show-all-symbols. I have no idea if getopt_long()
> is correct or not, but lets change parse_cmdline_add() so that it doesn't
> depend on getopt_long()'s behaviour.
>
> Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Hm. This behavior seems very strange to me. I couldn't find an explanation
for it in getopt_long(3) man page. But I tried this sample with glibc and
musl, and the behavior was the same and does not depend on POSIXLY_CORRECT
#include <stdio.h>
#include <getopt.h>
int main(int argc, char **argv)
{
char c;
opterr = 0;
while ((c = getopt_long(argc, argv, "", NULL, NULL)) != -1) {
if (c != '?')
printf("unexpected known option: %c\n", c);
break;
}
printf("optind=%d\n", optind);
return 0;
}
Here are the results from both libc:
for o in --f "--f f" -f -ff "-f f"; do printf '%s\t' "args='$o'"; ./z-musl $o; done
args='--f' optind=1
args='--f f' optind=1
args='-f' optind=2
args='-ff' optind=1
args='-f f' optind=2
So it seems that this is common behavior for different libc's.
Thank you so much for finding this and fixing it!
Acked-by: Alexey Gladkov <legion@kernel.org>
> ---
> semind.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/semind.c b/semind.c
> index fa084e04..e9708444 100644
> --- a/semind.c
> +++ b/semind.c
> @@ -298,7 +298,7 @@ static void parse_cmdline_add(int argc, char **argv)
> { "help", no_argument, NULL, 'h' },
> { NULL }
> };
> - int c;
> + int parsed = optind, c;
>
> opterr = 0;
>
> @@ -315,6 +315,7 @@ static void parse_cmdline_add(int argc, char **argv)
> case '?':
> goto done;
> }
> + parsed = optind;
> }
> done:
> if (optind == argc) {
> @@ -327,9 +328,8 @@ done:
> dissect_show_all_symbols = 1;
>
> // step back since sparse_initialize will ignore argv[0].
> - optind--;
> -
> - sparse_initialize(argc - optind, argv + optind, &semind_filelist);
> + parsed--;
> + sparse_initialize(argc - parsed, argv + parsed, &semind_filelist);
> }
>
> static void parse_cmdline_rm(int argc, char **argv)
> --
> 2.52.0
>
>
--
Rgrds, legion
next prev parent reply other threads:[~2025-12-27 15:58 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-26 14:31 [PATCH 1/2] sparse/semind: add the new --param=dissect-show-compiled option Oleg Nesterov
2025-12-26 14:32 ` [PATCH 2/2] sparse/semind: robustify parse_cmdline_add() Oleg Nesterov
2025-12-27 15:58 ` Alexey Gladkov [this message]
2025-12-27 15:58 ` [PATCH 1/2] sparse/semind: add the new --param=dissect-show-compiled option Alexey Gladkov
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=aVACEIgAFSJgUJYj@example.org \
--to=legion@kernel.org \
--cc=linux-sparse@vger.kernel.org \
--cc=lucvoo@kernel.org \
--cc=oleg@redhat.com \
--cc=sparse@chrisli.org \
/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;
as well as URLs for NNTP newsgroup(s).