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 1qHyzo-00015Q-DV for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Jul 2023 03:52:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qHyzn-00058q-8h for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Jul 2023 03:52:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qHyzm-00058h-RW for pgsql-hackers@lists.postgresql.org; Sat, 08 Jul 2023 03:52:30 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qHyzj-002e7w-Rq for pgsql-hackers@postgresql.org; Sat, 08 Jul 2023 03:52:29 +0000 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1b8b318c5cfso17535065ad.1 for ; Fri, 07 Jul 2023 20:52:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1688788347; x=1691380347; 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=ArQslwPU7xtII2JGvncWSlhTd5X7gSxaIil1rMoFRGs=; b=nISRT0+JBVxgtbsJK8EJaAXhicKaDL3/GfdaHSQYttNWT47DpCIVeiDHTPmpnv5vLi EcemaxcJ+rWFHIT8tJNlNfMNnAVB1Rry+b/fDlz8tMGFXlYzvIWck4rutcNxesbpnIGn a+gFCgQ5hkUmGtVTUZwTlpMNKwqh0TWsnXAX7SCp25ZqVR9G16chzBNsILjVqh3CwwUL Jkl2Trn8rz2ZbMQFPThWNwqtYLalQkqCRzXDh7KkG2QWZfU5o7Bjn76qTA+CQZTgW23z ftHLn8vZEv19/+ko/PoQVnuliZ43W58rPjZ4zA9UMxAqEldaTwB8XIYfLwgCLFwt0/Ef dcbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688788347; x=1691380347; 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=ArQslwPU7xtII2JGvncWSlhTd5X7gSxaIil1rMoFRGs=; b=JHl8TBjbbXLm41KuUqlrwNCtwqVdeR3eILMT5VlY4HgKZPnpmbRAFw5gXMRVSPGFHj pcM4ey/Y8/G7g4DlEPWGhJUtcdlLoPsgq2O0S6X/nFnZcTnzDIJaa6Eb0egrXzW8+Hgo OJ+FEKVad/ZQZ/okNFcekAIFnbI/X/Oi5SEO2NLNbZoSHZSQzqQxkBw1WqqV2VEXqC3N RJs2EOHN/cNryikTeF+oVglTM30mXSEtSNI9WPrJvsojWoHYyxPE04HMJ08gAiMPDCaS tk+F15PHo82EXaSiL8eoSIAWf8m7wDhaRNLyfQ/V3MUIEd2lMi0UxO3lO5KtlCV1ErpW wuRw== X-Gm-Message-State: ABy/qLYloU/CTLfueMKfMtZ6Lm5hhILowQ4AuZYSofcOM7j3g2Q1c5RA Dh749XUCyvRpqSwD2aGvOBE= X-Google-Smtp-Source: APBJJlFkaU/GF+xBamNI0OqxSGKw3n+I0cab2Ce6NuE84yMDUL+9ub5GoX0nay9dGSZfUtdFsT5YhQ== X-Received: by 2002:a17:902:cec3:b0:1b8:971c:b7b7 with SMTP id d3-20020a170902cec300b001b8971cb7b7mr7713902plg.56.1688788346932; Fri, 07 Jul 2023 20:52:26 -0700 (PDT) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id l11-20020a170902d34b00b001b89bd40294sm3944735plk.226.2023.07.07.20.52.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jul 2023 20:52:26 -0700 (PDT) Date: Fri, 7 Jul 2023 20:52:24 -0700 From: Nathan Bossart To: Kyotaro Horiguchi Cc: michael@paquier.xyz, noah@leadboat.com, pgsql-hackers@postgresql.org Subject: Re: add non-option reordering to in-tree getopt_long Message-ID: <20230708035224.GA4042213@nathanxps13> References: <20230616045828.GA972663@nathanxps13> <20230616.165138.1460643839992276252.horikyota.ntt@gmail.com> <20230616182847.GA71216@nathanxps13> <20230620.141244.1061039848681375358.horikyota.ntt@gmail.com> <20230621175624.GA882103@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="LQksG6bCIzRHxTLp" Content-Disposition: inline In-Reply-To: <20230621175624.GA882103@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --LQksG6bCIzRHxTLp Content-Type: text/plain; charset=us-ascii Content-Disposition: inline I spent some time tidying up the patch and adding a more detailed commit message. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --LQksG6bCIzRHxTLp Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v5-0001-Teach-in-tree-getopt_long-to-move-non-options-to-.patch" From 2525e6aed066fe8eafdaab0d33c8c5df055c7e09 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Fri, 7 Jul 2023 20:00:47 -0700 Subject: [PATCH v5 1/1] Teach in-tree getopt_long() to move non-options to the end of argv. Unlike the other implementations of getopt_long() I could find, the in-tree implementation does not reorder non-options to the end of argv. Instead, it returns -1 as soon as the first non-option is found, even if there are other options listed afterwards. By moving non-options to the end of argv, getopt_long() can parse all specified options and return -1 when only non-options remain. This quirk is periodically missed by hackers (e.g., 869aa40a27, ffd398021c, and d9ddc50baf). This commit introduces the aforementioned non-option reordering behavior to the in-tree getopt_long() implementation. The only systems I'm aware of that use it are Windows and AIX, both of which have been tested. Special thanks to Noah Misch for his help validating behavior on AIX. Reviewed-by: Kyotaro Horiguchi, Michael Paquier Discussion: https://postgr.es/m/20230609232257.GA121461%40nathanxps13 --- src/bin/scripts/t/040_createuser.pl | 10 ++-- src/port/getopt_long.c | 79 ++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 29 deletions(-) diff --git a/src/bin/scripts/t/040_createuser.pl b/src/bin/scripts/t/040_createuser.pl index 9ca282181d..3290e30c88 100644 --- a/src/bin/scripts/t/040_createuser.pl +++ b/src/bin/scripts/t/040_createuser.pl @@ -42,9 +42,8 @@ $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 +72,14 @@ $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..3fc2cf36e9 100644 --- a/src/port/getopt_long.c +++ b/src/port/getopt_long.c @@ -50,8 +50,9 @@ * 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.) + * + * Note that this routine reorders the pointers in argv (despite the const + * qualifier) so that all non-options will be at the end when -1 is returned. */ int getopt_long(int argc, char *const argv[], @@ -60,40 +61,68 @@ 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; + static bool force_nonopt = false; if (!*place) { /* update scanning pointer */ - if (optind >= argc) + for (;;) { - place = EMSG; - return -1; - } + char **args = (char **) argv; - place = argv[optind]; + if (optind >= argc) + { + place = EMSG; + nonopt_start = -1; + force_nonopt = false; + return -1; + } - if (place[0] != '-') - { - place = EMSG; - return -1; - } + place = argv[optind]; + + /* + * Any string that starts with '-' but is not equivalent to '-' or + * '--' is considered an option. If we see an option, we can + * immediately break out of the loop since no argument reordering + * is required. Note that we treat '--' as the end of options and + * immediately force reordering for every subsequent argument. + * This reinstates the original order of all non-options (which + * includes everything after the '--') before we return. + */ + if (!force_nonopt && place[0] == '-' && place[1]) + { + if (place[1] != '-' || place[2]) + break; - place++; + optind++; + force_nonopt = true; + continue; + } - if (!*place) - { - /* treat "-" as not being an option */ - place = EMSG; - return -1; - } + /* + * 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; + force_nonopt = false; + return -1; + } - if (place[0] == '-' && place[1] == '\0') - { - /* found "--", treat it as end of options */ - ++optind; - place = EMSG; - 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--; } + place++; + if (place[0] == '-' && place[1]) { /* long option */ -- 2.25.1 --LQksG6bCIzRHxTLp--