Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/ic cleanup debugging printfs to avoid overflow



details:   https://anonhg.NetBSD.org/src/rev/4586e3bebf11
branches:  trunk
changeset: 328137:4586e3bebf11
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Mar 27 18:28:26 2014 +0000

description:
cleanup debugging printfs to avoid overflow

diffstat:

 sys/dev/ic/aic79xx.c   |  35 ++++++++++++++++++++---------------
 sys/dev/ic/aic7xxx.c   |  45 ++++++++++++++++++++++++++-------------------
 sys/dev/ic/mpt_debug.c |  36 +++++++++++++++++++++---------------
 3 files changed, 67 insertions(+), 49 deletions(-)

diffs (281 lines):

diff -r 7e1696a1d6cb -r 4586e3bebf11 sys/dev/ic/aic79xx.c
--- a/sys/dev/ic/aic79xx.c      Thu Mar 27 18:27:34 2014 +0000
+++ b/sys/dev/ic/aic79xx.c      Thu Mar 27 18:28:26 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: aic79xx.c,v 1.46 2013/10/17 21:24:24 christos Exp $    */
+/*     $NetBSD: aic79xx.c,v 1.47 2014/03/27 18:28:26 christos Exp $    */
 
 /*
  * Core routines and tables shareable across OS platforms.
@@ -49,7 +49,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: aic79xx.c,v 1.46 2013/10/17 21:24:24 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: aic79xx.c,v 1.47 2014/03/27 18:28:26 christos Exp $");
 
 #include <dev/ic/aic79xx_osm.h>
 #include <dev/ic/aic79xx_inline.h>
@@ -5963,26 +5963,23 @@
 {
        const char *speed;
        const char *type;
-       int len;
-       char *ep;
-
-       ep = tbuf + l;
-
-       len = snprintf(tbuf, ep - tbuf, "%s: ",
+       size_t len;
+
+       len = snprintf(tbuf, l, "%s: ",
            ahd_chip_names[ahd->chip & AHD_CHIPID_MASK]);
-       tbuf += len;
-
+       if (len > l)
+               return;
        speed = "Ultra320 ";
        if ((ahd->features & AHD_WIDE) != 0) {
                type = "Wide ";
        } else {
                type = "Single ";
        }
-       len = snprintf(tbuf, ep - tbuf, "%s%sChannel %c, SCSI Id=%d, ",
+       len += snprintf(tbuf + len, l  - len, "%s%sChannel %c, SCSI Id=%d, ",
                      speed, type, ahd->channel, ahd->our_id);
-       tbuf += len;
-
-       snprintf(tbuf, ep - tbuf, "%s, %d SCBs", ahd->bus_description,
+       if (len > l)
+               return;
+       snprintf(tbuf + len, l - len, "%s, %d SCBs", ahd->bus_description,
                ahd->scb_data.maxhscbs);
 }
 
@@ -8597,7 +8594,7 @@
                   const char *name, u_int address, u_int value,
                   u_int *cur_column, u_int wrap_point)
 {
-       int     printed;
+       size_t  printed;
        u_int   printed_mask;
        char    line[1024];
 
@@ -8608,9 +8605,13 @@
                *cur_column = 0;
        }
        printed = snprintf(line, sizeof(line), "%s[0x%x]", name, value);
+       if (printed > sizeof(line))
+               printed = sizeof(line);
        if (table == NULL) {
                printed += snprintf(&line[printed], (sizeof line) - printed,
                    " ");
+               if (printed > sizeof(line))
+                       printed = sizeof(line);
                printf("%s", line);
                if (cur_column != NULL)
                        *cur_column += printed;
@@ -8626,6 +8627,8 @@
                         || ((printed_mask & table[entry].mask)
                          == table[entry].mask))
                                continue;
+                       if (printed > sizeof(line))
+                               printed = sizeof(line);
                        printed += snprintf(&line[printed],
                            (sizeof line) - printed, "%s%s",
                                printed_mask == 0 ? ":(" : "|",
@@ -8637,6 +8640,8 @@
                if (entry >= num_entries)
                        break;
        }
+       if (printed > sizeof(line))
+               printed = sizeof(line);
        if (printed_mask != 0)
                printed += snprintf(&line[printed],
                    (sizeof line) - printed, ") ");
diff -r 7e1696a1d6cb -r 4586e3bebf11 sys/dev/ic/aic7xxx.c
--- a/sys/dev/ic/aic7xxx.c      Thu Mar 27 18:27:34 2014 +0000
+++ b/sys/dev/ic/aic7xxx.c      Thu Mar 27 18:28:26 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: aic7xxx.c,v 1.130 2009/09/03 14:40:43 tsutsui Exp $    */
+/*     $NetBSD: aic7xxx.c,v 1.131 2014/03/27 18:28:26 christos Exp $   */
 
 /*
  * Core routines and tables shareable across OS platforms.
@@ -39,7 +39,7 @@
  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGES.
  *
- * $Id: aic7xxx.c,v 1.130 2009/09/03 14:40:43 tsutsui Exp $
+ * $Id: aic7xxx.c,v 1.131 2014/03/27 18:28:26 christos Exp $
  *
  * //depot/aic7xxx/aic7xxx/aic7xxx.c#112 $
  *
@@ -50,7 +50,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: aic7xxx.c,v 1.130 2009/09/03 14:40:43 tsutsui Exp $");
+__KERNEL_RCSID(0, "$NetBSD: aic7xxx.c,v 1.131 2014/03/27 18:28:26 christos Exp $");
 
 #include <dev/ic/aic7xxx_osm.h>
 #include <dev/ic/aic7xxx_inline.h>
@@ -4390,19 +4390,17 @@
 void
 ahc_controller_info(struct ahc_softc *ahc, char *tbuf, size_t l)
 {
-       int len;
-       char *ep;
-
-       ep = tbuf + l;
-
-       len = snprintf(tbuf, ep - tbuf, "%s: ",
+       size_t len;
+
+       len = snprintf(tbuf, l, "%s: ",
            ahc_chip_names[ahc->chip & AHC_CHIPID_MASK]);
-       tbuf += len;
+       if (len > l)
+               return;
        if ((ahc->features & AHC_TWIN) != 0)
-               len = snprintf(tbuf, ep - tbuf, "Twin Channel, A SCSI Id=%d, "
-                             "B SCSI Id=%d, primary %c, ",
-                             ahc->our_id, ahc->our_id_b,
-                             (ahc->flags & AHC_PRIMARY_CHANNEL) + 'A');
+               len += snprintf(tbuf + len, l - len,
+                   "Twin Channel, A SCSI Id=%d, B SCSI Id=%d, primary %c, ",
+                   ahc->our_id, ahc->our_id_b,
+                   (ahc->flags & AHC_PRIMARY_CHANNEL) + 'A');
        else {
                const char *speed;
                const char *type;
@@ -4420,16 +4418,17 @@
                } else {
                        type = "Single";
                }
-               len = snprintf(tbuf, ep - tbuf, "%s%s Channel %c, SCSI Id=%d, ",
+               len += snprintf(tbuf + len, l - len, "%s%s Channel %c, SCSI Id=%d, ",
                              speed, type, ahc->channel, ahc->our_id);
        }
-       tbuf += len;
+       if (len > l)
+               return;
 
        if ((ahc->flags & AHC_PAGESCBS) != 0)
-               snprintf(tbuf, ep - tbuf, "%d/%d SCBs",
+               snprintf(tbuf + len, l - len, "%d/%d SCBs",
                        ahc->scb_data->maxhscbs, AHC_MAX_QUEUE);
        else
-               snprintf(tbuf, ep - tbuf, "%d SCBs", ahc->scb_data->maxhscbs);
+               snprintf(tbuf + len, l - len, "%d SCBs", ahc->scb_data->maxhscbs);
 }
 
 /*
@@ -6545,7 +6544,7 @@
     const char *name, u_int address, u_int value,
     u_int *cur_column, u_int wrap_point)
 {
-       int     printed;
+       size_t  printed;
        u_int   printed_mask;
        char    line[1024];
 
@@ -6556,9 +6555,13 @@
                *cur_column = 0;
        }
        printed = snprintf(line, sizeof(line), "%s[0x%x]", name, value);
+       if (printed > sizeof(line))
+               printed = sizeof(line);
        if (table == NULL) {
                printed += snprintf(&line[printed], (sizeof line) - printed,
                    " ");
+               if (printed > sizeof(line))
+                       printed = sizeof(line);
                printf("%s", line);
                if (cur_column != NULL)
                        *cur_column += printed;
@@ -6574,6 +6577,8 @@
                         || ((printed_mask & table[entry].mask)
                          == table[entry].mask))
                                continue;
+                       if (printed > sizeof(line))
+                               printed = sizeof(line);
                        printed += snprintf(&line[printed],
                            (sizeof line) - printed, "%s%s",
                                printed_mask == 0 ? ":(" : "|",
@@ -6585,6 +6590,8 @@
                if (entry >= num_entries)
                        break;
        }
+       if (printed > sizeof(line))
+               printed = sizeof(line);
        if (printed_mask != 0)
                printed += snprintf(&line[printed],
                    (sizeof line) - printed, ") ");
diff -r 7e1696a1d6cb -r 4586e3bebf11 sys/dev/ic/mpt_debug.c
--- a/sys/dev/ic/mpt_debug.c    Thu Mar 27 18:27:34 2014 +0000
+++ b/sys/dev/ic/mpt_debug.c    Thu Mar 27 18:28:26 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mpt_debug.c,v 1.8 2011/10/17 16:40:53 mbalmer Exp $    */
+/*     $NetBSD: mpt_debug.c,v 1.9 2014/03/27 18:28:26 christos Exp $   */
 
 /*
  * Copyright (c) 2000, 2001 by Greg Ansley
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mpt_debug.c,v 1.8 2011/10/17 16:40:53 mbalmer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mpt_debug.c,v 1.9 2014/03/27 18:28:26 christos Exp $");
 
 #include <dev/ic/mpt.h>
 
@@ -195,14 +195,17 @@
 {
        const struct Error_Map *status = IOC_Diag;
        static char tbuf[128];
-       char *ptr = tbuf;
-       char *end = &tbuf[128];
-       tbuf[0] = '\0';
-       ptr += snprintf(tbuf, sizeof tbuf, "(0x%08x)", code);
+       size_t len;
+       len = snprintf(tbuf, sizeof(tbuf), "(0x%08x)", code);
+       if (len > sizeof(tbuf))
+               return tbuf;
        while (status->Error_Code >= 0) {
-               if ((status->Error_Code & code) != 0)
-                       ptr += snprintf(ptr, (size_t)(end-ptr), "%s ",
-                               status->Error_String);
+               if ((status->Error_Code & code) != 0) {
+                       len += snprintf(tbuf + len, sizeof(tbuf) - len, "%s ",
+                           status->Error_String);
+                       if (len > sizeof(tbuf))
+                               return tbuf;
+               }
                status++;
        }
        return tbuf;
@@ -239,14 +242,17 @@
 {
        const struct Error_Map *status = IOC_SCSIState;
        static char tbuf[128];
-       char *ptr = tbuf;
-       char *end = &tbuf[128];
-       tbuf[0] = '\0';
-       ptr += snprintf(tbuf, sizeof tbuf, "(0x%08x)", code);
+       size_t len;
+       len = snprintf(tbuf, sizeof(tbuf), "(0x%08x)", code);
+       if (len > sizeof(tbuf))
+               return tbuf;
        while (status->Error_Code >= 0) {
-               if ((status->Error_Code & code) != 0)
-                       ptr += snprintf(ptr, (size_t)(end-ptr), "%s ",
+               if ((status->Error_Code & code) != 0) {
+                       len += snprintf(tbuf + len, sizeof(tbuf) - len, "%s ",
                                status->Error_String);
+                       if (len > sizeof(tbuf))
+                               return tbuf;
+               }
                status++;
        }
        return tbuf;



Home | Main Index | Thread Index | Old Index