bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fix misalignment


From: Paul Eggert
Subject: Re: Fix misalignment
Date: Mon, 23 Jul 2007 13:26:41 -0700
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux)

Thanks for catching that.  I guess I've never noticed it because my
C library's malloc/realloc always returns a buffer suitably aligned
for any type?  But you're right, that property isn't guaranteed by the
C standard.

The patch you suggested works with the current implementation of the
xalloc module, but I'd rather have a patch that works with any
implementation matching xalloc's specification.  An xalloc
implementation could theoretically just grow the buffer by 1 byte each
time around, which would cause the proposed sort.c code to loop.
Here's a revised patch to fix this potential problem.  It doesn't use
X2NREALLOC or X2REALLOC because neither macro applies to this
situation (we have a char * pointer that is also suitable to be a
struct line * pointer but is used as a char * pointer mostly, sigh).


* src/sort.c (fillbuf): Don't assume that malloc (S) etc. always
returns an object suitably aligned for any type, as the storage
might be suitably aligned only for an object whose size is S.
Problem reported by Andreas Schwab in
<http://lists.gnu.org/archive/html/bug-coreutils/2007-07/msg00158.html>.
diff --git a/src/sort.c b/src/sort.c
index 824dd0d..c3e87ad 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -1489,9 +1489,14 @@ fillbuf (struct buffer *buf, FILE *fp, char const *file)
          return true;
        }

-      /* The current input line is too long to fit in the buffer.
-        Double the buffer size and try again.  */
-      buf->buf = X2REALLOC (buf->buf, &buf->alloc);
+      {
+       /* The current input line is too long to fit in the buffer.
+          Double the buffer size and try again, keeping it properly
+          aligned.  */
+       size_t line_alloc = buf->alloc / sizeof (struct line);
+       buf->buf = x2nrealloc (buf->buf, &line_alloc, sizeof (struct line));
+       buf->alloc = line_alloc * sizeof (struct line);
+      }
     }
 }

M ChangeLog
M src/sort.c
Committed as 0bf8ec277d893b49dc914f63b05304b718a384f0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]