casaroli opened a new pull request, #21564:
URL: https://github.com/apache/nuttx/pull/21564

   ## Summary
   
   POSIX says that when `argv[optind]` is `"--"`, getopt() increments `optind` 
and returns -1. When `argv[optind]` is `"-"`, getopt() returns -1 and does not 
change `optind`: `"-"` is an operand, often standard input.
   
   NuttX did not stop at either. getopt() took `"--"` for the option `'-'`, and 
getopt_long() and getopt_long_only() took it for a long option with no name. 
All three returned `'?'` and then parsed the next arguments as options, so `cmd 
-- -x` gave `'?'` and `'x'` instead of the operand `-x`. A `"-"` was skipped: 
getopt() went on with the next argument, and the program lost the `"-"` operand 
(`cmd -a - -b` gave `'a'` and `'b'`).
   
   This PR stops at `"--"` and at `"-"` in getopt_common(), where an argument 
starts. It also removes the special case of an empty option after `'-'`: the 
loop above it never ends with an empty option, so that code did not run.
   
   ## Impact
   
   libc only: getopt(), getopt_long() and getopt_long_only(). The same in flat, 
protected and kernel builds; no arch code. A `"--"` argument now ends the 
options and a `"-"` argument is an operand, as POSIX, glibc and the BSDs do. A 
`"--"` inside the argument of an option (`-o--`) and the other cases do not 
change. No Documentation change: the docs do not describe these cases.
   
   A test for ostest follows in nuttx-apps (testing/ostest/getopt.c), with a 
depends-on line for this PR.
   
   ## Testing
   
   Test program `getoptdd.c` (11 cases): `-a -- -b x`, `--` alone, `-- -a`, 
getopt_long() `-a --long -- --long -a`, getopt_long_only() `-long -- -a`, `-` 
alone, `-a - -b`, getopt_long() `--long - --long`, getopt_long_only() `-long - 
-a`, and two cases that must not change (`-a x -b`, `-o-- -a`). The macOS libc 
passes all 11 (with `POSIXLY_CORRECT` for getopt_long(), which otherwise moves 
operands).
   
   - qemu-armv8a:knsh (kernel build, arm64, QEMU): master fails 9 of 11 (`-a -- 
-b x` gives `a ? b`, `-a - -b` gives `a b`); this PR 11/11 PASS. ostest exits 0 
on qemu-armv8a:knsh and qemu-armv8a:nsh, also its getopt() test.
   - qemu-armv8a:nsh (flat build, arm64) and qemu-armv7a:nsh (flat build, 
32-bit arm, arm-none-eabi-gcc 15.3), the test as a builtin: 11/11 PASS, ostest 
exits 0.
   - mps2-an500:knsh (protected build, Cortex-M7, QEMU): 11/11 PASS.
   - The new ostest cases of the nuttx-apps PR, qemu-armv8a:nsh: they pass with 
this PR (ostest exits 0) and fail on master (assertion in getopt_test(), ostest 
exits with status 256).
   - Build only, with `EXTRAFLAGS="-Wno-cpp -Werror"` (with the first version 
of this PR, `"--"` only, on a tree with three unrelated small libc and header 
fixes): nucleo-f072rb:nsh (Cortex-M0), mps2-an500:knsh (protected), 
rv-virt:pnsh64 (protected, RISC-V), esp32s3-devkit:nsh (Xtensa): all build with 
no compiler warning.
   
   `tools/checkpatch.sh -c -u -m -g origin/master..HEAD` passes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to