pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Quentin Rameau <quinq@fifth.space>
To: pgsql-hackers@lists.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:21:36 +0200
Message-ID: <20190825212136.04b56ac3@fifth.space> (raw)
In-Reply-To: <20190825210009.586cfd5c@fifth.space>
References: <20190825100617.GA6087@fifth.space>
	<14042.1566745352@sss.pgh.pa.us>
	<20190825175338.74ed0f0e@fifth.space>
	<7842.1566752268@sss.pgh.pa.us>
	<20190825210009.586cfd5c@fifth.space>

> 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.

Actually OpenBSD seems to set optopt to '?' by default, so the updated
attached patch ensure we start with an empty optopt.

Attachments:

  [text/x-patch] 0001-Fix-handling-of-option.patch (1.2K, ../20190825212136.04b56ac3@fifth.space/2-0001-Fix-handling-of-option.patch)
  download | inline diff:
From ba0b331e67db4eb1a66ee7a1445a536f9788f0ad 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 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 4730c73396..333b49985a 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -507,6 +507,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
 	int			c;
 
 	memset(options, 0, sizeof *options);
+	optopt = 0;
 
 	while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
 							long_options, &optindex)) != -1)
@@ -667,7 +668,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: <20190825212136.04b56ac3@fifth.space>
Permalink:  ../20190825212136.04b56ac3@fifth.space/
Also on:    postgresql.org/message-id/20190825212136.04b56ac3@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, pgsql-hackers@lists.postgresql.org, tgl@sss.pgh.pa.us
  Subject: Re: [PATCH] Fix missing argument handling in psql getopt
  In-Reply-To: <20190825212136.04b56ac3@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