public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
* Bad alignment in locale-archive
@ 2002-10-22  7:00 Andreas Schwab
  2002-10-22  7:29 ` Jakub Jelinek
  0 siblings, 1 reply; 3+ messages in thread
From: Andreas Schwab @ 2002-10-22  7:00 UTC (permalink / raw)
  To: libc-hacker

This fixes invalid alignments in the locale-archive file.

Andreas.

2002-10-21  Andreas Schwab  <schwab@suse.de>

	* locale/programs/locarchive.c (create_archive): Properly align
	offsets.
	(enlarge_archive): Likewise.

--- locale/programs/locarchive.c.~1.16.~	2002-10-18 11:14:16.000000000 +0200
+++ locale/programs/locarchive.c	2002-10-21 13:28:27.000000000 +0200
@@ -72,6 +72,9 @@ static const char *locnames[] =
 #define INITIAL_NUM_SUMS	2000
 
 
+#define ALIGN(offset, alignment) \
+  (((offset) + (alignment) - 1) & -(alignment))
+
 static void
 create_archive (const char *archivefname, struct locarhandle *ah)
 {
@@ -90,7 +93,8 @@ create_archive (const char *archivefname
 
   /* Create the initial content of the archive.  */
   head.magic = AR_MAGIC;
-  head.namehash_offset = sizeof (struct locarhead);
+  head.namehash_offset = ALIGN (sizeof (struct locarhead),
+				__alignof__ (struct namehashent));
   head.namehash_used = 0;
   head.namehash_size = next_prime (INITIAL_NUM_NAMES);
 
@@ -99,12 +103,15 @@ create_archive (const char *archivefname
   head.string_used = 0;
   head.string_size = INITIAL_SIZE_STRINGS;
 
-  head.locrectab_offset = head.string_offset + head.string_size;
+  head.locrectab_offset = ALIGN (head.string_offset + head.string_size,
+				 __alignof__ (struct locrecent));
   head.locrectab_used = 0;
   head.locrectab_size = INITIAL_NUM_LOCREC;
 
-  head.sumhash_offset = (head.locrectab_offset
-			 + head.locrectab_size * sizeof (struct locrecent));
+  head.sumhash_offset = ALIGN (head.locrectab_offset
+			       + (head.locrectab_size
+				  * sizeof (struct locrecent)),
+			       __alignof__ (struct sumhashent));
   head.sumhash_used = 0;
   head.sumhash_size = next_prime (INITIAL_NUM_SUMS);
 
@@ -274,13 +281,16 @@ enlarge_archive (struct locarhandle *ah,
 			      * sizeof (struct namehashent)));
   newhead.string_size = MAX (2 * newhead.string_used, newhead.string_size);
 
-  newhead.locrectab_offset = newhead.string_offset + newhead.string_size;
+  newhead.locrectab_offset = ALIGN (newhead.string_offset
+				    + newhead.string_size,
+				    __alignof__ (struct locrecent));
   newhead.locrectab_size = MAX (2 * newhead.locrectab_used,
 				newhead.locrectab_size);
 
-  newhead.sumhash_offset = (newhead.locrectab_offset
-			    + (newhead.locrectab_size
-			       * sizeof (struct locrecent)));
+  newhead.sumhash_offset = ALIGN (newhead.locrectab_offset
+				  + (newhead.locrectab_size
+				     * sizeof (struct locrecent)),
+				  __alignof__ (struct sumhashent));
   newhead.sumhash_size = MAX (next_prime (2 * newhead.sumhash_used),
 			      newhead.sumhash_size);
 

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Bad alignment in locale-archive
  2002-10-22  7:00 Bad alignment in locale-archive Andreas Schwab
@ 2002-10-22  7:29 ` Jakub Jelinek
  2002-10-22  9:08   ` Roland McGrath
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Jelinek @ 2002-10-22  7:29 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: libc-hacker

On Mon, Oct 21, 2002 at 04:21:39PM +0200, Andreas Schwab wrote:
> This fixes invalid alignments in the locale-archive file.

I don't think we should use __alignof__, what if it is different among
arches with the same endianity. locale-archive format should be only
endianess dependent, not anything else.
Furthermore, all but one alignment is unnecessary, and in that one case
(enlarge_archive's
newhead.locrectab_offset = newhead.string_offset + newhead.string_size;
) it is IMHO better to make sure string_size is always multiple of 4, like:

2002-10-22  Jakub Jelinek  <jakub@redhat.com>

	* locale/programs/locarchive.c (enlarge_archive): Make sure
	string_size is always multiple of 4.
	Reported by Andreas Schwab <schwab@suse.de>.

--- libc/locale/programs/locarchive.c.jj	2002-10-22 15:34:37.000000000 +0200
+++ libc/locale/programs/locarchive.c	2002-10-22 16:32:38.000000000 +0200
@@ -272,7 +272,8 @@ enlarge_archive (struct locarhandle *ah,
   newhead.string_offset = (newhead.namehash_offset
 			   + (newhead.namehash_size
 			      * sizeof (struct namehashent)));
-  newhead.string_size = MAX (2 * newhead.string_used, newhead.string_size);
+  newhead.string_size = MAX ((2 * newhead.string_used + 3) & -4,
+			     newhead.string_size);
 
   newhead.locrectab_offset = newhead.string_offset + newhead.string_size;
   newhead.locrectab_size = MAX (2 * newhead.locrectab_used,

	Jakub

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Bad alignment in locale-archive
  2002-10-22  7:29 ` Jakub Jelinek
@ 2002-10-22  9:08   ` Roland McGrath
  0 siblings, 0 replies; 3+ messages in thread
From: Roland McGrath @ 2002-10-22  9:08 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Andreas Schwab, libc-hacker

I put your version of the fix in, with a comment.  (Please add comments in
the code for changes such as this.  If it was nonobvious enough to have
missed when writing it the first time, it's nonbovious enough to warrant an
explanatory comment when reading the code.)

I agree that the alignment should always be 4.  No extant architecture that
I know of needs more for these types (all structs containing uint32_t
members).  OTOH, if __alignof__ ever reports more for some architecture and
we use 4, then we have a problem.  From what we know now the files should
be portable modulo byte order, but no current configuration/installation
plan actually assumes they are (except maybe between 32/64 modes of bi-arch
platforms).

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-10-22 16:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-22  7:00 Bad alignment in locale-archive Andreas Schwab
2002-10-22  7:29 ` Jakub Jelinek
2002-10-22  9:08   ` Roland McGrath

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).