Re: [PATCH] Fix missing argument handling in psql getopt
Quentin Rameau <quinq@fifth.space>
From: Quentin Rameau <quinq@fifth.space>
To: pgsql-hackers@postgresql.org
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Date: 2019-08-25T15:53:38Z
Lists: pgsql-hackers
Hello Tom, > > When passing an argument option with a missing argument, strcmp > > would be called with the argv terminating NULL. > > Um ... so how would control get there with optind too large? That's from the getopt specification[0]: “If the option was the last character in the string pointed to by an element of argv, then optarg shall contain the next element of argv, and optind shall be incremented by 2. If the resulting value of optind is greater than argc, this indicates a missing option-argument, and getopt() shall return an error indication.” So in the case of “psql -h“, optarg points to argv[2] and optind is set to 3 (optind is initialized to 1, then incremented by 2). > What test case/platform are you considering? Test case is just running psql -h. The bug has been exposed by testing psql on musl. > (There really shouldn't be *any* case where getopt advances > optind past argc, imo.) Actually that's the typical error case, as per the specification quoted above. [0] https://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html
Commits
-
Avoid platform-specific null pointer dereference in psql.
- fb55e95396ca 10.11 landed
- c693c5c4904d 9.4.25 landed
- 65b1cad5a092 9.5.20 landed
- 6338fa3e715a 13.0 landed
- 5fc7b1e939c4 11.6 landed
- 363382521eb2 12.0 landed
- 28d2ce3c7c99 9.6.16 landed