[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#969618: getopt: optarg is NULL outside of loop



Hi,

On 2020-09-06 09:25, Florian Weimer wrote:
> * John Scott:
> 
> > #define _POSIX_C_SOURCE 200809L
> > #include <assert.h>
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <unistd.h>
> > int main(int argc, char *argv[]) {
> > 	int opt;
> > 	while((opt = getopt(argc, argv, "a:")) != -1) {}
> > 	assert(optarg != NULL);
> > }
> >
> > If this is invoked as './a.out -afoo', the inner assertion will
> > the last assertion will fail with glibc.
> 
> POSIX leaves it unspecified if optarg is changed if getopt returns -1.
> Only optind must be left unchanged.  I do not think this is a glibc
> bug (or a musl bug).

Thanks Florian for the explanations. This can be confirmed that optarg
is not NULL by moving the assert in the while loop. So it doesn't seems
like a bug to me.

Aurelien

-- 
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
aurelien@aurel32.net                 http://www.aurel32.net


Reply to: