From: Wolfgang Denk Date: Wed, 08 Sep 2010 01:33:38 +0200 Subject: [U-Boot] [PATCH v2] display_buffer: fix misaligned buffer In-Reply-To: <1283243000-25427-1-git-send-email-u-boot@emk-elektronik.de> References: <1283243000-25427-1-git-send-email-u-boot@emk-elektronik.de> Message-ID: <20100907233338.CE0A31506AA@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 Reinhard Meyer, In message <1283243000-25427-1-git-send-email-u-boot@emk-elektronik.de> you wrote: > use a union to cause necessary alignment per architecture > > Signed-off-by: Reinhard Meyer > --- > lib/display_options.c | 24 +++++++++++++----------- > 1 files changed, 13 insertions(+), 11 deletions(-) > > diff --git a/lib/display_options.c b/lib/display_options.c > index 20319e6..4f2ad69 100644 > --- a/lib/display_options.c > +++ b/lib/display_options.c > @@ -101,10 +101,12 @@ void print_size(unsigned long long size, const char *s) > #define DEFAULT_LINE_LENGTH_BYTES (16) > int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen) > { > - uint8_t linebuf[MAX_LINE_LENGTH_BYTES + 1]; > - uint32_t *uip = (void*)linebuf; > - uint16_t *usp = (void*)linebuf; > - uint8_t *ucp = (void*)linebuf; > + /* linebuf as a union causes proper alignment */ > + union linebuf { > + uint32_t ui[MAX_LINE_LENGTH_BYTES/4 + 1]; > + uint16_t us[MAX_LINE_LENGTH_BYTES/2 + 1]; > + uint8_t uc[MAX_LINE_LENGTH_BYTES/1 + 1]; Please replace the magic numbers 4, 2 and 1 by respective sizeof(). > int i; > > if (linelen*width > MAX_LINE_LENGTH_BYTES) > @@ -123,21 +125,21 @@ int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen) > for (i = 0; i < linelen; i++) { > uint32_t x; > if (width == 4) > - x = uip[i] = *(volatile uint32_t *)data; > + x = lb.ui[i] = *(volatile uint32_t *)data; > else if (width == 2) > - x = usp[i] = *(volatile uint16_t *)data; > + x = lb.us[i] = *(volatile uint16_t *)data; > else > - x = ucp[i] = *(volatile uint8_t *)data; > + x = lb.uc[i] = *(volatile uint8_t *)data; > printf(" %0*x", width * 2, x); > data += width; > } > > /* Print data in ASCII characters */ > for (i = 0; i < linelen * width; i++) > - if (!isprint(ucp[i]) || ucp[i] >= 0x80) > - ucp[i] = '.'; > - ucp[i] = '\0'; > - printf(" %s\n", ucp); > + if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80) > + lb.uc[i] = '.'; Please fix this old bug: the multi-line statement needs braces. 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 at denx.de It is surely a great calamity for a human being to have no ob- sessions. - Robert Bly