From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <stable-bounces@dpdk.org>
Received: from dpdk.org (dpdk.org [92.243.14.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 8A9B9A04BC
	for <public@inbox.dpdk.org>; Fri, 25 Sep 2020 01:17:19 +0200 (CEST)
Received: from [92.243.14.124] (localhost [127.0.0.1])
	by dpdk.org (Postfix) with ESMTP id 2C85D1E498;
	Fri, 25 Sep 2020 01:17:19 +0200 (CEST)
Received: from mail-lj1-f193.google.com (mail-lj1-f193.google.com
 [209.85.208.193]) by dpdk.org (Postfix) with ESMTP id CC37C1DEE8;
 Fri, 25 Sep 2020 01:17:16 +0200 (CEST)
Received: by mail-lj1-f193.google.com with SMTP id s205so844511lja.7;
 Thu, 24 Sep 2020 16:17:16 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
 h=from:to:cc:subject:date:message-id:mime-version
 :content-transfer-encoding;
 bh=mg1sCRk2lDLwtm7thbjXQ3tnFpbme5edYyHEhXtM4PU=;
 b=BsLegCIWg4pO3Nq3F0PdC7VYR6RzXi0k314EzRzYyCzl7k8knjuVoQfxBR6i2i/9+R
 KIFeLMI31AKDDnvmf2ed2V7MwsfONG6lMyRpLFHlfN2JzAwvkPkJxSWxMKvJ729di2pF
 HkczCcZM/moinYBIxxW1PYNRL5uphNU3aEvyLCAi9/pBOHXD1iSovNUUbCMobMnuKz11
 WcoEgrf98nnUuq3Rs5TZTFhoRfEP8wCaoE5HrNCoQXG2aFooppsxOmrY3CxfEQb+Tiec
 w7rq8g+71nLbVSLKDRTe5HDC7Kvpilyl4QesTi/J88uOkR+gUGv/zukUQ+ORBvZCNfLE
 J9tQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version
 :content-transfer-encoding;
 bh=mg1sCRk2lDLwtm7thbjXQ3tnFpbme5edYyHEhXtM4PU=;
 b=jZ+uZhxnXeYPVTvEvNbnch1N2N2BlZRjxtM7o2BPmFo7KpWINf2bQhwm2Jy/C/bmpD
 ro7N3h6feMAy5S9f94fNwXngXirkAArH8x1NxHivtlAnGBOYCqAKPzYvdTkrupBVYh7L
 UBsigq+GcJShH+U9ubs7xQ7dzHyCACkEaNOWUDrY5FmABRHYzkiVbLjedASRk8YDVKzk
 vOdJHVjDXvYtLv7jUpOdKEwU1JQhnaraYhzrQnhCtbtTDFEcRXXAzXNARGfXToKH724z
 AIwci4BEQC8oim12o5xFaSjb/bIhi8WgTkkHcTV/25MtGVo5XNdFLtsjNwxR0qLtOHZw
 yv+Q==
X-Gm-Message-State: AOAM531YDwU2D5mFV+MQFq0Gnj9BSCuYCMKUYEH1yUQV+KUeUP+BYkDS
 Zfie1oXCIdr1nZAFwU89dOPuTabbL2mM11vV
X-Google-Smtp-Source: ABdhPJzJwOCMLi7JCe/XJY59HP/+6FZs9p5lPO7tPSxCvyILI6VPBE1KBAgDUNGue+6lj3yVLM6OUg==
X-Received: by 2002:a2e:8805:: with SMTP id x5mr454483ljh.130.1600989436062;
 Thu, 24 Sep 2020 16:17:16 -0700 (PDT)
Received: from localhost.localdomain (broadband-37-110-65-23.ip.moscow.rt.ru.
 [37.110.65.23])
 by smtp.gmail.com with ESMTPSA id y26sm610712lfy.163.2020.09.24.16.17.14
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Thu, 24 Sep 2020 16:17:15 -0700 (PDT)
From: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
To: dev@dpdk.org
Cc: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>, stable@dpdk.org,
 Khoa To <khot@microsoft.com>, Tal Shnaiderman <talshn@nvidia.com>,
 Narcisa Ana Maria Vasile <navasile@linux.microsoft.com>,
 Dmitry Malloy <dmitrym@microsoft.com>,
 Pallavi Kadam <pallavi.kadam@intel.com>
Date: Fri, 25 Sep 2020 02:17:07 +0300
Message-Id: <20200924231707.23554-1-dmitry.kozliuk@gmail.com>
X-Mailer: git-send-email 2.25.4
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Subject: [dpdk-stable] [PATCH] eal/windows: use bundled getopt with MinGW
X-BeenThere: stable@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: patches for DPDK stable branches <stable.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/stable>,
 <mailto:stable-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/stable/>
List-Post: <mailto:stable@dpdk.org>
List-Help: <mailto:stable-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/stable>,
 <mailto:stable-request@dpdk.org?subject=subscribe>
Errors-To: stable-bounces@dpdk.org
Sender: "stable" <stable-bounces@dpdk.org>

Clang builds use getopt.c in librte_eal while MinGW provides
implementation as part of the toolchain. Statically linking librte_eal
to an application that depends on getopt results in undefined reference
errors with MinGW. There are no such errors with Clang, because with
Clang librte_eal actually defines getopt functions.

Use getopt.c in EAL with Clang and MinGW to get identical behavior.
Adjust code for MinGW. Incidentally, this removes a bug when free() is
called on uninitialized memory.

Fixes: 5e373e456e6 ("eal/windows: add getopt implementation")
Cc: stable@dpdk.org

Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
Reported-by: Khoa To <khot@microsoft.com>
Reported-by: Tal Shnaiderman <talshn@nvidia.com>
---

This patch probably supesedes http://patchwork.dpdk.org/patch/78574/.

 lib/librte_eal/meson.build      | 3 ---
 lib/librte_eal/windows/getopt.c | 9 ++++-----
 2 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/lib/librte_eal/meson.build b/lib/librte_eal/meson.build
index 8d492897d..7d6222e78 100644
--- a/lib/librte_eal/meson.build
+++ b/lib/librte_eal/meson.build
@@ -25,6 +25,3 @@ endif
 if cc.has_function('getentropy', prefix : '#include <unistd.h>')
 	cflags += '-DRTE_LIBEAL_USE_GETENTROPY'
 endif
-if cc.has_header('getopt.h')
-	cflags += ['-DHAVE_GETOPT_H', '-DHAVE_GETOPT', '-DHAVE_GETOPT_LONG']
-endif
diff --git a/lib/librte_eal/windows/getopt.c b/lib/librte_eal/windows/getopt.c
index a08f7c109..a1f51c6c2 100644
--- a/lib/librte_eal/windows/getopt.c
+++ b/lib/librte_eal/windows/getopt.c
@@ -242,7 +242,6 @@ getopt_internal(int nargc, char **nargv, const char *options,
 	char *oli;				/* option letter list index */
 	int optchar, short_too;
 	static int posixly_correct = -1;
-	char *buf;
 	size_t len;
 	int optreset = 0;
 
@@ -253,16 +252,16 @@ getopt_internal(int nargc, char **nargv, const char *options,
 	 * Disable GNU extensions if POSIXLY_CORRECT is set or options
 	 * string begins with a '+'.
 	 */
-	if (posixly_correct == -1)
-		posixly_correct = _dupenv_s(&buf, &len, "POSIXLY_CORRECT");
+	if (posixly_correct == -1) {
+		errno_t err = _wgetenv_s(&len, NULL, 0, L"POSIXLY_CORRECT");
+		posixly_correct = (err == 0) && (len > 0);
+	}
 	if (!posixly_correct || *options == '+')
 		flags &= ~FLAG_PERMUTE;
 	else if (*options == '-')
 		flags |= FLAG_ALLARGS;
 	if (*options == '+' || *options == '-')
 		options++;
-	if (!posixly_correct)
-		free(buf);
 	/*
 	 * reset if requested
 	 */
-- 
2.25.4