From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2 autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 14352 invoked from network); 10 Mar 2023 16:28:34 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 10 Mar 2023 16:28:34 -0000 Received: (qmail 9378 invoked by uid 550); 10 Mar 2023 16:28:31 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 9343 invoked from network); 10 Mar 2023 16:28:30 -0000 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru 0F2F840D403D Date: Fri, 10 Mar 2023 19:28:18 +0300 (MSK) From: Alexander Monakov To: musl@lists.openwall.com In-Reply-To: <20230310161022.108593-1-izbyshev@ispras.ru> Message-ID: References: <20230310161022.108593-1-izbyshev@ispras.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Subject: Re: [musl] [PATCH] getopt: fix null pointer arithmetic ub Hi, On Fri, 10 Mar 2023, Alexey Izbyshev wrote: > When an option that requires an argument is the last character of > argv[argc-1], getopt computes argv[argc] + optpos. While optpos > is always zero in this case, adding it to null pointer is still > undefined. > --- > src/misc/getopt.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/src/misc/getopt.c b/src/misc/getopt.c > index c3f66995..af12973a 100644 > --- a/src/misc/getopt.c > +++ b/src/misc/getopt.c > @@ -87,7 +87,8 @@ int getopt(int argc, char * const argv[], const char *optstring) > if (optstring[i] == ':') { > optarg = 0; > if (optstring[i+1] != ':' || optpos) { > - optarg = argv[optind++] + optpos; > + optarg = argv[optind++]; > + if (optarg) optarg += optpos; Can this be written as 'if (optpos) optarg += optpos;' instead? That will be folded back into plain addition by the compiler. (also (unlike the quoted variant) would allow undefined behavior instrumentation to catch attempted NULL pointer arithmetic) Alexander