Repository navigation
Conversation
Move the comments that introduce two case labels above the labels, and add a blank line after a declaration. No code change: the object code is identical before and after on qemu-armv8a:nsh. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
POSIX specifies optarg, optind, opterr and optopt as variables. NuttX defines them as macros over per-task storage, because the tasks of a flat build share one copy of the C library data. A program that brings its own getopt() and defines these variables then reads a different optarg than its getopt() writes: the option argument of every option is NULL. In the user space of a kernel build every process has its own copy of the C library data, so there they are now plain variables. getopt_common() copies them to and from its internal state on each call, and getoptargp() and the other accessors return their addresses, so programs built against the old macros keep working. The variables are in a file of their own, so a program that defines them gets no second definition from the library. Flat and protected builds do not change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
casaroli
requested review from
jerpelea,
pkarashchenko and
xiaoxiang781216
as code owners
October 9, 2026 15:16
| #define opterr (*(getopterrp())) | ||
| #define optind (*(getoptindp())) | ||
| #define optopt (*(getoptoptp())) | ||
| #if !defined(CONFIG_BUILD_KERNEL) || defined(__KERNEL__) |
Contributor
There was a problem hiding this comment.
kernel/flat should keep the same behavior. anyway, we have a similar and better fix, will provide soon.
Contributor
Author
There was a problem hiding this comment.
OK, please link your PR here when it is ready and I will close this one. I can test it with a program that defines optarg itself.
|
acassis
approved these changes
Oct 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POSIX specifies
optarg,optind,opterrandoptoptas variables. NuttX defines them as macros over per-task storage, because the tasks of a flat build share one copy of the C library data.A program that brings its own
getopt()and defines these variables does not compile:char *optarg = NULL;expands tochar *(*(getoptargp())) = NULL;. A program that only declares them reads a differentoptargthan itsgetopt()writes.In the user space of a kernel build every process has its own copy of the C library data, so there this PR makes them plain variables.
getopt_common()copies them to and from its internal state on each call, andgetoptargp()and the other accessors return their addresses, so programs built against the old macros keep working. The variables are in a file of their own, so a program that defines them gets no second definition from the library. Flat and protected builds do not change.The first commit fixes the old nxstyle errors in
lib_getopt_common.c(two comments moved above their case labels, one blank line), with no code change: the object code is identical before and after (qemu-armv8a:nsh).Impact
Kernel builds only:
optarg,optind,opterrandoptoptare variables in user space. Programs that use them through the macros, or callgetopt()from the C library, work as before. Flat and protected builds: no change.Testing
qemu-armv8a:knshin QEMU 11. The test programs were built outside the tree against the export package.A program that defines the four variables and calls
getopt(), run asoptvars -f file -v x:Before:
After:
The same program without its own definitions gives the same output before and after.
Kernel builds with
-Wno-cpp -Werror, each followed bymake exportand the appsmake import(all apps of the configuration compiled for user space):qemu-armv8a:knsh,imx93-evk:knsh,qemu-armv7a:knsh,sabre-6quad:netknsh,sama5d4-ek:knsh,rv-virt:knsh,rv-virt:knsh64,rv-virt:ksmp64,rv-virt:knsh_paging,rv-virt:knetnsh64,rv-virt:kfb64,icicle:knsh,canmv230:knsh,esp32s3-devkit:kernel_n8r2.Flat builds with
-Wno-cpp -Werror, size unchanged:lm3s6965-ek:qemu-flat,mps2-an500:nsh.tools/checkpatch.sh -c -u -m -gpasses.