This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 6c91f95f560 libs/libc/unistd: End the options at "--" and at "-" in 
getopt().
6c91f95f560 is described below

commit 6c91f95f5604e771f866bc090bc778daf2a37f90
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Nov 8 09:38:03 2026 +0100

    libs/libc/unistd: End the options at "--" and at "-" in getopt().
    
    POSIX: when argv[optind] is "--", getopt() increments optind and
    returns -1; when it is "-", getopt() returns -1 and does not change
    optind ("-" is an operand, often standard input).
    
    NuttX took "--" for the option '-' (getopt()) or for a long option
    with no name (getopt_long() and getopt_long_only()): it returned '?'
    and then went on with the next arguments as options.  So "cmd -- -x"
    gave '?' and 'x' instead of the operand "-x".  A "-" was skipped: the
    next argument was parsed as an option, and the program lost the "-"
    operand.
    
    Stop at "--" and at "-" in getopt_common(), where an argument starts.
    Remove the special case of an empty option after '-': the loop above
    never ends with an empty option, so that code did not run.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/unistd/lib_getopt_common.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/libs/libc/unistd/lib_getopt_common.c 
b/libs/libc/unistd/lib_getopt_common.c
index 5e8c669af55..2288969440a 100644
--- a/libs/libc/unistd/lib_getopt_common.c
+++ b/libs/libc/unistd/lib_getopt_common.c
@@ -407,27 +407,28 @@ int getopt_common(int argc, FAR char * const argv[],
            * the first character must be '-'
            */
 
-          if (*go->go_optptr != '-')
+          if (*go->go_optptr != '-' || go->go_optptr[1] == '\0')
             {
-              /* The argument does not start with '-', we are finished */
+              /* The argument does not start with '-' or it is "-" (an
+               * operand), we are finished
+               */
 
               go->go_binitialized = false;
               return ERROR;
             }
 
-          /* Skip over the '-' */
+          /* "--" ends the options: skip over it and stop */
 
-          go->go_optptr++;
-        }
-
-      /* Special case handling of "-" and "-:" */
+          if (strcmp(go->go_optptr, "--") == 0)
+            {
+              go->go_optind++;
+              go->go_binitialized = false;
+              return ERROR;
+            }
 
-      if (!*go->go_optptr)
-        {
-          /* We'll fix up optptr the next time we are called */
+          /* Skip over the '-' */
 
-          go->go_optopt = '\0';
-          return '?';
+          go->go_optptr++;
         }
 
       /* Handle the case of "-:" */

Reply via email to