Module Name: src Committed By: christos Date: Thu Mar 27 18:28:26 UTC 2014
Modified Files: src/sys/dev/ic: aic79xx.c aic7xxx.c mpt_debug.c Log Message: cleanup debugging printfs to avoid overflow To generate a diff of this commit: cvs rdiff -u -r1.46 -r1.47 src/sys/dev/ic/aic79xx.c cvs rdiff -u -r1.130 -r1.131 src/sys/dev/ic/aic7xxx.c cvs rdiff -u -r1.8 -r1.9 src/sys/dev/ic/mpt_debug.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/dev/ic/aic79xx.c diff -u src/sys/dev/ic/aic79xx.c:1.46 src/sys/dev/ic/aic79xx.c:1.47 --- src/sys/dev/ic/aic79xx.c:1.46 Thu Oct 17 17:24:24 2013 +++ src/sys/dev/ic/aic79xx.c Thu Mar 27 14:28:26 2014 @@ -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 @@ ahd_controller_info(struct ahd_softc *ah { const char *speed; const char *type; - int len; - char *ep; + size_t len; - ep = tbuf + l; - - len = snprintf(tbuf, ep - tbuf, "%s: ", + 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 @@ ahd_print_register(ahd_reg_parse_entry_t 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 @@ ahd_print_register(ahd_reg_parse_entry_t *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 @@ ahd_print_register(ahd_reg_parse_entry_t || ((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 @@ ahd_print_register(ahd_reg_parse_entry_t if (entry >= num_entries) break; } + if (printed > sizeof(line)) + printed = sizeof(line); if (printed_mask != 0) printed += snprintf(&line[printed], (sizeof line) - printed, ") "); Index: src/sys/dev/ic/aic7xxx.c diff -u src/sys/dev/ic/aic7xxx.c:1.130 src/sys/dev/ic/aic7xxx.c:1.131 --- src/sys/dev/ic/aic7xxx.c:1.130 Thu Sep 3 10:40:43 2009 +++ src/sys/dev/ic/aic7xxx.c Thu Mar 27 14:28:26 2014 @@ -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 @@ ahc_alloc_scbs(struct ahc_softc *ahc) void ahc_controller_info(struct ahc_softc *ahc, char *tbuf, size_t l) { - int len; - char *ep; + size_t len; - ep = tbuf + l; - - len = snprintf(tbuf, ep - tbuf, "%s: ", + 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 @@ ahc_controller_info(struct ahc_softc *ah } 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 @@ ahc_print_register(ahc_reg_parse_entry_t 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 @@ ahc_print_register(ahc_reg_parse_entry_t *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 @@ ahc_print_register(ahc_reg_parse_entry_t || ((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 @@ ahc_print_register(ahc_reg_parse_entry_t if (entry >= num_entries) break; } + if (printed > sizeof(line)) + printed = sizeof(line); if (printed_mask != 0) printed += snprintf(&line[printed], (sizeof line) - printed, ") "); Index: src/sys/dev/ic/mpt_debug.c diff -u src/sys/dev/ic/mpt_debug.c:1.8 src/sys/dev/ic/mpt_debug.c:1.9 --- src/sys/dev/ic/mpt_debug.c:1.8 Mon Oct 17 12:40:53 2011 +++ src/sys/dev/ic/mpt_debug.c Thu Mar 27 14:28:26 2014 @@ -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 @@ mpt_ioc_diag(u_int32_t code) { 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 @@ mpt_scsi_state(int code) { 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;