Skip to content

Commit

Permalink
Handle broken vsnprintf implementations in strbuf
Browse files Browse the repository at this point in the history
Solaris 9's vsnprintf implementation returns -1 if we pass it a
buffer of length 0.  The only way to get it to give us the actual
length necessary for the formatted string is to grow the buffer
out to have at least 1 byte available in the strbuf and then ask
it to compute the length.

If the available space is 0 I'm growing it out by 64 to ensure
we will get an accurate length estimate from all implementations.
Some callers may need to grow the strbuf again but 64 should be a
reasonable enough initial growth.

We also no longer silently fail to append to the string when we are
faced with a broken vsnprintf implementation.  On Solaris 9 this
silent failure caused me to no longer be able to execute "git clone"
as we tried to exec the empty string rather than "git-clone".

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Shawn O. Pearce authored and Junio C Hamano committed Nov 14, 2007
1 parent 71aa2b8 commit f141bd8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions strbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
int len;
va_list ap;

if (!strbuf_avail(sb))
strbuf_grow(sb, 64);
va_start(ap, fmt);
len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
va_end(ap);
if (len < 0) {
len = 0;
}
if (len < 0)
die("your vsnprintf is broken");
if (len > strbuf_avail(sb)) {
strbuf_grow(sb, len);
va_start(ap, fmt);
Expand Down
4 changes: 2 additions & 2 deletions trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void trace_printf(const char *fmt, ...)
if (!fd)
return;

strbuf_init(&buf, 0);
strbuf_init(&buf, 64);
va_start(ap, fmt);
len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
va_end(ap);
Expand Down Expand Up @@ -103,7 +103,7 @@ void trace_argv_printf(const char **argv, int count, const char *fmt, ...)
if (!fd)
return;

strbuf_init(&buf, 0);
strbuf_init(&buf, 64);
va_start(ap, fmt);
len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
va_end(ap);
Expand Down

0 comments on commit f141bd8

Please sign in to comment.