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

Re: [PATCH] Fix git-pack-objects for 64-bit platforms

From
Junio C Hamano <junkio@cox.net>
Date
May 11, 2006, 18:52 UTC
Message-ID
<7v7j4swg0r.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<Pine.LNX.4.64.0605111054290.3866@g5.osdl.org>
Linus Torvalds <torvalds@osdl.org> writes:
Show 12 quoted lines
> On Thu, 11 May 2006, Dennis Stosberg wrote:
>> 
>> I am not sure whether an int cast or an int32_t cast is more
>> appropriate here.  An int is not guaranteed to be four bytes wide,
>> but I don't know of any modern platform where that's not the case.
>> On the other hand int32_t is not necessarily available before C99.
>> 
>> Any opinions?  I wonder why no one has hit this on x86_64...
>
> I think the "ntohl()" hides it. It loads a 64-bit value, but since x86-64 
> is little-endian, the low 32 bits are correct. The htonl() will then strip 
> the high bits and make it all be big-endian.
That sounds sensible.

Since I saw a patch that touches only one place, I thought I'd better point this out...

There are a few more places that knows about this ((char*)base_pointer + (entry_count * 24)) magic in our code.

$ git grep -n -e '24  *\*' -e '\*  *24' master -- '*.c'
master:pack-objects.c:159:		long hl = *((long *)(index + 24 * i));
	This is yours.

master:sha1_file.c:447: if (idx_size != 4*256 + nr * 24 + 20 + 20) master:sha1_file.c:1148: memcpy(sha1, (index + 24 * n + 4), 20); master:sha1_file.c:1162: int cmp = memcmp(index + 24 * mi + 4, sha1, 20);

	These three should be OK, I think.
master:sha1_file.c:1164:			e->offset = ntohl(*((int*)(index + 24 * mi)));
	This you might want to look at; I suspect it is what
	Linus suggests.

Also we _might_ have uglier magic that assumes the base_pointer to be a pointer to a 4-byte integer and uses offset of multiple of 6 instead of 24, although I do not think it is likely.

I have to leave the keyboard in a few minutes so I cannot verify nor fix them myself for the next 8 hours or so. Sorry.

Show 6 quoted lines
> And while I actually run a 64-bit big-endian machine myself (G5 ppc64), my 
> user space is all 32-bit by default, so it never showed up on linux-ppc64 
> either.
>
> Anyway, the correct type to use is "uint32_t" in this case. That's what 
> htonl() takes.

Is uint32_t guaranteed to be exactly 32-bit, or merely enough to hold 32-bit?

Previous: Linus TorvaldsNext: Linus Torvalds
Message 3 of 10 in “Fix git-pack-objects for 64-bit platforms”
  1. Fix git-pack-objects for 64-bit platformsDennis Stosberg, May 11, 2006
  2. Linus TorvaldsMay 11, 2006
  3. Junio C HamanoMay 11, 2006
  4. Linus TorvaldsMay 11, 2006
  5. Linus TorvaldsMay 11, 2006
  6. Junio C HamanoMay 13, 2006
  7. Ben CliffordMay 14, 2006
  8. include header to define uint32_t, necessary on Mac OS XBen Clifford, May 14, 2006
  9. Alex RiesenMay 20, 2006
  10. Linus TorvaldsMay 20, 2006

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.