perf strbuf: Match va_{add,copy} with va_end
authorSanskriti Sharma <sansharm@redhat.com>
Tue, 2 Oct 2018 14:29:10 +0000 (10:29 -0400)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 8 Oct 2018 17:23:44 +0000 (14:23 -0300)
Ensure that all code paths in strbuf_addv() call va_end() on the
ap_saved copy that was made.

Fixes the following coverity complaint:

  Error: VARARGS (CWE-237): [#def683]
  tools/perf/util/strbuf.c:106: missing_va_end: va_end was not called
  for "ap_saved".

Signed-off-by: Sanskriti Sharma <sansharm@redhat.com>
Reviewed-by: Jiri Olsa <jolsa@kernel.org>
Cc: Joe Lawrence <joe.lawrence@redhat.com>
Link: http://lkml.kernel.org/r/1538490554-8161-2-git-send-email-sansharm@redhat.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/strbuf.c

index 3d1cf5bf7f184b9c18a0fbb59768510e3620cc6f..9005fbe0780edff5d142c0f4c00ac890afa98654 100644 (file)
@@ -98,19 +98,25 @@ static int strbuf_addv(struct strbuf *sb, const char *fmt, va_list ap)
 
        va_copy(ap_saved, ap);
        len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
-       if (len < 0)
+       if (len < 0) {
+               va_end(ap_saved);
                return len;
+       }
        if (len > strbuf_avail(sb)) {
                ret = strbuf_grow(sb, len);
-               if (ret)
+               if (ret) {
+                       va_end(ap_saved);
                        return ret;
+               }
                len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap_saved);
                va_end(ap_saved);
                if (len > strbuf_avail(sb)) {
                        pr_debug("this should not happen, your vsnprintf is broken");
+                       va_end(ap_saved);
                        return -EINVAL;
                }
        }
+       va_end(ap_saved);
        return strbuf_setlen(sb, sb->len + len);
 }