coverity/133892: Resource leak

Variable tbuf going out of scope leaks the storage it points to.

We don't have to use the copy tbuf of a match. Because matches are always in
ctags style, we can operate on them directly.
This commit is contained in:
oni-link
2016-01-21 16:22:34 +01:00
parent f2558890f5
commit a649299e76

View File

@@ -1646,7 +1646,6 @@ static char *cs_pathcomponents(char *path)
static void cs_print_tags_priv(char **matches, char **cntxts, static void cs_print_tags_priv(char **matches, char **cntxts,
size_t num_matches) FUNC_ATTR_NONNULL_ALL size_t num_matches) FUNC_ATTR_NONNULL_ALL
{ {
char *fname, *lno, *extra, *tbuf;
size_t num; size_t num;
char *globalcntx = "GLOBAL"; char *globalcntx = "GLOBAL";
char *context; char *context;
@@ -1681,26 +1680,23 @@ static void cs_print_tags_priv(char **matches, char **cntxts,
assert(strcnt(matches[i], '\t') >= 2); assert(strcnt(matches[i], '\t') >= 2);
size_t idx = i; size_t idx = i;
/* if we really wanted to, we could avoid this malloc and strcpy // Parse filename, line number and optional part.
* by parsing matches[i] on the fly and placing stuff into buf char *fname = strchr(matches[i], '\t') + 1;
* directly, but that's too much of a hassle char *fname_end = strchr(fname, '\t');
*/ // Replace second '\t' in matches[i] with NUL to terminate fname.
tbuf = xmalloc(strlen(matches[idx]) + 1); *fname_end = NUL;
(void)strcpy(tbuf, matches[idx]);
if (strtok(tbuf, (const char *)"\t") == NULL) char *lno = fname_end + 1;
continue; char *extra = xstrchrnul(lno, '\t');
if ((fname = strtok(NULL, (const char *)"\t")) == NULL) // Ignore ;" at the end of lno.
continue; char *lno_end = extra - 2;
if ((lno = strtok(NULL, (const char *)"\t")) == NULL) *lno_end = NUL;
continue; // Do we have an optional part?
extra = strtok(NULL, (const char *)"\t"); extra = *extra ? extra + 1 : NULL;
lno[strlen(lno)-2] = '\0'; /* ignore ;" at the end */
const char *csfmt_str = "%4zu %6s "; const char *csfmt_str = "%4zu %6s ";
/* hopefully 'num' (num of matches) will be less than 10^16 */ /* hopefully 'num' (num of matches) will be less than 10^16 */
newsize = strlen(csfmt_str) + 16 + strlen(lno); newsize = strlen(csfmt_str) + 16 + (size_t)(lno_end - lno);
if (bufsize < newsize) { if (bufsize < newsize) {
buf = xrealloc(buf, newsize); buf = xrealloc(buf, newsize);
bufsize = newsize; bufsize = newsize;
@@ -1737,7 +1733,9 @@ static void cs_print_tags_priv(char **matches, char **cntxts,
MSG_PUTS_LONG(extra); MSG_PUTS_LONG(extra);
} }
xfree(tbuf); /* only after printing extra due to strtok use */ // restore matches[i]
*fname_end = '\t';
*lno_end = ';';
if (msg_col) if (msg_col)
msg_putchar('\n'); msg_putchar('\n');