From acae981690c22102f5ad269cd5e348af36657132 Mon Sep 17 00:00:00 2001 From: rosenrost <52100845+rosenrost@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:15:44 +0100 Subject: [PATCH] Fix endless loop in getopt() on illegal options When an unknown option character was encountered, getopt() reset sp but never advanced to the next option character or argv element, so every subsequent call returned '?' for the same character again. --- sources/getopt.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sources/getopt.c b/sources/getopt.c index 227c68f..6a9a855 100644 --- a/sources/getopt.c +++ b/sources/getopt.c @@ -40,7 +40,13 @@ int getopt(int argc, char * const argv[], const char *optstring) if (print_errors) { fprintf(stderr, "%s: illegal option -- %c\r\n", argv[0], c); } - sp = 1; + /* skip the illegal option character, otherwise we would return '?' forever */ + if (argv[optind][sp + 1] == '\0') { + optind++; + sp = 1; + } else { + sp++; + } optopt = c; return '?'; } else if (cp[1] == ':') {