From: Wolfgang Denk Date: Thu, 13 Mar 2014 19:41:24 +0100 Subject: [U-Boot] [PATCH V2 2/3] lib: uuid: add functions to generate UUID version 4 In-Reply-To: References: <72629ef58556732156fadf26a2a48be85704f224.1393600504.git.p.marczak@samsung.com> Message-ID: <20140313184124.773EE380EAD@gemini.denx.de> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de Dear Przemyslaw Marczak, In message 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 -- DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de As a general rule, the freedom of any people can be judged by the volume of their laughter.