]> diplodocus.org Git - nmh/commitdiff
Check advertise()'s iob[] isn't too short with assert(3).
authorRalph Corderoy <ralph@inputplus.co.uk>
Sat, 22 Oct 2016 14:53:51 +0000 (15:53 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Sat, 22 Oct 2016 14:53:51 +0000 (15:53 +0100)
Using DIM(a) added to h/mh.h

h/mh.h
sbr/error.c

diff --git a/h/mh.h b/h/mh.h
index 55825da03e2f941917dbd925132b717a550f0677..67b71702c2f13b76379f251a131ffb23c3891375 100644 (file)
--- a/h/mh.h
+++ b/h/mh.h
@@ -43,6 +43,9 @@ typedef unsigned char  boolean;  /* not int so we can pack in a structure */
 #define NMH_UNUSED(i) i
 #endif
 
 #define NMH_UNUSED(i) i
 #endif
 
+/* DIM gives the number of elements in the one-dimensional array a. */
+#define DIM(a) (sizeof (a) / sizeof (*(a)))
+
 /* LEN gives the strlen() of string constant s, excluding the
  * terminating NUL. */
 #define LEN(s) (sizeof (s) - 1)
 /* LEN gives the strlen() of string constant s, excluding the
  * terminating NUL. */
 #define LEN(s) (sizeof (s) - 1)
index 5dc84e87a438ca15b8570fa64fb6a3d4ff208605..d6d96851504420c9a8a3995075fc63f7fa016600 100644 (file)
@@ -70,7 +70,8 @@ advertise (const char *what, char *tail, const char *fmt, va_list ap)
 {
     int        eindex = errno;
     char buffer[BUFSIZ], errbuf[BUFSIZ], *err;
 {
     int        eindex = errno;
     char buffer[BUFSIZ], errbuf[BUFSIZ], *err;
-    struct iovec iob[20], *iov;
+    struct iovec iob[10], *iov;
+    size_t niov;
 
     iov = iob;
 
 
     iov = iob;
 
@@ -112,9 +113,12 @@ advertise (const char *what, char *tail, const char *fmt, va_list ap)
 #undef ADD_LITERAL
 #undef ADD_VAR
 
 #undef ADD_LITERAL
 #undef ADD_VAR
 
+    niov = iov - iob;
+    assert(niov <= DIM(iob));
+
     fflush (stdout);
     fflush (stderr);
     fflush (stdout);
     fflush (stderr);
-    if (writev (fileno (stderr), iob, iov - iob) < 0) {
+    if (writev(fileno(stderr), iob, niov) == -1) {
         snprintf(buffer, sizeof buffer, "%s: write stderr failed: %d\n",
             invo_name && *invo_name ? invo_name : "nmh", errno);
         if (write(2, buffer, strlen(buffer)) == -1) {
         snprintf(buffer, sizeof buffer, "%s: write stderr failed: %d\n",
             invo_name && *invo_name ? invo_name : "nmh", errno);
         if (write(2, buffer, strlen(buffer)) == -1) {