Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/sdmmc Do not read past array end, found by gcc -O3.



details:   https://anonhg.NetBSD.org/src/rev/0badd4461a27
branches:  trunk
changeset: 778089:0badd4461a27
user:      bouyer <bouyer%NetBSD.org@localhost>
date:      Thu Mar 15 18:37:43 2012 +0000

description:
Do not read past array end, found by gcc -O3.
This could cause to HWRITE4() a bad value, but maybe last 2 bytes are
probably ignored by hardware anyway.

diffstat:

 sys/dev/sdmmc/sdhc.c |  26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)

diffs (54 lines):

diff -r a0d3f4ddf7ff -r 0badd4461a27 sys/dev/sdmmc/sdhc.c
--- a/sys/dev/sdmmc/sdhc.c      Thu Mar 15 18:35:20 2012 +0000
+++ b/sys/dev/sdmmc/sdhc.c      Thu Mar 15 18:37:43 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sdhc.c,v 1.12 2012/03/02 18:20:33 nonaka Exp $ */
+/*     $NetBSD: sdhc.c,v 1.13 2012/03/15 18:37:43 bouyer Exp $ */
 /*     $OpenBSD: sdhc.c,v 1.25 2009/01/13 19:44:20 grange Exp $        */
 
 /*
@@ -23,7 +23,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sdhc.c,v 1.12 2012/03/02 18:20:33 nonaka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sdhc.c,v 1.13 2012/03/15 18:37:43 bouyer Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_sdmmc.h"
@@ -441,8 +441,10 @@
                                uint32_t v = HREAD4(hp, i);
                                hp->regs[i + 0] = (v >> 0);
                                hp->regs[i + 1] = (v >> 8);
-                               hp->regs[i + 2] = (v >> 16);
-                               hp->regs[i + 3] = (v >> 24);
+                               if (i + 3 < sizeof hp->regs) {
+                                       hp->regs[i + 2] = (v >> 16);
+                                       hp->regs[i + 3] = (v >> 24);
+                               }
                        }
                } else {
                        for (i = 0; i < sizeof hp->regs; i++) {
@@ -466,11 +468,17 @@
                (void)sdhc_host_reset(hp);
                if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
                        for (i = 0; i < sizeof hp->regs; i += 4) {
-                               HWRITE4(hp, i,
-                                   (hp->regs[i + 0] << 0)
-                                   | (hp->regs[i + 1] << 8)
-                                   | (hp->regs[i + 2] << 16)
-                                   | (hp->regs[i + 3] << 24));
+                               if (i + 3 < sizeof hp->regs) {
+                                       HWRITE4(hp, i,
+                                           (hp->regs[i + 0] << 0)
+                                           | (hp->regs[i + 1] << 8)
+                                           | (hp->regs[i + 2] << 16)
+                                           | (hp->regs[i + 3] << 24));
+                               } else {
+                                       HWRITE4(hp, i,
+                                           (hp->regs[i + 0] << 0)
+                                           | (hp->regs[i + 1] << 8));
+                               }
                        }
                } else {
                        for (i = 0; i < sizeof hp->regs; i++) {



Home | Main Index | Thread Index | Old Index