Permalink
Browse files

Corruption in vbufprintf

  • Loading branch information...
1 parent 5cf7c86 commit 86b851a55b12812df4d3b874b73097447799bdeb @andre-d committed Nov 1, 2011
Showing with 27 additions and 29 deletions.
  1. +27 −26 src/buffer.c
  2. +0 −3 src/buffer.h
View
@@ -113,9 +113,35 @@ bufprintf(struct buf *buf, const char *fmt, ...)
if (!buf || !buf->unit)
return;
+ int n;
+
+ if (buf == 0 || (buf->size >= buf->asize && bufgrow(buf, buf->size + 1)) < 0)
+ return;
+
va_start(ap, fmt);
- vbufprintf(buf, fmt, ap);
+ n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
va_end(ap);
+
+ if (n < 0) {
+#ifdef _MSC_VER
+ n = _vscprintf(fmt, ap);
+#else
+ return;
+#endif
+ }
+
+ if ((size_t)n >= buf->asize - buf->size) {
+ if (bufgrow(buf, buf->size + n + 1) < 0)
+ return;
+ va_start(ap, fmt);
+ n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
+ va_end(ap);
+ }
+
+ if (n < 0)
+ return;
+
+ buf->size += n;
}
/* bufput: appends raw data to a buffer */
@@ -198,31 +224,6 @@ bufslurp(struct buf *buf, size_t len)
void
vbufprintf(struct buf *buf, const char *fmt, va_list ap)
{
- int n;
- if (buf == 0 || (buf->size >= buf->asize && bufgrow(buf, buf->size + 1)) < 0)
- return;
-
- n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
-
- if (n < 0) {
-#ifdef _MSC_VER
- n = _vscprintf(fmt, ap);
-#else
- return;
-#endif
- }
-
- if ((size_t)n >= buf->asize - buf->size) {
- if (bufgrow(buf, buf->size + n + 1) < 0)
- return;
-
- n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
- }
-
- if (n < 0)
- return;
-
- buf->size += n;
}
View
@@ -85,7 +85,4 @@ void bufslurp(struct buf *, size_t);
/* bufprintf: formatted printing to a buffer */
void bufprintf(struct buf *, const char *, ...) __attribute__ ((format (printf, 2, 3)));
-/* vbufprintf: stdarg variant of formatted printing into a buffer */
-void vbufprintf(struct buf *, const char * , va_list);
-
#endif

0 comments on commit 86b851a

Please sign in to comment.