pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Quentin Rameau <quinq@fifth.space>
To: pgsql-hackers@postgresql.org
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Subject: Re: [PATCH] Fix missing argument handling in psql getopt
Date: Sun, 25 Aug 2019 17:53:38 +0200
Message-ID: <20190825175338.74ed0f0e@fifth.space> (raw)
In-Reply-To: <14042.1566745352@sss.pgh.pa.us>
References: <20190825100617.GA6087@fifth.space>
	<14042.1566745352@sss.pgh.pa.us>

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





view thread (9+ messages)  latest in thread

Message-ID: <20190825175338.74ed0f0e@fifth.space>
Permalink:  ../20190825175338.74ed0f0e@fifth.space/
Also on:    postgresql.org/message-id/20190825175338.74ed0f0e@fifth.space

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: quinq@fifth.space, tgl@sss.pgh.pa.us
  Subject: Re: [PATCH] Fix missing argument handling in psql getopt
  In-Reply-To: <20190825175338.74ed0f0e@fifth.space>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox