From: Toon Claes <toon@iotcl.com>
To: Jeff King <peff@peff.net>, Collin Funk <collin.funk1@gmail.com>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Phillip Wood <phillip.wood@dunelm.org.uk>,
Matthew John Cheetham <mjcheetham@outlook.com>,
Victoria Dye <vdye@github.com>, Derrick Stolee <stolee@gmail.com>,
Justin Tobler <jltobler@gmail.com>
Subject: Re: [PATCH] git-compat-util: make git_find_last_dir_sep return a const pointer
Date: Thu, 19 Mar 2026 12:06:43 +0100 [thread overview]
Message-ID: <87a4w42i4c.fsf@iotcl.com> (raw)
In-Reply-To: <20260204053218.GA942606@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
>> > Looking at strchr()'s declaration in string.h, which is defined like:
>> >
>> > # define strchr(S, C) \
>> > __glibc_const_generic (S, const char *, strchr (S, C))
>> >
>> > I think the answer is probably "yes". But it also doesn't quite solve
>> > our problem. That would give us type-checking of callers of our
>> > function, but we still have to convince the compiler not to complain
>> > about its implementation. For that we'd need to either cast away const
>> > manually, I guess.
>>
>> That macro depends on Generic selections from C11 [1]. I wasn't sure if
>> Git would like that, given it is conservative with other C features.
>
> We definitely can't rely on it everywhere. But if there is a solution
> that is conditionally compiled, and can kick in only when these extra
> warnings also kick in, that would be OK. Assuming the result is not too
> painful to look at, of course.
So the Git project would be okay to conditionally compile with Generic
selections if the compiler supports it? Seems to me this is the easiest
way forward to silence the errors for users who see these warnings (that
includes me).
> Probably the best path forward for most spots is just fixing the code to
> make it more obvious about its use of const.
Yeah, that's in any case a good idea. I think using Generic selections
should make it easier to locate those.
This is what I've done in an experiment in git-compat-util.h:
#ifndef find_last_dir_sep
static inline char *git_find_last_dir_sep_nonconst(char *path)
{
return strrchr(path, '/');
}
static inline const char *git_find_last_dir_sep_const(const char *path)
{
return strrchr(path, '/');
}
#if __STDC_VERSION__ >= 201112L || (defined(__GNUC__) && __GNUC__ >= 5)
#define find_last_dir_sep(path) \
_Generic((path), \
const char *: git_find_last_dir_sep_const((const char *)(path)), \
default: git_find_last_dir_sep_nonconst((char *)(path)))
#else
#define find_last_dir_sep(path) git_find_last_dir_sep_nonconst((char *)(path))
#endif
#endif
This leads to 28 places where the warning happens:
bloom.c:515:52: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
builtin/config.c:855:22: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
builtin/receive-pack.c:1045:19: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
builtin/receive-pack.c:1075:27: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
builtin/receive-pack.c:1098:19: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
builtin/rev-parse.c:278:22: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
builtin/rev-parse.c:339:21: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
builtin/rev-parse.c:343:28: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
builtin/rev-parse.c:347:28: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
convert.c:1189:24: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
convert.c:1212:32: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
convert.c:1223:29: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
dir.c:3526:15: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
http-push.c:1775:44: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
http.c:755:43: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
pager.c:121:28: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
pseudo-merge.c:647:16: warning: return discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
range-diff.c:109:50: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
refs/files-backend.c:2199:25: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
revision.c:2135:24: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
revision.c:2179:14: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
revision.c:2188:14: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
revision.c:2194:14: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
run-command.c:608:32: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
send-pack.c:184:19: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
send-pack.c:215:27: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
send-pack.c:240:19: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
transport-helper.c:803:19: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
I did not look into any of them, but I think you (Collin) have sent out
patches for various of these? But they _should_ managable to address?
--
Cheers,
Toon
next prev parent reply other threads:[~2026-03-19 11:06 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-03-20 4:39 ` Jeff King
2026-03-20 5:20 ` Collin Funk
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87a4w42i4c.fsf@iotcl.com \
--to=toon@iotcl.com \
--cc=Johannes.Schindelin@gmx.de \
--cc=collin.funk1@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jltobler@gmail.com \
--cc=mjcheetham@outlook.com \
--cc=peff@peff.net \
--cc=phillip.wood@dunelm.org.uk \
--cc=stolee@gmail.com \
--cc=vdye@github.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox