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

[PATCH] commit-slab: sizeof() the right type in xrealloc

From
Thomas Rast <tr@thomasrast.ch>
Date
Dec 1, 2013, 20:41 UTC
Message-ID
<87siucpalo.fsf_-_@linux-1gf2.Speedport_W723_V_Typ_A_1_00_098>
In-Reply-To
<CACsJy8CnxvPRwC_xXgBNF_JEmkpfnk=faMwOWtkJOFU-18aHgA@mail.gmail.com>

When allocating the slab, the code accidentally computed the array size from s->slab (an elemtype**). The slab is an array of elemtype*, however, so we should take the size of *s->slab.

Noticed-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Thomas Rast <tr@thomasrast.ch>
---
[I hope this comes through clean.  git-send-email is currently broken
for me, and I'm still investigating, so I have to kludge around it.]

I browsed around for a while, and couldn't find out whether any architecture actually has any hope of running git (i.e. is at least mostly POSIX conformant) but still violates the assumption that all pointers[*] are the same size.

The comp.lang.c FAQ has some interesting examples of wildly different pointer representations at:

  http://c-faq.com/null/machexamp.html

Consider the cases mentioned there where void* and char* have different representations from, e.g., int*. Then if elemtype is char, the slab will be too small before this patch.

But I have no idea if any of those are POSIXish.

One interesting, though orthogonal, tidbit is that POSIX actually requires _function_ pointers to have the same representation as void*. From the specification of dlsym(), which depends on this to be able to return function pointers:

RATIONALE
  [...] Indeed, the ISO C standard does not require that an object of
  type void * can hold a pointer to a function. Implementations
  supporting the XSI extension, however, do require that an object of
  type void * can hold a pointer to a function.
Thank god for POSIX.  So much craziness averted.

[*] Note that C++ method pointers are yet another story. This only applies to the kinds of pointers that C supports.

 commit-slab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/commit-slab.h b/commit-slab.h
index d068e2d..cc114b5 100644
--- a/commit-slab.h
+++ b/commit-slab.h
@@ -91,7 +91,7 @@ struct slabname {							\
 	if (s->slab_count <= nth_slab) {				\
 		int i;							\
 		s->slab = xrealloc(s->slab,				\
-				   (nth_slab + 1) * sizeof(s->slab));	\
+				   (nth_slab + 1) * sizeof(*s->slab));	\
 		stat_ ##slabname## realloc++;				\
 		for (i = s->slab_count; i <= nth_slab; i++)		\
 			s->slab[i] = NULL;				\
-- 
1.8.5.427.g6d3141d
Previous: Duy NguyenNext: Jeff King
Message 17 of 18 in “commit-slab cleanups”
  1. 0/2 commit-slab cleanupsThomas Rast, Nov 25, 2013
  2. 1/2 commit-slab: document clear_$slabname()Thomas Rast, Nov 25, 2013
  3. Jonathan NiederNov 25, 2013
  4. Thomas RastNov 29, 2013
  5. Junio C HamanoNov 25, 2013
  6. Eric SunshineNov 27, 2013
  7. 2/2 commit-slab: declare functions "static inline"Thomas Rast, Nov 25, 2013
  8. Junio C HamanoNov 25, 2013
  9. Thomas RastNov 25, 2013
  10. commit-slab: declare functions "static inline"Thomas Rast, Nov 25, 2013
  11. Jonathan NiederNov 25, 2013
  12. Thomas RastNov 25, 2013
  13. Jonathan NiederNov 25, 2013
  14. Junio C HamanoNov 25, 2013
  15. Junio C HamanoNov 25, 2013
  16. Duy NguyenDec 1, 2013
  17. commit-slab: sizeof() the right type in xreallocThomas Rast, Dec 1, 2013
  18. Jeff KingDec 2, 2013

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.