git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] git: remove unneeded casts

From
Jeff King <peff@peff.net>
Date
Dec 17, 2022, 13:55 UTC
Message-ID
<Y53KNbX7SO+FOoKf@coredump.intra.peff.net>
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
Previous: Ævar Arnfjörð BjarmasonNext: Phillip Wood
Message 7 of 8 in “git: remove unneeded casts”
  1. git: remove unneeded castsRose via GitGitGadget, Dec 14, 2022
  2. Jeff KingDec 14, 2022
  3. Junio C HamanoDec 14, 2022
  4. Jeff KingDec 17, 2022
  5. Junio C HamanoDec 19, 2022
  6. Ævar Arnfjörð BjarmasonDec 15, 2022
  7. Jeff KingDec 17, 2022
  8. Phillip WoodDec 15, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.