public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] git-compat-util: make git_find_last_dir_sep return a const pointer
@ 2026-02-03  5:19 Collin Funk
  2026-02-03  6:25 ` Jeff King
  0 siblings, 1 reply; 7+ messages in thread
From: Collin Funk @ 2026-02-03  5:19 UTC (permalink / raw)
  To: git
  Cc: Collin Funk, Ævar Arnfjörð Bjarmason,
	Junio C Hamano, Johannes Schindelin, Phillip Wood,
	Matthew John Cheetham, Victoria Dye, Jeff King, Derrick Stolee

Unsure if this should be tagged [RFC], but this patch clears up lots
of warning spam with glibc 2.43 because of a change mentioned in the
commit message.

I plan to handle the rest of them and try to organize the changes by
subsystem, for lack of a better term. But I figured it was best to
submit just this one for review first.

-- 8< --

The recent glibc 2.43 release had the following change listed in its
NEWS file:

    For ISO C23, the functions bsearch, memchr, strchr, strpbrk, strrchr,
    strstr, wcschr, wcspbrk, wcsrchr, wcsstr and wmemchr that return
    pointers into their input arrays now have definitions as macros that
    return a pointer to a const-qualified type when the input argument is
    a pointer to a const-qualified type.

When compiling with GCC 15, which defaults to -std=gnu23, this causes
many warnings like this:

        CC abspath.o
    In file included from abspath.c:1:
    git-compat-util.h: In function ‘git_find_last_dir_sep’:
    git-compat-util.h:344:16: warning: return discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
      344 |         return strrchr(path, '/');
          |                ^~~~~~~

Most of the warnings are from git_find_last_dir_sep which calls strrchr
on a "const char *" but returns a "char *". This patch addresses them by
changing the return type to be const, since only one location needs the
qualifier casted away.

Signed-off-by: Collin Funk <collin.funk1@gmail.com>
---
 config.c          | 2 +-
 git-compat-util.h | 2 +-
 remote.c          | 2 +-
 scalar.c          | 8 ++++----
 strbuf.c          | 2 +-
 5 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/config.c b/config.c
index 7f6d53b473..156f2a24fa 100644
--- a/config.c
+++ b/config.c
@@ -160,7 +160,7 @@ static int handle_path_include(const struct key_value_info *kvi,
 	 * based on the including config file.
 	 */
 	if (!is_absolute_path(path)) {
-		char *slash;
+		const char *slash;
 
 		if (!kvi || kvi->origin_type != CONFIG_ORIGIN_FILE) {
 			ret = error(_("relative config includes must come from files"));
diff --git a/git-compat-util.h b/git-compat-util.h
index bebcf9f698..fb4251564a 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -339,7 +339,7 @@ static inline int is_path_owned_by_current_uid(const char *path,
 #endif
 
 #ifndef find_last_dir_sep
-static inline char *git_find_last_dir_sep(const char *path)
+static inline const char *git_find_last_dir_sep(const char *path)
 {
 	return strrchr(path, '/');
 }
diff --git a/remote.c b/remote.c
index b756ff6f15..8c1a0a0c15 100644
--- a/remote.c
+++ b/remote.c
@@ -2753,7 +2753,7 @@ void remote_state_clear(struct remote_state *remote_state)
  */
 static int chop_last_dir(char **remoteurl, int is_relative)
 {
-	char *rfind = find_last_dir_sep(*remoteurl);
+	char *rfind = (char *) find_last_dir_sep(*remoteurl);
 	if (rfind) {
 		*rfind = '\0';
 		return 0;
diff --git a/scalar.c b/scalar.c
index c9df9348ec..54a75ad971 100644
--- a/scalar.c
+++ b/scalar.c
@@ -393,7 +393,7 @@ static int delete_enlistment(struct strbuf *enlistment)
 {
 	struct strbuf parent = STRBUF_INIT;
 	size_t offset;
-	char *path_sep;
+	const char *path_sep;
 
 	if (unregister_dir())
 		return error(_("failed to unregister repository"));
@@ -479,11 +479,11 @@ static int cmd_clone(int argc, const char **argv)
 		/* Strip suffix `.git`, if any */
 		strbuf_strip_suffix(&buf, ".git");
 
-		enlistment = find_last_dir_sep(buf.buf);
-		if (!enlistment) {
+		const char *last = find_last_dir_sep(buf.buf);
+		if (!last) {
 			die(_("cannot deduce worktree name from '%s'"), url);
 		}
-		enlistment = xstrdup(enlistment + 1);
+		enlistment = xstrdup(last + 1);
 	} else {
 		usage_msg_opt(_("You must specify a repository to clone."),
 			      clone_usage, clone_options);
diff --git a/strbuf.c b/strbuf.c
index 59678bf5b0..3939863cf3 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1119,6 +1119,6 @@ void strbuf_stripspace(struct strbuf *sb, const char *comment_prefix)
 
 void strbuf_strip_file_from_path(struct strbuf *sb)
 {
-	char *path_sep = find_last_dir_sep(sb->buf);
+	const char *path_sep = find_last_dir_sep(sb->buf);
 	strbuf_setlen(sb, path_sep ? path_sep - sb->buf + 1 : 0);
 }
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-03-20  5:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-03  5:19 [PATCH] git-compat-util: make git_find_last_dir_sep return a const pointer Collin Funk
2026-02-03  6:25 ` Jeff King
2026-02-04  3:15   ` Collin Funk
2026-02-04  5:32     ` Jeff King
2026-03-19 11:06       ` Toon Claes
2026-03-20  4:39         ` Jeff King
2026-03-20  5:20           ` Collin Funk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox