@matheusbsbs / matheus-bagliano / commits / f141bd804d
Fork 0

Handle broken vsnprintf implementations in strbuf

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>

Shawn O. Pearce committed Nov 13, 2007 at 03:22 UTC f141bd804d2cba4500769934f9d9e7f0f7bcf282
2 files changed +6 -5
strbuf.c
+4 -3
@@ -111,12 +111,13 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
111 int len;
112 va_list ap;
113
114 + if (!strbuf_avail(sb))
115 + strbuf_grow(sb, 64);
116 va_start(ap, fmt);
117 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
118 va_end(ap);
117 - if (len < 0) {
118 - len = 0;
119 - }
119 + if (len < 0)
120 + die("your vsnprintf is broken");
121 if (len > strbuf_avail(sb)) {
122 strbuf_grow(sb, len);
123 va_start(ap, fmt);
trace.c
+2 -2
@@ -72,7 +72,7 @@ void trace_printf(const char *fmt, ...)
72 if (!fd)
73 return;
74
75 - strbuf_init(&buf, 0);
75 + strbuf_init(&buf, 64);
76 va_start(ap, fmt);
77 len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
78 va_end(ap);
@@ -103,7 +103,7 @@ void trace_argv_printf(const char **argv, int count, const char *fmt, ...)
103 if (!fd)
104 return;
105
106 - strbuf_init(&buf, 0);
106 + strbuf_init(&buf, 64);
107 va_start(ap, fmt);
108 len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
109 va_end(ap);