in libobjc/archive.c, objc_write_type() is a problem when writing a type with 
more than one character. 
The reason in short is the use of the ROUND-macro, defined in that file too. 
the ROUND-macro returns a 
new value that is rounded up to an alignment, but when its called the returned 
value is added to the 
alignment value. A program that shows the behaviour follows. The bug is in all 
gccs from at least 3.3 up 
to 4.0.1 Appended is also a patch to fix libobjc/archive.c since the 
file-attach field in the bug-report form 
seems to be gone. The diff was created with gcc-3.3.6.

Greetings - Rasmus

#define _GNU_SOURCE
#include <objc/typedstream.h>
#include <stdio.h>
#include <stdlib.h>

int main (int ac, char **av)
{
  FILE *f; TypedStream *ts;
  struct T { int a, b; } x = { 1, 2 };
  f = fopen ("foo", "w"); ts = objc_open_typed_stream (f, OBJC_WRITEONLY);
  objc_write_type (ts, @encode(struct T), &x);
  objc_close_typed_stream (ts); fclose (f);
  f = fopen ("foo", "r"); ts = objc_open_typed_stream (f, OBJC_READONLY);
  struct T y;
  objc_read_type (ts, @encode(struct T), &y);
  printf ("a = %d, b = %d\n", y.a, y.b); /* should print a=1, b=2 */
  objc_close_typed_stream (ts); fclose (f);
}

--- gcc-3.3.6-org/libobjc/archive.c     2005-07-28 10:49:33.368821348 +0200
+++ gcc-3.3.6/libobjc/archive.c 2002-07-02 21:41:56.000000000 +0200
@@ -1058,7 +1058,7 @@
       while (*type != _C_STRUCT_E)
        {
          align = objc_alignof_type (type);       /* padd to alignment */
-         acc_size = ROUND (acc_size, align);
+         acc_size += ROUND (acc_size, align);
          objc_write_type (stream, type, ((char *) data) + acc_size);
          acc_size += objc_sizeof_type (type);   /* add component size */
          type = objc_skip_typespec (type);      /* skip component */
@@ -1154,7 +1154,7 @@
       while (*type != _C_STRUCT_E)
        {
          align = objc_alignof_type (type);       /* padd to alignment */
-         acc_size = ROUND (acc_size, align);
+         acc_size += ROUND (acc_size, align);
          objc_read_type (stream, type, ((char*)data)+acc_size);
          acc_size += objc_sizeof_type (type);   /* add component size */
          type = objc_skip_typespec (type);      /* skip component */

-- 
           Summary: alignment bug in libobjc/archive.c
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libobjc
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rassahah at neofonie dot de
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23108

Reply via email to