Commit ae3e58d3dc53a95fde87ea86835d38bdd204dfd2

Zack Weinberg 2017-04-06T11:14:13

getopt: fix fencepost error in ambiguous-W-option handling getopt_long contains an undocumented (AFAICT) feature in which, if you put "W;" in the short-options list, then '-W foo' and '-Wfoo' are treated as equivalent to '--foo'. This is implemented with a partial second copy of the code for handling long options, and that code increments optind one too many times when recovering from an ambiguous abbreviated option, which can cause the main loop to walk past the end of argv and crash. I discovered this while writing a test case that tries to exercise all of getopt's error reporting paths; I wouldn't be surprised to learn that this feature is never used by real applications. * lib/getopt.c (_getopt_internal_r): Don't increment d->optind a second time when reporting ambiguous -W options.