Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q9x2X-00077R-A2 for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Jun 2023 00:10:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q9x2V-0003Q5-Hv for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Jun 2023 00:10:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q9x2V-0003Pv-7F for pgsql-hackers@lists.postgresql.org; Fri, 16 Jun 2023 00:10:07 +0000 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q9x2S-002eyT-2g for pgsql-hackers@postgresql.org; Fri, 16 Jun 2023 00:10:06 +0000 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-6668c030ec9so238162b3a.1 for ; Thu, 15 Jun 2023 17:10:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686874202; x=1689466202; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=AZD/gKACot7cWzUHOTFstAnFcXHIGOp7V9OKCmf7mnI=; b=BjF5lrC94WSeAB2Arg2vV+4TMwBRA3wI4Oa9NesRnpMAU3jeZMxXpE15Fb29xJDyKK UzaB/Ilu1c7J/aJPGXQQ8EHek2SK3J5GhMzrUmtgu08yprcNjY2q9Ag8cexhAciDNao4 bRHGks3KXxW/PlMW2vvIvCAhxlAJKtMpzFeqq7tAjrKhc+4rL6zCfZrT0XGQZotxytxy mMO3MhwR5u36Bo3Mw0i7738H9Pw7P8Mjox6VNbrxaGA5MV2kq4TR3jc0+Ay9eYSnJx3d KNhO38DUGMgctGEQmydoGeSh2iSi8GX2ynd6fzqvIP4KHQ4I61PyxNnkm9QqUMD1xbHw S9cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686874202; x=1689466202; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=AZD/gKACot7cWzUHOTFstAnFcXHIGOp7V9OKCmf7mnI=; b=I2dPiPFrMfXhjsSwsvGoFb3iRllyP74c9DhTGzq6akXrLkN8VLrTWd3MGzOTgwQnmr RvDVy71Laik+NsjPxXqz3SxrjODmahPy2vguxYSiiC+L6giLefH6shUAGU8ur5dte5uq 40+T7uxatLmb5qdUnnVlij9zccmwbF/mQ+IB4yRdE0vtj6ayJpCLWCWLuvLnbEDN04jY SpbxGoeF4QGuIs2gV8rK+ZGGSYlyDh8e+5dR53S7aYMf9jmzCNZ11WTqi19h9dXw3ZxX nawljYMbcBu7fW2L7rkYtluQJSCngCF9B9y/15V6Q8E2YR12dLTUkAOArUPA/Bs3/uZp gvOw== X-Gm-Message-State: AC+VfDxb4SPS5r/X6HT3WpGEK+JJjgaOINlZ9I5WvIB8PJhFE6MgZCPl 803pkWZF3U12QY4NL/5XhRU= X-Google-Smtp-Source: ACHHUZ7DxnReid5KwIR7AEuIv3yZNWwHBorZYMO3E777mmTVjv+snQl5lJlUYuPU+W6uIgk6aEEGIA== X-Received: by 2002:a05:6a21:788f:b0:116:d3dd:aeaf with SMTP id bf15-20020a056a21788f00b00116d3ddaeafmr1066002pzc.5.1686874201694; Thu, 15 Jun 2023 17:10:01 -0700 (PDT) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id d5-20020a170902c18500b001b3df3ae3f8sm7024887pld.281.2023.06.15.17.10.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jun 2023 17:10:01 -0700 (PDT) Date: Thu, 15 Jun 2023 17:09:59 -0700 From: Nathan Bossart To: Kyotaro Horiguchi Cc: noah@leadboat.com, michael@paquier.xyz, pgsql-hackers@postgresql.org Subject: Re: add non-option reordering to in-tree getopt_long Message-ID: <20230616000959.GA922959@nathanxps13> References: <20230614212816.GA492731@nathanxps13> <20230614221154.GA1656253@rfd.leadboat.com> <20230614224608.GA625054@nathanxps13> <20230615.143034.1555041904838122564.horikyota.ntt@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="pWyiEgJYm5f9v55/" Content-Disposition: inline In-Reply-To: <20230615.143034.1555041904838122564.horikyota.ntt@gmail.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --pWyiEgJYm5f9v55/ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Jun 15, 2023 at 02:30:34PM +0900, Kyotaro Horiguchi wrote: > At Wed, 14 Jun 2023 15:46:08 -0700, Nathan Bossart wrote in >> Hm. IIUC modifying the argv pointers on AIX will modify the process title, >> which could cause 'ps' to temporarily show duplicate/missing arguments >> during option parsing. That doesn't seem too terrible, but if pointer >> assignments aren't atomic, maybe 'ps' could be sent off to another part of >> memory, which does seem terrible. > > Hmm, the discussion seems to be based on the assumption that argv[0] > can be safely redirected to a different memory location. If that's the > case, we can prpbably rearrange the array, even if there's a small > window where ps might display a confusing command line, right? If that's the extent of the breakage, then it seems alright to me. I've attached a new version of the patch that omits the POSIXLY_CORRECT stuff. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --pWyiEgJYm5f9v55/ Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-Teach-in-tree-getopt_long-to-move-non-options-to-.patch" From 89ec098d2515d7cf03b630b787e8f53ca25916b9 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Fri, 9 Jun 2023 15:51:58 -0700 Subject: [PATCH v2 1/1] Teach in-tree getopt_long() to move non-options to the end of argv. --- src/bin/scripts/t/040_createuser.pl | 10 +++++---- src/port/getopt_long.c | 34 +++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/bin/scripts/t/040_createuser.pl b/src/bin/scripts/t/040_createuser.pl index 9ca282181d..ba40ab11a3 100644 --- a/src/bin/scripts/t/040_createuser.pl +++ b/src/bin/scripts/t/040_createuser.pl @@ -42,9 +42,9 @@ $node->issues_sql_like( 'add a role as a member with admin option of the newly created role'); $node->issues_sql_like( [ - 'createuser', '-m', - 'regress_user3', '-m', - 'regress user #4', 'REGRESS_USER5' + 'createuser', 'REGRESS_USER5', + '-m', 'regress_user3', + '-m', 'regress user #4' ], qr/statement: CREATE ROLE "REGRESS_USER5" NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN NOREPLICATION NOBYPASSRLS ROLE regress_user3,"regress user #4";/, 'add a role as a member of the newly created role'); @@ -73,11 +73,13 @@ $node->issues_sql_like( qr/statement: CREATE ROLE regress_user11 NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN NOREPLICATION NOBYPASSRLS IN ROLE regress_user1;/, '--role'); $node->issues_sql_like( - [ 'createuser', '--member-of', 'regress_user1', 'regress_user12' ], + [ 'createuser', 'regress_user12', '--member-of', 'regress_user1' ], qr/statement: CREATE ROLE regress_user12 NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN NOREPLICATION NOBYPASSRLS IN ROLE regress_user1;/, '--member-of'); $node->command_fails([ 'createuser', 'regress_user1' ], 'fails if role already exists'); +$node->command_fails([ 'createuser', 'regress_user1', '-m', 'regress_user2', 'regress_user3' ], + 'fails for too many non-options'); done_testing(); diff --git a/src/port/getopt_long.c b/src/port/getopt_long.c index c989276988..4bbd8e0b85 100644 --- a/src/port/getopt_long.c +++ b/src/port/getopt_long.c @@ -50,8 +50,8 @@ * This implementation does not use optreset. Instead, we guarantee that * it can be restarted on a new argv array after a previous call returned -1, * if the caller resets optind to 1 before the first call of the new series. - * (Internally, this means we must be sure to reset "place" to EMSG before - * returning -1.) + * (Internally, this means we must be sure to reset "place" to EMSG and + * nonopt_start to -1 before returning -1.) */ int getopt_long(int argc, char *const argv[], @@ -60,21 +60,45 @@ getopt_long(int argc, char *const argv[], { static char *place = EMSG; /* option letter processing */ char *oli; /* option letter list index */ + static int nonopt_start = -1; if (!*place) { /* update scanning pointer */ if (optind >= argc) { place = EMSG; + nonopt_start = -1; return -1; } +retry: place = argv[optind]; if (place[0] != '-') { - place = EMSG; - return -1; + char **args = (char **) argv; + + /* + * If only non-options remain, return -1. Else, move the + * non-option to the end and try again. + */ + if (optind == nonopt_start) + { + place = EMSG; + nonopt_start = -1; + return -1; + } + + for (int i = optind; i < argc - 1; i++) + args[i] = args[i + 1]; + args[argc - 1] = place; + + if (nonopt_start == -1) + nonopt_start = argc - 1; + else + nonopt_start--; + + goto retry; } place++; @@ -83,6 +107,7 @@ getopt_long(int argc, char *const argv[], { /* treat "-" as not being an option */ place = EMSG; + nonopt_start = -1; return -1; } @@ -91,6 +116,7 @@ getopt_long(int argc, char *const argv[], /* found "--", treat it as end of options */ ++optind; place = EMSG; + nonopt_start = -1; return -1; } -- 2.25.1 --pWyiEgJYm5f9v55/--