[Toybox] vmstat bug + patch

enh enh at google.com
Tue Sep 1 14:44:49 PDT 2015


ping.

here's yet another patch from the third person to hit this
(https://android-review.googlesource.com/#/c/169242/):

Author: Colin Cross <ccross at android.com>
Date:   Tue Sep 1 14:32:59 2015 -0700

    vmstat: reset header pointer when looping

    Modifying the headers pointer when printing the headers causes
    a buffer overrun the second time they are printed.  Use a local
    header pointer that is reset to the beginning of the buffer for
    each loop.

    Change-Id: Id2520b78255833197b4075f789766395bc36f218

diff --git a/toys/other/vmstat.c b/toys/other/vmstat.c
index 9a38e45..5b79702 100644
--- a/toys/other/vmstat.c
+++ b/toys/other/vmstat.c
@@ -96,14 +96,14 @@ void vmstat_main(void)

     // Print headers
     if (rows>3 && !(loop % (rows-3))) {
+      const char *header = headers;
       if (isatty(1)) terminal_size(0, &rows);
       else rows = 0;

       printf("procs -----------memory---------- ---swap-- -----io----
-system-- ----cpu----\n");
-
       for (i=0; i<sizeof(lengths); i++) {
-        printf(" %*s"+!i, lengths[i], headers);
-        headers += strlen(headers)+1;
+        printf(" %*s"+!i, lengths[i], header);
+        header += strlen(header)+1;
       }
       xputc('\n');
     }


On Mon, May 11, 2015 at 1:22 AM, José Bollo <jobol at nonadev.net> wrote:
> Please find here an alternate proposal for fixing that issue.
> Best regards
> José
>
>
> diff --git a/toys/other/vmstat.c b/toys/other/vmstat.c
> index eed7945..0884fe9 100644
> --- a/toys/other/vmstat.c
> +++ b/toys/other/vmstat.c
> @@ -93,14 +93,15 @@ void vmstat_main(void)
>
>      // Print headers
>      if (rows>3 && !(loop % (rows-3))) {
> +      char *h = headers;
>        if (isatty(1)) terminal_size(0, &rows);
>        else rows = 0;
>
>        printf("procs -----------memory---------- ---swap-- -----io----
> -system-- ----cpu----\n");
>
>        for (i=0; i<sizeof(lengths); i++) {
> -        printf(" %*s"+!i, lengths[i], headers);
> -        headers += strlen(headers)+1;
> +        printf(" %*s"+!i, lengths[i], h++);
> +        while(*h++ != 0);
>        }
>        xputc('\n');
>      }
>
>
>
> _______________________________________________
> Toybox mailing list
> Toybox at lists.landley.net
> http://lists.landley.net/listinfo.cgi/toybox-landley.net



-- 
Elliott Hughes - http://who/enh - http://jessies.org/~enh/
Android native code/tools questions? Mail me/drop by/add me as a reviewer.


More information about the Toybox mailing list