Author: dim
Date: Sun Feb 10 13:44:36 2019
New Revision: 343959
URL: https://svnweb.freebsd.org/changeset/base/343959

Log:
  Fix the first couple of AddressSanitizer violations in usr.bin/top.
  
  Avoid setting zero bytes beyond the length of the 'thisline' parameters
  in i_process() and u_process(), and don't attempt to memset a negative
  number of bytes.
  
  MFC after:    1 week

Modified:
  head/usr.bin/top/display.c

Modified: head/usr.bin/top/display.c
==============================================================================
--- head/usr.bin/top/display.c  Sun Feb 10 13:34:21 2019        (r343958)
+++ head/usr.bin/top/display.c  Sun Feb 10 13:44:36 2019        (r343959)
@@ -829,7 +829,11 @@ i_process(int line, char *thisline)
     }
 
     /* truncate the line to conform to our current screen width */
-    thisline[screen_width] = '\0';
+    int len = strlen(thisline);
+    if (screen_width < len)
+    {
+       thisline[screen_width] = '\0';
+    }
 
     /* write the line out */
     fputs(thisline, stdout);
@@ -839,7 +843,10 @@ i_process(int line, char *thisline)
     p = stpcpy(base, thisline);
 
     /* zero fill the rest of it */
-    memset(p, 0, screen_width - (p - base));
+    if (p - base < screen_width)
+    {
+       memset(p, 0, screen_width - (p - base));
+    }
 }
 
 void
@@ -853,7 +860,11 @@ u_process(int line, char *newline)
     bufferline = &screenbuf[lineindex(line)];
 
     /* truncate the line to conform to our current screen width */
-    newline[screen_width] = '\0';
+    int len = strlen(newline);
+    if (screen_width < len)
+    {
+       newline[screen_width] = '\0';
+    }
 
     /* is line higher than we went on the last display? */
     if (line >= last_hi)
@@ -878,7 +889,10 @@ u_process(int line, char *newline)
        optr = stpcpy(bufferline, newline);
 
        /* zero fill the rest of it */
-       memset(optr, 0, screen_width - (optr - bufferline));
+       if (optr - bufferline < screen_width)
+       {
+           memset(optr, 0, screen_width - (optr - bufferline));
+       }
     }
     else
     {
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to