Re: [PATCH perf/core v2 5/8] perf header: Make topology checkers to check return value of strbuf

From: Masami Hiramatsu
Date: Mon May 09 2016 - 22:58:28 EST


On Thu, 5 May 2016 20:55:18 -0300
Arnaldo Carvalho de Melo <acme@xxxxxxxxxx> wrote:

> Em Sat, Apr 30, 2016 at 12:10:33AM +0900, Masami Hiramatsu escreveu:
> > Make topology checkers to check the return value of strbuf
> > APIs so that it can detect errors in it.
>
> > }
> > @@ -1907,7 +1910,7 @@ static int process_numa_topology(struct perf_file_section *section __maybe_unuse
> > u32 nr, node, i;
> > char *str;
> > uint64_t mem_total, mem_free;
> > - struct strbuf sb;
> > + struct strbuf sb = STRBUF_INIT;
>
> Since you're going to call strbuf_init() later, is the above really
> needed?

Actually, this is for strbuf_release() at the error path at the
end of this function.
----
error:
strbuf_release(&sb);
return -1;
}
----

So, without initializing sb, strbuf_release tries to release random
address in sb.buf.
Of course we can just return -1 for such cases and avoid initializing :)

>
> > /* nr nodes */
> > ret = readn(fd, &nr, sizeof(nr));
> > @@ -1918,7 +1921,8 @@ static int process_numa_topology(struct perf_file_section *section __maybe_unuse
> > nr = bswap_32(nr);
> >
> > ph->env.nr_numa_nodes = nr;
> > - strbuf_init(&sb, 256);
> > + if (strbuf_init(&sb, 256) < 0)
> > + goto error;
> >


--
Masami Hiramatsu <mhiramat@xxxxxxxxxx>