Module Name: src Committed By: martin Date: Mon Feb 11 19:15:38 UTC 2019
Modified Files: src/usr.sbin/sysinst: Makefile.inc partman.c Log Message: Make sure we properly truncate snprintf() results when the target buffer is too small, in a way that appeases gcc 7 warnings. To generate a diff of this commit: cvs rdiff -u -r1.17 -r1.18 src/usr.sbin/sysinst/Makefile.inc cvs rdiff -u -r1.25 -r1.26 src/usr.sbin/sysinst/partman.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.sbin/sysinst/Makefile.inc diff -u src/usr.sbin/sysinst/Makefile.inc:1.17 src/usr.sbin/sysinst/Makefile.inc:1.18 --- src/usr.sbin/sysinst/Makefile.inc:1.17 Mon Feb 4 10:22:15 2019 +++ src/usr.sbin/sysinst/Makefile.inc Mon Feb 11 19:15:38 2019 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.inc,v 1.17 2019/02/04 10:22:15 mrg Exp $ +# $NetBSD: Makefile.inc,v 1.18 2019/02/11 19:15:38 martin Exp $ # # Makefile for sysinst @@ -118,11 +118,6 @@ COPTS.bsddisklabel.c += -Wno-format-nonl COPTS.md.c += -Wno-format-nonliteral COPTS.partman.c += -Wno-format-nonliteral -Wno-stack-protector -# Lots of warnings -.if defined(HAVE_GCC) && ${HAVE_GCC} == 7 && ${ACTIVE_CC} == "gcc" -COPTS.partman.c+= -Wno-error=format-truncation -.endif - # Host to ftp from. Default: # "ftp.NetBSD.org" # Index: src/usr.sbin/sysinst/partman.c diff -u src/usr.sbin/sysinst/partman.c:1.25 src/usr.sbin/sysinst/partman.c:1.26 --- src/usr.sbin/sysinst/partman.c:1.25 Thu Dec 13 12:28:25 2018 +++ src/usr.sbin/sysinst/partman.c Mon Feb 11 19:15:38 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: partman.c,v 1.25 2018/12/13 12:28:25 martin Exp $ */ +/* $NetBSD: partman.c,v 1.26 2019/02/11 19:15:38 martin Exp $ */ /* * Copyright 2012 Eugene Lozovoy @@ -1893,7 +1893,7 @@ pm_gpt_commit(void) int pm_getrefdev(pm_devs_t *pm_cur) { - int i, ii, dev_num, num_devs, num_devs_s; + int i, ii, dev_num, num_devs, num_devs_s, r; char dev[SSTRSIZE]; dev[0] = '\0'; pm_cur->refdev = NULL; @@ -1902,10 +1902,15 @@ pm_getrefdev(pm_devs_t *pm_cur) for (i = 0; i < MAX_CGD; i++) if (cgds[i].blocked && cgds[i].node == dev_num) { pm_cur->refdev = &cgds[i]; - - snprintf(pm_cur->diskdev_descr, STRSIZE, "%s (%s, %s-%d)", - pm_cur->diskdev_descr, cgds[i].pm_name, - cgds[i].enc_type, cgds[i].key_size); + r = snprintf(pm_cur->diskdev_descr, + sizeof(pm_cur->diskdev_descr)-1, + "%s (%s, %s-%d)", + pm_cur->diskdev_descr, cgds[i].pm_name, + cgds[i].enc_type, cgds[i].key_size); + if (r >= (int)sizeof(pm_cur->diskdev_descr)) + pm_cur->diskdev_descr[ + sizeof(pm_cur->diskdev_descr)-1] + = 0; break; } } else if (! strncmp(pm_cur->diskdev, "vnd", 3)) { @@ -1914,8 +1919,17 @@ pm_getrefdev(pm_devs_t *pm_cur) if (vnds[i].blocked && vnds[i].node == dev_num) { pm_cur->refdev = &vnds[i]; pm_getdevstring(dev, SSTRSIZE, vnds[i].pm, vnds[i].pm_part); - snprintf(pm_cur->diskdev_descr, STRSIZE, "%s (%s, %s)", - pm_cur->diskdev_descr, dev, vnds[i].filepath); + r = snprintf(pm_cur->diskdev_descr, + sizeof(pm_cur->diskdev_descr)-1, + "%s (%s, %s)", + pm_cur->diskdev_descr, dev, + vnds[i].filepath); + if (r >= (int)sizeof(pm_cur->diskdev_descr)) + pm_cur->diskdev_descr[ + sizeof(pm_cur->diskdev_descr)-1] + = 0; + pm_cur->diskdev_descr[ + sizeof(pm_cur->diskdev_descr)-1] = 0; break; } } else if (! strncmp(pm_cur->diskdev, "raid", 4)) { @@ -1931,9 +1945,14 @@ pm_getrefdev(pm_devs_t *pm_cur) else num_devs++; } - snprintf(pm_cur->diskdev_descr, STRSIZE, + r = snprintf(pm_cur->diskdev_descr, + sizeof(pm_cur->diskdev_descr)-1, "%s (lvl %d, %d disks, %d spare)", pm_cur->diskdev_descr, raids[i].raid_level, num_devs, num_devs_s); + if (r >= (int)sizeof(pm_cur->diskdev_descr)) + pm_cur->diskdev_descr[ + sizeof(pm_cur->diskdev_descr)-1] + = 0; break; } } else @@ -2178,13 +2197,16 @@ pm_mountall(void) static int pm_mount(pm_devs_t *pm_cur, int part_num) { - int error = 0; + int error = 0, r; char buf[MOUNTLEN]; if (strlen(pm_cur->bsdlabel[part_num].mounted) > 0) return 0; - snprintf(buf, MOUNTLEN, "/tmp/%s%c", pm_cur->diskdev, part_num + 'a'); + r = snprintf(buf, sizeof(buf)-1, "/tmp/%s%c", pm_cur->diskdev, + part_num + 'a'); + if (r >= (int)sizeof(buf)) + buf[sizeof(buf)-1] = 0; if (! dir_exists_p(buf)) run_program(RUN_DISPLAY | RUN_PROGRESS, "/bin/mkdir -p %s", buf); if (pm_cur->bsdlabel[part_num].pi_flags & PIF_MOUNT && @@ -2479,7 +2501,7 @@ pm_menufmt(menudesc *m, int opt, void *a { const char *dev_status = ""; char buf[STRSIZE]; - int part_num = ((part_entry_t *)arg)[opt].dev_num; + int r, part_num = ((part_entry_t *)arg)[opt].dev_num; pm_devs_t *pm_cur = ((part_entry_t *)arg)[opt].dev_ptr; switch (((part_entry_t *)arg)[opt].type) { @@ -2531,8 +2553,11 @@ pm_menufmt(menudesc *m, int opt, void *a pm_cur->bsdlabel[part_num].pi_size / (MEG / pm_cur->sectorsize)); break; case PM_SPEC_T: - snprintf(buf, STRSIZE, "%s: %s", - pm_cur->diskdev_descr, pm_cur->bsdlabel[0].pi_mount); + r = snprintf(buf, sizeof(buf)-1, "%s: %s", + pm_cur->diskdev_descr, + pm_cur->bsdlabel[0].pi_mount); + if (r >= (int)sizeof(buf)) + buf[sizeof(buf)-1] = 0; wprintw(m->mw, "%-33.32s %-22.21s %11luM", buf, getfslabelname(pm_cur->bsdlabel[0].pi_fstype), pm_cur->bsdlabel[0].pi_size / (MEG / pm_cur->sectorsize));