Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i1upy-0002fk-OT for pgsql-hackers@arkaria.postgresql.org; Sun, 25 Aug 2019 15:53:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i1upv-0000HD-RP for pgsql-hackers@arkaria.postgresql.org; Sun, 25 Aug 2019 15:53:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i1upv-0000Ax-9O for pgsql-hackers@lists.postgresql.org; Sun, 25 Aug 2019 15:53:47 +0000 Received: from fifth.space ([45.32.148.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i1ups-0000lM-BL for pgsql-hackers@postgresql.org; Sun, 25 Aug 2019 15:53:46 +0000 Received: from fifth.space (localhost [127.0.0.1]) by fifth.space (OpenSMTPD) with ESMTP id 0e5f7f77; Sun, 25 Aug 2019 17:53:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=fifth.space; h=date:from :to:cc:subject:message-id:in-reply-to:references:mime-version :content-type:content-transfer-encoding; s=dkim; bh=3qsUJg/BXVfH MNzwnYeRwpU4M3o=; b=ff5Ny2TyO6EjzkslL+W5fCoebOCu01Y3TuBWhk1DkXoa E7OcK4T5S3rTHS/yUtfafUS5lGIB3GYGxvFfYpyI3IUhTqMJTzexHqvWzpvzLPPr MCEKwfDcVvxToAilPAs5yRXyLDf7yIuRpFw6L2f9FfGUyAXGKOR/FBIQTjcGNR9L F/zhEMBD1DLGi41w+rxWjZZTBzQLUJZ4jJJC3OF75r3mBGhD/fDwZ7i6NCqCNJrh ikIPaK0G2Tz6OAwDgfGc1Twtj484EUMPHiFMi7od5UvecHN4sWZSe9bral5Rvd9b e6KMPjI2qUKX8FBTeN3oA6HZiX8EdCvKUiBlLQnJ0Q== DomainKey-Signature: a=rsa-sha1; c=nofws; d=fifth.space; h=date:from:to :cc:subject:message-id:in-reply-to:references:mime-version :content-type:content-transfer-encoding; q=dns; s=dkim; b=fBtPde 2ip3LU2oCT3ezPQC1CKlF0+pCT1UPnjUf7AMLjv41CEdNHvesTvN1FLebWcLd8QR G89qiXlLSeUnK99/+3NBlCiH5I75cSKjNv/Wt67BrVuuwkUcoCX9c6LuDm0NSjlW r3o6LNuzFOMOVEX7Q+nSduZOKp+/CbsBWGorQjFA4NdlH/+xrEMaWRZfh+hUJ7fv Q/YUdrJ+GyxoU7AhZ35ChG17xEUmDecixc7BeauaAlmwyy6iFVckT4SZ+6E4hcT8 s/1zEZ+ULL5NgGoDPnpq29kTBIhey79C9rELNMY8vJO/XsfiBhRzaU7Aeu0dnzlo 0mrru7h55fHbigvg== Received: from localhost.localdomain ( [IPv6:2a01:e34:ecae:2d51:29d6:6297:bd67:8dee]) by fifth.space (OpenSMTPD) with ESMTPSA id 41fe0448 (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO); Sun, 25 Aug 2019 17:53:40 +0200 (CEST) Date: Sun, 25 Aug 2019 17:53:38 +0200 From: Quentin Rameau To: pgsql-hackers@postgresql.org Cc: Tom Lane Subject: Re: [PATCH] Fix missing argument handling in psql getopt Message-ID: <20190825175338.74ed0f0e@fifth.space> In-Reply-To: <14042.1566745352@sss.pgh.pa.us> References: <20190825100617.GA6087@fifth.space> <14042.1566745352@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello Tom, > > When passing an argument option with a missing argument, strcmp > > would be called with the argv terminating NULL. =20 >=20 > Um ... so how would control get there with optind too large? That's from the getopt specification[0]: =E2=80=9CIf 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.=E2=80=9D So in the case of =E2=80=9Cpsql -h=E2=80=9C, optarg points to argv[2] and o= ptind 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