
Dear Przemyslaw Marczak,
In message cc0f558724a4d3ea3497b84601038f5f18f37a7b.1394037321.git.p.marczak@samsung.com you wrote:
This patch adds support to generate UUID (Universally Unique Identifier) in version 4 based on RFC4122, which is randomly.
...
+struct uuid {
- unsigned int time_low;
- unsigned short time_mid;
- unsigned short time_hi_and_version;
- unsigned char clock_seq_hi_and_reserved;
- unsigned char clock_seq_low;
- unsigned char node[6];
+};
This struct starts with an uint, so it requires alignment on a 32 bit boundary (i. e. an address that is a multiple of 4).
+void gen_rand_uuid(unsigned char *uuid_bin) +{
- struct uuid *uuid = (struct uuid *)uuid_bin;
Here you cast a pointer to the (unaligned) character buffer to a struct buffer, which requires alignment.
- unsigned int *ptr = (unsigned int *)uuid_bin;
- /* Set all fields randomly */
- for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); i++)
*(ptr + i) = rand();
This code is dangerous - if the size of the struct should not be a multiple of sizeof(uint), there would remain uninitialized data.
And note that it is likely that all these accesses are unaligned and might cause exceptions.
- /* Set V4 format */
- uuid->time_hi_and_version &= UUID_VERSION_CLEAR_BITS;
- uuid->time_hi_and_version |= UUID_VERSION << UUID_VERSION_SHIFT;
Potentially unaligned accesses.
Best regards,
Wolfgang Denk