diff mbox series

options: Fix getopts handling of colon in optstr

Message ID CAPHxB8twYdk0v_m-kwQw3crRdNTditd5U10mk7SR8WTdt2vhcw@mail.gmail.com (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show
Series options: Fix getopts handling of colon in optstr | expand

Commit Message

Subhaditya Nath Feb. 6, 2023, 3:08 p.m. UTC
Putting a colon at the beginning of optstring to silence errors doesn't
mean that the colon is a valid option. Before this patch, dash treated
-: as a valid option if the optstring started with a colon. This patch
fixes that problem.

Test:

    getopts :a opt -:
    echo $opt$OPTARG

Correct output - ?:
Invalid output - :
---
 src/options.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Herbert Xu April 6, 2024, 7:35 a.m. UTC | #1
On Mon, Feb 06, 2023 at 08:38:47PM +0530, Subhaditya Nath wrote:
> Putting a colon at the beginning of optstring to silence errors doesn't
> mean that the colon is a valid option. Before this patch, dash treated
> -: as a valid option if the optstring started with a colon. This patch
> fixes that problem.
> 
> Test:
> 
>     getopts :a opt -:
>     echo $opt$OPTARG
> 
> Correct output - ?:
> Invalid output - :
> ---
>  src/options.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Patch applied.  Thanks.
diff mbox series

Patch

diff --git a/src/options.c b/src/options.c
index 2d4bd3b..8593710 100644
--- a/src/options.c
+++ b/src/options.c
@@ -465,7 +465,7 @@  atend:
     }

     c = *p++;
-    for (q = optstr; *q != c; ) {
+    for (q = optstr[0] == ':' ? optstr + 1 : optstr; *q != c; ) {
         if (*q == '\0') {
             if (optstr[0] == ':') {
                 s[0] = c;