Browse Source

lib: uuid: alignment error in gen_rand_uuid()

Packed structures like struct uuid are not aligned. GCC 9.1 therefore
throws an error when trying to compile gen_rand_uuid().

lib/uuid.c: In function ‘gen_rand_uuid’:
lib/uuid.c:244:2: error: converting a packed ‘struct uuid’ pointer
(alignment 1) to a ‘unsigned int’ pointer (alignment 4) may result in
an unaligned pointer value [-Werror=address-of-packed-member]
  244 |  unsigned int *ptr = (unsigned int *)&uuid;
      |  ^~~~~~~~

Generate the uuid in a properly aligned buffer.

The byte order of a random number should not matter. Do not call
cpu_to_be32() to change the byte order.

Reported-by: Ramon Fried <rfried.dev@gmail.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Heinrich Schuchardt 4 years ago
parent
commit
a1b633df55
1 changed files with 7 additions and 7 deletions
  1. 7 7
      lib/uuid.c

+ 7 - 7
lib/uuid.c

@@ -240,25 +240,25 @@ void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
 #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
 void gen_rand_uuid(unsigned char *uuid_bin)
 {
-	struct uuid uuid;
-	unsigned int *ptr = (unsigned int *)&uuid;
+	u32 ptr[4];
+	struct uuid *uuid = (struct uuid *)ptr;
 	int i;
 
 	srand(get_ticks() + rand());
 
 	/* Set all fields randomly */
-	for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); i++)
-		*(ptr + i) = cpu_to_be32(rand());
+	for (i = 0; i < 4; i++)
+		ptr[i] = rand();
 
-	clrsetbits_be16(&uuid.time_hi_and_version,
+	clrsetbits_be16(&uuid->time_hi_and_version,
 			UUID_VERSION_MASK,
 			UUID_VERSION << UUID_VERSION_SHIFT);
 
-	clrsetbits_8(&uuid.clock_seq_hi_and_reserved,
+	clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
 		     UUID_VARIANT_MASK,
 		     UUID_VARIANT << UUID_VARIANT_SHIFT);
 
-	memcpy(uuid_bin, &uuid, sizeof(struct uuid));
+	memcpy(uuid_bin, uuid, 16);
 }
 
 /*