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

Re: [PATCH 1/6] Fix some "printf format" warnings.

From
Junio C Hamano <junkio@cox.net>
Date
Mar 12, 2007, 19:40 UTC
Message-ID
<7vfy8as129.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<45F55DC5.8060702@fs.ei.tum.de>
Simon 'corecode' Schubert <corecode@fs.ei.tum.de> writes:
Show 21 quoted lines
> Ramsay Jones wrote:
>>>> -        printf("%s%06o %s %d\t",
>>>> +        printf("%s%06lo %s %d\t",
>>>>                 tag,
>>>>                 ntohl(ce->ce_mode),
>>> I think we should do this instead:
>>>
>>>     printf("%s%06o %s %d\t", tag, (unsigned) ntohl(ce->ce_mode), ...
>>
>> Oops, yes you are right.
>> (cygwin typedef's uint32_t as unsigned long.)
>>
>> However, I would hate to add all those casts! Casts are not always
>> evil, but should be avoided if possible. Having said that, I don't
>> see another solution ...
>
> shouldn't it be something like this?
>
> printf("%s%06"PRIo32" %s %d\t", tag, ntohl(ce->ce_mode), ...)
>
> that's the correct and allegedly portable way I guess.

Yes, except that that is only portable across platforms with inttypes.h, and we would need a compatibility definition in git-compat-util.h next to PRIuMAX definition we already have.

But I wonder if this is really worth it.  The thing is,
    printf("%s%06o %s %d\t", tag, (unsigned) ntohl(ce->ce_mode), ...

is perfectly readable for even old timers about git, as long as they know traditional C and what ntohl() is. And ce->ce_mode even fits in 16-bit, so while we are _not_ supporting platforms whose unsigned int is 16-bit, the above cast is not losing any useful precision either.

Previous: Simon 'corecode' SchubertNext: Ramsay Jones
Message 5 of 8 in “Fix some "printf format" warnings.”
  1. 1/6 Fix some "printf format" warnings.Ramsay Jones, Mar 3, 2007
  2. Junio C HamanoMar 3, 2007
  3. Ramsay JonesMar 4, 2007
  4. Simon 'corecode' SchubertMar 12, 2007
  5. Junio C HamanoMar 12, 2007
  6. Ramsay JonesMar 14, 2007
  7. Junio C HamanoMar 14, 2007
  8. Ramsay JonesMar 13, 2007

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.