diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2019-12-21 15:26:48 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2019-12-21 15:26:48 +0000 |
commit | 9f105418a1aa53cfb4a7fcad0ace3a921c3794fc (patch) | |
tree | 62a5d5fb81188866f1305f3a995428b1a212e291 /usr.bin/make | |
parent | 391e9419c2cf5c257ddf500eeb13411d2393f4ce (diff) |
tweak buffer handling a bit:
- make BufExpand a real function, zap BufOverflow
- sprinkle assert that justify the arithmetic
- use unsigned constants
- fix a bug in the unlikely condition where Buf_printf would exactly
match the buffer boundary and Buf_Retrieve would be called right after
okay millert@
Diffstat (limited to 'usr.bin/make')
-rw-r--r-- | usr.bin/make/buf.c | 59 | ||||
-rw-r--r-- | usr.bin/make/buf.h | 6 |
2 files changed, 29 insertions, 36 deletions
diff --git a/usr.bin/make/buf.c b/usr.bin/make/buf.c index 868653ac887..f5e3f8bd9b9 100644 --- a/usr.bin/make/buf.c +++ b/usr.bin/make/buf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buf.c,v 1.27 2015/04/29 00:42:12 millert Exp $ */ +/* $OpenBSD: buf.c,v 1.28 2019/12/21 15:26:47 espie Exp $ */ /* $NetBSD: buf.c,v 1.9 1996/12/31 17:53:21 christos Exp $ */ /* @@ -66,6 +66,7 @@ * Functions for automatically expanded buffers. */ +#include <assert.h> #include <ctype.h> #include <limits.h> #include <stddef.h> @@ -96,45 +97,37 @@ fatal_overflow() exit(2); } -/* BufExpand(bp, nb) - * Expand buffer bp to hold upto nb additional - * chars. Makes sure there's room for an extra '\0' char at - * the end of the buffer to terminate the string. */ -#define BufExpand(bp,nb) \ -do { \ - size_t occupied = (bp)->inPtr - (bp)->buffer; \ - size_t size = (bp)->endPtr - (bp)->buffer; \ - DO_STAT_BUF(bp, nb); \ - \ - do { \ - if (size <= SIZE_MAX/2) { \ - size *= 2 ; \ - } else { \ - fatal_overflow(); \ - } \ - } while (size - occupied < (nb)+1+BUF_MARGIN); \ - (bp)->buffer = (bp)->inPtr = (bp)->endPtr = \ - erealloc((bp)->buffer, size); \ - (bp)->inPtr += occupied; \ - (bp)->endPtr += size; \ -} while (0); - -#define BUF_DEF_SIZE 256 /* Default buffer size */ -#define BUF_MARGIN 256 /* Make sure we are comfortable */ +#define BUF_DEF_SIZE 256U /* Default buffer size */ +#define BUF_MARGIN 256U /* Make sure we are comfortable */ -/* the hard case for Buf_AddChar: buffer must be expanded to accommodate - * one more char. */ +/* BufExpand(bp, nb) + * Expand buffer bp to hold up to nb additional + * chars. Makes sure there's always room for an extra '\0' char + * at the end of the buffer for Buf_Retrieve. */ void -BufOverflow(Buffer bp) +BufExpand(Buffer bp, size_t nb) { - BufExpand(bp, 1); -} + size_t occupied = bp->inPtr - bp->buffer; + size_t size = bp->endPtr - bp->buffer; + DO_STAT_BUF(bp, nb); + do { + if (size <= SIZE_MAX/2) { + size *= 2 ; + } else { + fatal_overflow(); + } + assert(size >= occupied); + } while (size - occupied < nb+1+BUF_MARGIN); + bp->buffer = erealloc(bp->buffer, size); + bp->inPtr = bp->buffer +occupied; + bp->endPtr = bp->buffer + size; +} void Buf_AddChars(Buffer bp, size_t numBytes, const char *bytesPtr) { - + assert(bp->endPtr >= bp->inPtr); if ((size_t)(bp->endPtr - bp->inPtr) < numBytes+1) BufExpand(bp, numBytes); @@ -150,7 +143,7 @@ Buf_printf(Buffer bp, const char *fmt, ...) va_start(va, fmt); n = vsnprintf(bp->inPtr, bp->endPtr - bp->inPtr, fmt, va); va_end(va); - if (n > bp->endPtr - bp->inPtr) { + if (n+1 > bp->endPtr - bp->inPtr) { va_list vb; BufExpand(bp, n); va_start(vb, fmt); diff --git a/usr.bin/make/buf.h b/usr.bin/make/buf.h index 018002c2397..6a31142b289 100644 --- a/usr.bin/make/buf.h +++ b/usr.bin/make/buf.h @@ -1,7 +1,7 @@ #ifndef _BUF_H #define _BUF_H -/* $OpenBSD: buf.h,v 1.22 2012/11/07 14:18:41 espie Exp $ */ +/* $OpenBSD: buf.h,v 1.23 2019/12/21 15:26:47 espie Exp $ */ /* $NetBSD: buf.h,v 1.7 1996/12/31 17:53:22 christos Exp $ */ /* @@ -85,7 +85,7 @@ typedef struct Buffer_ { } BUFFER; /* Internal support for Buf_AddChar. */ -extern void BufOverflow(Buffer); +extern void BufExpand(Buffer, size_t); /* User interface */ @@ -117,7 +117,7 @@ extern void Buf_Init(Buffer, size_t); #define Buf_AddChar(bp, byte) \ do { \ if ((bp)->endPtr - (bp)->inPtr <= 1) \ - BufOverflow(bp); \ + BufExpand(bp, 1); \ *(bp)->inPtr++ = (byte); \ } while (0) |