[PATCH v2] top: fix and merge code to parse /proc/meminfo

Timo Teras timo.teras at iki.fi
Mon Jul 28 16:48:31 UTC 2014


On Mon, 28 Jul 2014 17:52:41 +0200
Bartosz Gołaszewski <bartekgola at gmail.com> wrote:

> 2014-07-28 17:25 GMT+02:00 Timo Teras <timo.teras at iki.fi>:
> > So something like:
> >
> > diff --git a/procps/top.c b/procps/top.c
> > index 62f9421..119c32b 100644
> > --- a/procps/top.c
> > +++ b/procps/top.c
> > @@ -536,7 +536,7 @@ static void parse_meminfo(unsigned long
> > meminfo[MI_MAX]) FILE *f;
> >         int i;
> >
> > -       memset(meminfo, 0, sizeof(meminfo));
> > +       memset(meminfo, 0, MI_MAX*sizeof(meminfo[0]));
> >         f = xfopen_for_read("meminfo");
> >         while (fgets(buf, sizeof(buf), f) != NULL) {
> >                 char *c = strchr(buf, ':');
> >
> > Would be needed to fix it.
> 
> This should be memset(meminfo, 0, MI_MAX*sizeof(unsigned long))
> otherwise you're taking the size of a pointer.

I was taking sizeof of individual element: meminfo[0]. Alternative
would be sizeof *meminfo. I prefer not to expand to the actual type.
It's easier to change if needed, and the correctness more obvious as
you don't need to look what was type of meminfo - looking at the
memset line alone is sufficient.
 


More information about the busybox mailing list