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

Re: [PATCH] wrapper: avoid UB in macOS

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 19, 2019, 14:40 UTC
Message-ID
<xmqqk1dhr63n.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20190616184003.17236-1-carenas@gmail.com>
Carlo Marcelo Arenas Belón  <carenas@gmail.com> writes:
Show 6 quoted lines
> 0620b39b3b ("compat: add a mkstemps() compatibility function", 2009-05-31)
> included a function based on code from libiberty which would result in
> undefined behaviour in platforms where timeval's tv_usec is a 32-bit signed
> type as shown by:
>
> wrapper.c:505:31: runtime error: left shift of 594546 by 16 places cannot be represented in type '__darwin_suseconds_t' (aka 'int')

I had to scratch my head wondering what an UB was (spell it out as "undefined behaviour" if that is what you wanted to say).

Show 5 quoted lines
> interestingly the version of this code from gcc never had this bug and the
> code had a cast that would had prevented the issue (at least in 64-bit
> platforms) but was misapplied.
>
> change the cast to uint64_t so it also works in 32-bit platforms.

Yup, regardless of the platform, that's the "right" type to use, certainly more correct than "size_t", as value is declared to be u64 in this function.

Show 6 quoted lines
>  	gettimeofday(&tv, NULL);
> -	value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
> +	value = ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
>  	filename_template = &pattern[len - 6 - suffix_len];
>  	for (count = 0; count < TMP_MAX; ++count) {
>  		uint64_t v = value;
Thanks.
Previous: Carlo Marcelo Arenas Belón
Message 2 of 2 in “wrapper: avoid UB in macOS”
  1. wrapper: avoid UB in macOSCarlo Marcelo Arenas Belón, Jun 16, 2019
  2. Junio C HamanoJun 19, 2019

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.