From daaa4883cc523984e567cfb5323bc6e4f032f401 Mon Sep 17 00:00:00 2001
From: Sehrope Sarkuni <sehrope@jackdb.com>
Date: Sat, 29 Aug 2026 00:34:18 +0000
Subject: [PATCH 1/4] Fix optional argument handling in port getopt_long()

A long option with an optional argument and no "=" advanced optind
twice, skipping the next argument, and returned BADARG when optstring
starts with a colon even though nothing is missing.
---
 src/port/getopt_long.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/src/port/getopt_long.c b/src/port/getopt_long.c
index 2e869fed58b..7489a0206ee 100644
--- a/src/port/getopt_long.c
+++ b/src/port/getopt_long.c
@@ -137,31 +137,23 @@ retry:
 					{
 						if (place[namelen] == '=')
 							optarg = place + namelen + 1;
-						else if (optind < argc - 1 &&
-								 has_arg == required_argument)
+						else if (has_arg == optional_argument)
+							optarg = NULL;
+						else if (optind < argc - 1)
 						{
 							optind++;
 							optarg = argv[optind];
 						}
 						else
 						{
+							/* required argument missing */
 							optind++;
-							if (optstring[0] == ':')
-							{
-								place = EMSG;
-								return BADARG;
-							}
-
-							if (opterr && has_arg == required_argument)
+							if (opterr && optstring[0] != ':')
 								fprintf(stderr,
 										"%s: option requires an argument -- %s\n",
 										argv[0], place);
-
 							place = EMSG;
-
-							if (has_arg == required_argument)
-								return BADCH;
-							optarg = NULL;
+							return optstring[0] == ':' ? BADARG : BADCH;
 						}
 					}
 					else
-- 
2.17.1

