From: Jeff King <peff@peff.net>
To: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Cc: Rose via GitGitGadget <gitgitgadget@gmail.com>,
git@vger.kernel.org, Seija Kijin <doremylover123@gmail.com>
Subject: Re: [PATCH] git: remove unneeded casts
Date: Sat, 17 Dec 2022 08:55:01 -0500 [thread overview]
Message-ID: <Y53KNbX7SO+FOoKf@coredump.intra.peff.net> (raw)
In-Reply-To: <221215.86edt12bte.gmgdl@evledraar.gmail.com>
On Thu, Dec 15, 2022 at 11:07:24AM +0100, Ævar Arnfjörð Bjarmason wrote:
> Excluding compat/ the diff is the below. Some of it's broken, but some
> of it suggests things that are worth picking up, e.g. the return value
> of xmalloc() being cast (a C++-ism), "(char *)NULL" (do we ever need to
> cast NULL?)
Yeah, getting rid of explicit casts to/from void (as in xmalloc()) is a
good thing, IMHO.
Casting NULL is trickier. In normal assignment, no, it should never be
needed. When passed to a variadic function (which covers all the cases
in your patch below), the compiler needs to know that it's a pointer,
and not the integer 0. So of the two allowed null pointer constants,
only one is guaranteed to work:
execl(foo, 0); /* bad! there's nothing to say it's a pointer */
execl(foo, (char *)0); /* ok */
though whether it matters in practice depends on your ABI, I think.
But what about NULL? My copy of C99 (7.17.3) says it "expands to an
implementation-defined null pointer constant". So it could be either of
those.
And that is backed up by looking at "git log -S')NULL'", which yields
5d314759d7 (Cast execl*() NULL sentinels to (char *), 2010-07-24).
That said, I think we have lots of bare NULLs passed to variadic
functions. Any function with LAST_ARG_MUST_BE_NULL will have a NULL in
each of its callers, and we do not bother casting most of them. So I
think this is one of those "technically could violate the standard, but
OK in practice" things.
-Peff
next prev parent reply other threads:[~2022-12-17 13:55 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-14 15:35 [PATCH] git: remove unneeded casts Rose via GitGitGadget
2022-12-14 21:16 ` Jeff King
2022-12-14 23:49 ` Junio C Hamano
2022-12-17 13:32 ` Jeff King
2022-12-19 1:07 ` Junio C Hamano
2022-12-15 10:07 ` Ævar Arnfjörð Bjarmason
2022-12-17 13:55 ` Jeff King [this message]
2022-12-15 10:18 ` Phillip Wood
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=Y53KNbX7SO+FOoKf@coredump.intra.peff.net \
--to=peff@peff.net \
--cc=avarab@gmail.com \
--cc=doremylover123@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.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;
as well as URLs for NNTP newsgroup(s).