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 1q8uGj-0007YA-4R for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Jun 2023 03:00:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q8uGi-0007aZ-0B for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Jun 2023 03:00:28 +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 1q8uGh-0007aE-Nd for pgsql-hackers@lists.postgresql.org; Tue, 13 Jun 2023 03:00:27 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q8uGe-001tdb-NU for pgsql-hackers@postgresql.org; Tue, 13 Jun 2023 03:00:26 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1b3d29cfb17so14246585ad.3 for ; Mon, 12 Jun 2023 20:00:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686625224; x=1689217224; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:from:subject:cc:to:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=frtujgMlSn4vO+WJdxV6zM1CQDvjkcWvmbsukhIRi24=; b=OttCBTggUaAdg7fFjUqivmTdKUNt1YH0E49UIwUryaLu02CRGxvROkblhhlsj6/b+Z 46y5l/yA2HKOSDaNu3zrkfGKmiq1vkgsu1XyDSbL76v3B45yGyPaHU2G+4F0tnDShuu6 Y8uMLld/307GDD0K++x+cRlaZL7J+3uXB5iTOab3hg7h6JnAE8AWBx+kNHuPUN+O17vI 48p81A4ehnNRvpbm+f0A0V6cegPK8KNqj3+XX5DTpDLtDuUz+BDrmf7zeZjCEP29kOhb YdbcSuel0+mrQzhMcLCVdwUPiGNQLV90XDd1z89hgeVadVPOBpihoSbZx7YUXN8vZ/xh 5bZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686625224; x=1689217224; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:from:subject:cc:to:message-id:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=frtujgMlSn4vO+WJdxV6zM1CQDvjkcWvmbsukhIRi24=; b=FUQIy+Cf0yrPWtJrL2mSPMnYiFjw87xBCI813OWhxbNor5s+yNAsy22hHqcbg9OWod FnFbRxBSIpQ3C9JyI2sk0VX9bSTt2V537/0bHNZNePr2PH2ldjdX4CXTvwa7E1s1po7q ezHAk0aiwK6s18IbfQHSVoskAAtIe42L8hM7uUB/W2Y79F9NWG5eyO9ubvhEIkM/xXGe 5Ma2B82jSHYZrdPzpFLCzsdB9ap9QX4x6BABdo2Bc3oanTgd0250S4t8DOM8ROK4OdR/ BE8PNJ4FdoOp8G+j71RH5KY/oNMGCRADCvIMASiIb4lN2ZdbydTj6VQfsBtIhLHna6tQ ARPQ== X-Gm-Message-State: AC+VfDxc+WD31WGk+EESekrUyBuqPEzSwF+XWzbVTxB6oFR8N51I9y2q 0CEGFehEf7RR7Ly+7hxZJyM= X-Google-Smtp-Source: ACHHUZ56QahCc9oYLb//JHMbxvJhOZyxqXt+bCtcxcuFZJxEpUzZ6NZEviBnrjoZ31VH6TBfcaj7Cg== X-Received: by 2002:a17:902:ab14:b0:1b3:d44d:93fd with SMTP id ik20-20020a170902ab1400b001b3d44d93fdmr3888707plb.48.1686625223558; Mon, 12 Jun 2023 20:00:23 -0700 (PDT) Received: from localhost (KD036014041111.ppp-bb.dion.ne.jp. [36.14.41.111]) by smtp.gmail.com with ESMTPSA id n10-20020a170902d2ca00b001b03c225c0asm8956536plc.221.2023.06.12.20.00.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jun 2023 20:00:23 -0700 (PDT) Date: Tue, 13 Jun 2023 12:00:01 +0900 (JST) Message-Id: <20230613.120001.2091320520660271559.horikyota.ntt@gmail.com> To: nathandbossart@gmail.com Cc: pgsql-hackers@postgresql.org Subject: Re: add non-option reordering to in-tree getopt_long From: Kyotaro Horiguchi In-Reply-To: <20230609232257.GA121461@nathanxps13> References: <20230609232257.GA121461@nathanxps13> User-Agent: Mew version 6.8 on Emacs 27.2 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk At Fri, 9 Jun 2023 16:22:57 -0700, Nathan Bossart wrote in > While working on 2dcd157, I noticed cfbot failures for Windows due to tests > with commands that had non-options specified before options. For example, > "createuser myrole -a myadmin" specified a non-option (myrole) before the > option/argument pair (-a admin). To get the tests passing for Windows, > non-options must be placed at the end (e.g., "createuser -a myadmin > myrole"). This same problem was encountered while working on 08951a7 [0], > but it was again fortunately caught with cfbot. Others have not been so > lucky [1] [2] [3]. While I don't see it as reason to change the behavior, I do believe the change could be beneficial from a user's perspective. > The reason for this discrepancy is because Windows uses the in-tree > implementation of getopt_long(), which differs from the other > implementations I've found in that it doesn't reorder non-options to the > end of argv by default. 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. The > implementations I reviewed all reorder argv unless the POSIXLY_CORRECT > environment variable is set or the "optstring" argument begins with '+'. > > The best reasons I can think of to keep the current behavior are 1) > reordering involves writing to the original argv array, which could be > risky (as noted by Tom [4]) and 2) any systems with a getopt_long() > implementation that doesn't reorder non-options could begin failing tests > that take advantage of this behavior. However, this quirk in the in-tree > getopt_long() is periodically missed by hackers, the only systems I'm aware > of that use it are Windows and AIX, and every other implementation of > getopt_long() I saw reorders non-options by default. Furthermore, C99 > omits const decorations in main()'s signature, so modifying argv might not > be too scary. POSIXLY_CORRECT appears to be intended for debugging or feature validation. If we know we can always rearrange argv on those platforms, we don't need it. I would suggest that we turn on the new feature at the compile time on those platforms where we know this rearrangement works, instead of switching at runtime. > Thus, I propose introducing this non-option reordering behavior but > allowing it to be disabled via the POSIXLY_CORRECT environment variable. > The attached patch is my first attempt at implementing this proposal. I > don't think we need to bother with handling '+' at the beginning of > "optstring" since it seems unlikely to be used in PostgreSQL, but it would > probably be easy enough to add if folks want it. > > I briefly looked at getopt() and concluded that we should probably retain > its POSIX-compliant behavior for non-options, as reordering support seems > much less universal than with getopt_long(). AFAICT all client utilities > use getopt_long(), anyway. > > Thoughts? As far as I can see, getopt_long on Rocky9 does *not* rearrange argv until it reaches the end of the array. But it won't matter much. regards. -- Kyotaro Horiguchi NTT Open Source Software Center