<div dir="ltr">Hi Rob,<div><br></div><div>In function __readfile()__, the buffer __buf__ is free'd when __readall()__ fails.</div><div>This __free__ can cause a crash, if the buffer passed by user of function </div><div>
is not __malloc'ed__ one.</div><div><br></div><div>__names_to_pid()__ is one usecase example here.</div><div><br></div><div>The patch takes care of the same.</div><div><br></div><div><div>diff -Naur a/lib/lib.c b/lib/lib.c</div>
<div>--- a/lib/lib.c<span class="" style="white-space:pre"> </span>2014-04-29 16:33:17.000000000 +0530</div><div>+++ b/lib/lib.c<span class="" style="white-space:pre"> </span>2014-05-02 16:14:27.194614098 +0530</div><div>
@@ -323,9 +323,10 @@</div><div> </div><div> // Read contents of file as a single nul-terminated string.</div><div> // malloc new one if buf=len=0</div><div>-char *readfile(char *name, char *buf, off_t len)</div><div>+char *readfile(char *name, char *ibuf, off_t len)</div>
<div> {</div><div> int fd;</div><div>+ char *buf;</div><div> </div><div> fd = open(name, O_RDONLY);</div><div> if (fd == -1) return 0;</div><div>@@ -335,12 +336,13 @@</div><div> // proc files don't report a length, so try 1 page minimum.</div>
<div> if (len<4096) len = 4096;</div><div> }</div><div>- if (!buf) buf = xmalloc(len+1);</div><div>+ if (!ibuf) buf = xmalloc(len+1);</div><div>+ else buf = ibuf;</div><div> </div><div> len = readall(fd, buf, len-1);</div>
<div> close(fd);</div><div> if (len<0) {</div><div>- free(buf);</div><div>+ if (ibuf != buf) free(buf);</div><div> buf = 0;</div><div> } else buf[len] = 0;</div></div><div><br></div><div>regards,</div><div>
Ashwini</div></div>