Skip to content

Commit

Permalink
Silence gcc warnings (#973)
Browse files Browse the repository at this point in the history
* Silence gcc warning in bcf_hdr_printf()

For some optimisation levels, gcc would report a format-truncation
warning for the line that measured how much memory was needed to
store the header string.  Silence it by supplying a reasonably
big buffer, which has the added bonus that the malloc and second
call to vsnprintf can be avoided completely in most cases.

* Silence gcc 4 warning

Silence 'end' may be used uninitialized warning from gcc 4
by making it more obvious that EOF is returned when 'start' is
NULL (and 'end' is not used in that case).
  • Loading branch information
daviesrob authored and whitwham committed Nov 19, 2019
1 parent d256bed commit 1579ace
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
9 changes: 4 additions & 5 deletions hfile_s3_write.c
Expand Up @@ -171,13 +171,12 @@ static int get_entry(char *in, char *start_tag, char *end_tag, kstring_t *out) {
}

start = strstr(in, start_tag);
if (!start) return EOF;

if (start) {
start += strlen(start_tag);
end = strstr(start, end_tag);
}
start += strlen(start_tag);
end = strstr(start, end_tag);

if (!start || !end) return EOF;
if (!end) return EOF;

return kputsn(start, end - start, out);
}
Expand Down
19 changes: 13 additions & 6 deletions vcf.c
Expand Up @@ -942,19 +942,26 @@ void bcf_hdr_remove(bcf_hdr_t *hdr, int type, const char *key)

int bcf_hdr_printf(bcf_hdr_t *hdr, const char *fmt, ...)
{
char tmp[256], *line = tmp;
va_list ap;
va_start(ap, fmt);
int n = vsnprintf(NULL, 0, fmt, ap) + 2;
int n = vsnprintf(line, sizeof(tmp), fmt, ap);
va_end(ap);

char *line = (char*)malloc(n);
va_start(ap, fmt);
vsnprintf(line, n, fmt, ap);
va_end(ap);
if (n >= sizeof(tmp)) {
n++; // For trailing NUL
line = (char*)malloc(n);
if (!line)
return -1;

va_start(ap, fmt);
vsnprintf(line, n, fmt, ap);
va_end(ap);
}

int ret = bcf_hdr_append(hdr, line);

free(line);
if (line != tmp) free(line);
return ret;
}

Expand Down

0 comments on commit 1579ace

Please sign in to comment.