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 21:00:09 +0200
Message-ID: <20190825210009.586cfd5c@fifth.space> (raw)
In-Reply-To: <7842.1566752268@sss.pgh.pa.us>
References: <20190825100617.GA6087@fifth.space>
	<14042.1566745352@sss.pgh.pa.us>
	<20190825175338.74ed0f0e@fifth.space>
	<7842.1566752268@sss.pgh.pa.us>

> > “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.”  
> 
> Hm, interesting --- glibc doesn't seem to do that (advance optind past
> argc), nor do any of the principal BSDen.  I see that this could be
> read as requiring it, but it seems like musl is pretty out of step
> by reading it that way.

Yes they don't, but the specification looks pretty clear to me, there's
nothing ambiguous about it.
Now it's a matter of implementation willing to adhere or not to it.

> I actually don't care for that code very much and would prefer that
> we nuke it entirely, because I think it's assuming more than it ought to
> about the meaning of optind: in the case of multiple option letters in one
> argv element, it's unspecified exactly when optind advances.

Yes, optind shouldn't be used this way here.
But it's specified exactly how and when optind advances, following the
above code, which specifies the first use case of separated option and
argument, there's a second case for grouped option and argument:

“Otherwise, optarg shall point to the string following the option
character in that element of argv, and optind shall be incremented by 1.”

> So the other
> problem here is that sometimes it's looking at the argv element *before*
> the relevant one.  (It's easily demonstrated that this is so with glibc's
> getopt().)  Probably that doesn't ever result in wrong behavior in
> practice, but it still seems bogus.
> 
> The normal case of "psql -?" is handled before we ever get to this code,
> so if we just deleted '?' entirely from this logic, it would mostly do
> what we want.  The case that would change is, eg,
> 
> 	psql -f foo -?
> 
> where now you get a usage message but you'd just get an "invalid option"
> complaint without the special case.  Seeing that none of our other
> command-line programs have this special case, I'm not sure why psql
> still does.

Another better way, I think, to fix this is to check for optopt
instead, which would be set to the option which caused the error, which
if empty means there isn't an error.

Patch attached.

Attachments:

  [text/x-patch] 0001-Fix-handling-of-option.patch (959B, ../20190825210009.586cfd5c@fifth.space/2-0001-Fix-handling-of-option.patch)
  download | inline diff:
From 8aaec7d151567385c43d8966f46f0e5e4837c93c Mon Sep 17 00:00:00 2001
From: Quentin Rameau <quinq@fifth.space>
Date: Sun, 25 Aug 2019 20:45:29 +0200
Subject: [PATCH] Fix handling of ? option

Using optind to check back the original given option this way is bogus
and could lead to dereferencing argv out of bounds with a missing
argument to an option.

The proper way to verify if an error has occured is to check if optopt
has been set.
---
 src/bin/psql/startup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 4730c73396..e5f6894177 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -667,7 +667,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
 				break;
 			case '?':
 				/* Actual help option given */
-				if (strcmp(argv[optind - 1], "-?") == 0)
+				if (!optopt)
 				{
 					usage(NOPAGER);
 					exit(EXIT_SUCCESS);
-- 
2.23.0

view thread (9+ messages)  latest in thread

Message-ID: <20190825210009.586cfd5c@fifth.space>
Permalink:  ../20190825210009.586cfd5c@fifth.space/
Also on:    postgresql.org/message-id/20190825210009.586cfd5c@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: <20190825210009.586cfd5c@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