[or-cvs] r16302: When a struct ends with char a[1], the size of all earlier m (in tor/trunk: . src/or)

nickm at seul.org nickm at seul.org
Thu Jul 31 12:18:15 UTC 2008


Author: nickm
Date: 2008-07-31 08:18:14 -0400 (Thu, 31 Jul 2008)
New Revision: 16302

Modified:
   tor/trunk/ChangeLog
   tor/trunk/src/or/buffers.c
Log:
When a struct ends with char a[1], the size of all earlier members of the struct is _not_ sizeof(st)-1;  compilers add alignment.  Problem spotted by rovv.  Backport candidate.

Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog	2008-07-31 10:52:53 UTC (rev 16301)
+++ tor/trunk/ChangeLog	2008-07-31 12:18:14 UTC (rev 16302)
@@ -52,6 +52,8 @@
       HiddenServiceExcludeNodes as obsolete: they never worked properly,
       and nobody claims to be using them. Fixes bug 754. Bugfix on
       0.1.0.1-rc.  Patch from Christian Wilms.
+    - Fix a small alignment and memory-wasting bug on buffer chunks.  Spotted
+      by rovv.
 
   o Minor bugfixes (controller):
     - When closing an application-side connection because its circuit

Modified: tor/trunk/src/or/buffers.c
===================================================================
--- tor/trunk/src/or/buffers.c	2008-07-31 10:52:53 UTC (rev 16301)
+++ tor/trunk/src/or/buffers.c	2008-07-31 12:18:14 UTC (rev 16302)
@@ -69,12 +69,14 @@
                 * more than one byte long. */
 } chunk_t;
 
+#define CHUNK_HEADER_LEN STRUCT_OFFSET(chunk_t, mem[0])
+
 /** Return the number of bytes needed to allocate a chunk to hold
  * <b>memlen</b> bytes. */
-#define CHUNK_ALLOC_SIZE(memlen) (sizeof(chunk_t) + (memlen) - 1)
+#define CHUNK_ALLOC_SIZE(memlen) (CHUNK_HEADER_LEN + (memlen))
 /** Return the number of usable bytes in a chunk allocated with
  * malloc(<b>memlen</b>). */
-#define CHUNK_SIZE_WITH_ALLOC(memlen) ((memlen) - sizeof(chunk_t) + 1)
+#define CHUNK_SIZE_WITH_ALLOC(memlen) ((memlen) - CHUNK_HEADER_LEN)
 
 /** Return the next character in <b>chunk</b> onto which data can be appended.
  * If the chunk is full, this might be off the end of chunk->mem. */



More information about the tor-commits mailing list