From patchwork Thu May 30 12:50:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13680361 Received: from fhigh5-smtp.messagingengine.com (fhigh5-smtp.messagingengine.com [103.168.172.156]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 83A5E1474C3 for ; Thu, 30 May 2024 12:50:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.156 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717073455; cv=none; b=bv9jmI+kG8RivL+4jj+gco9AemcRoXb7kJrqlV8GefZSEXT6ixdlEBdzsm1TIuYPHRGoBwi71JZnpSaA3Y+fUANtLv2rEXW5HluD9Qn4iuBdXAPK3NT6bZa8/ReCF/qdsYtbOQ3xhHdy1Ved+SgP1I/WAXdoOK20FfjQ1LKye64= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717073455; c=relaxed/simple; bh=axUNjoo5gPunrXbQrwsVzAOZ8Ef0ee+uBaJz/7QmobA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=eMhhILw4qybeKg0l/Mjj0Pu9Iokdga1MgOSpDZDIImNFE2OSE+YiaiBW2hbR9pa7UdIsHWYPmPE78SX8KHanV2Ti0qlwTlx27ftlxvV+AdL+96buLLnLkQaPxqSyOlUM+AwXZOnwiZntBJtBwSE6S+Z3otcCFtcwkOWj3EiYQlk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=F37YBxJ9; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=KMS4Aujk; arc=none smtp.client-ip=103.168.172.156 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="F37YBxJ9"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="KMS4Aujk" Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfhigh.nyi.internal (Postfix) with ESMTP id CDA5F1140150; Thu, 30 May 2024 08:50:52 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Thu, 30 May 2024 08:50:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1717073452; x=1717159852; bh=iKNvqJ6y10 siDBXcSXo0C4hKFXzFEmSeb/UMLZyHeCc=; b=F37YBxJ9rVYf2+yZzlxA6+MQF7 yAI819OmlxnC5H/0nay3IH/EAgiLFahGM78IRPwp4gUXxK3RcpZUz1zzDxNNi7+L 1PBg+nAAklbgxslTCLPVg+pHBT6qmXCe6ARjSKotHf1ajXr50Bb7OTh52wXIvPBD 6rzeQAsXDGro+JLFKX83YIKp2p+zNd3YRs7zkSRqmT7tEsPazwUk4k3lxc6alaww QhzB15x5KG+HtQu42Pz7v0AcNBoJUvOp8VSnsq1dScRkbTyY0wVKS//b/cC6Q0VR nSTPVOuihJSWNdE7e3jO19fU7T7+VF7ls1LLb43tmiIehsJGo3D1onH9GsIQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1717073452; x=1717159852; bh=iKNvqJ6y10siDBXcSXo0C4hKFXzF EmSeb/UMLZyHeCc=; b=KMS4AujkURozXc3nxIzRlct/EaEynhEcEiAaYo2Udp2V 3x30u1Sgh/Ktq0K4J51koYvp4i0xnNwrsyTw5RNj4P7FK/cJH1ilNiATLclMp2QI qWTEthr+Nzr6b9oc6EXk3ebbGlyNMR9tqPkoZ7shVV7NkNUFALS7LNQWazbbj1SD /nhxryJx2/30OTK8OK6ivcdaFevRBj6/Fn3dw5c+aAVQoY7sZKRPfT5nyiTx+LJs 3bhUAb92srn0mLw5ZYIY/x7XHvqpidaizkRwENgE4TMtyU8KYAYWh6iaWaToiVgZ 3hunUaY4RPmA+udICUJbi8dFYQ72kZUg9Bc8aVMldA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdekgedgheehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvvefukfhfgggtuggjsehgtd erredttddvnecuhfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshes phhkshdrihhmqeenucggtffrrghtthgvrhhnpeeukedtvedtffevleejtefgheehieegke eluddvfeefgeehgfeltddtheejleffteenucevlhhushhtvghrufhiiigvpedtnecurfgr rhgrmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 30 May 2024 08:50:51 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id d36737b2 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 30 May 2024 12:50:35 +0000 (UTC) Date: Thu, 30 May 2024 14:50:49 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Jeff King , Junio C Hamano Subject: [PATCH v2 04/19] compat/win32: fix const-correctness with string constants Message-ID: <961b3357d57296cb236c3832e1823ed44320a44c.1717073346.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Adjust various places in our Win32 compatibility layer where we are not assigning string constants to `const char *` variables. Signed-off-by: Patrick Steinhardt --- compat/basename.c | 15 +++++++++++++-- compat/mingw.c | 25 +++++++++++++------------ compat/winansi.c | 2 +- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/compat/basename.c b/compat/basename.c index 96bd9533b4..c3c9d65fac 100644 --- a/compat/basename.c +++ b/compat/basename.c @@ -1,6 +1,13 @@ #include "../git-compat-util.h" #include "../strbuf.h" +/* + * Both basename(3P) and dirname(3P) are mis-specified because they return a + * non-constant pointer even though it is specified that they may return a + * pointer to internal memory. This variable here is a result of that. + */ +static char current_directory[] = "."; + /* Adapted from libiberty's basename.c. */ char *gitbasename (char *path) { @@ -10,7 +17,7 @@ char *gitbasename (char *path) skip_dos_drive_prefix(&path); if (!path || !*path) - return "."; + return current_directory; for (base = path; *path; path++) { if (!is_dir_sep(*path)) @@ -33,8 +40,12 @@ char *gitdirname(char *path) char *p = path, *slash = NULL, c; int dos_drive_prefix; + /* + * Same here, dirname(3P) is broken because it returns a non-constant + * pointer that may point to internal memory. + */ if (!p) - return "."; + return current_directory; if ((dos_drive_prefix = skip_dos_drive_prefix(&p)) && !*p) goto dot; diff --git a/compat/mingw.c b/compat/mingw.c index 6b06ea540f..60f0986f76 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2257,6 +2257,7 @@ struct passwd *getpwuid(int uid) { static unsigned initialized; static char user_name[100]; + static char unknown[] = "unknown"; static struct passwd *p; wchar_t buf[100]; DWORD len; @@ -2279,7 +2280,7 @@ struct passwd *getpwuid(int uid) p->pw_name = user_name; p->pw_gecos = get_extended_user_info(NameDisplay); if (!p->pw_gecos) - p->pw_gecos = "unknown"; + p->pw_gecos = unknown; p->pw_dir = NULL; initialized = 1; @@ -2800,16 +2801,16 @@ int is_path_owned_by_current_sid(const char *path, struct strbuf *report) strbuf_addf(report, "'%s' is on a file system that does " "not record ownership\n", path); } else if (report) { - LPSTR str1, str2, str3, str4, to_free1 = NULL, - to_free3 = NULL, to_local_free2 = NULL, - to_local_free4 = NULL; + PCSTR str1, str2, str3, str4; + LPSTR to_free1 = NULL, to_free3 = NULL, + to_local_free2 = NULL, to_local_free4 = NULL; - if (user_sid_to_user_name(sid, &str1)) - to_free1 = str1; + if (user_sid_to_user_name(sid, &to_free1)) + str1 = to_free1; else str1 = "(inconvertible)"; - if (ConvertSidToStringSidA(sid, &str2)) - to_local_free2 = str2; + if (ConvertSidToStringSidA(sid, &to_local_free2)) + str2 = to_local_free2; else str2 = "(inconvertible)"; @@ -2822,13 +2823,13 @@ int is_path_owned_by_current_sid(const char *path, struct strbuf *report) str4 = "(invalid)"; } else { if (user_sid_to_user_name(current_user_sid, - &str3)) - to_free3 = str3; + &to_free3)) + str3 = to_free3; else str3 = "(inconvertible)"; if (ConvertSidToStringSidA(current_user_sid, - &str4)) - to_local_free4 = str4; + &to_local_free4)) + str4 = to_local_free4; else str4 = "(inconvertible)"; } diff --git a/compat/winansi.c b/compat/winansi.c index f83610f684..575813bde8 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -139,7 +139,7 @@ static void write_console(unsigned char *str, size_t len) /* convert utf-8 to utf-16 */ int wlen = xutftowcsn(wbuf, (char*) str, ARRAY_SIZE(wbuf), len); if (wlen < 0) { - wchar_t *err = L"[invalid]"; + const wchar_t *err = L"[invalid]"; WriteConsoleW(console, err, wcslen(err), &dummy, NULL); return; }