From: Adam Dickmeiss Date: Fri, 18 Apr 2014 17:40:51 +0000 (+0200) Subject: Fix wrbuf_putc + wrbuf_cstr assert YAZ-760 X-Git-Tag: v5.0.24~3 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=169256b8c16c13191e2627f49441c1b76809e02b;p=yaz-moved-to-github.git Fix wrbuf_putc + wrbuf_cstr assert YAZ-760 Change the meaning of WRBUF size. Just allocate size + 1 instead of size. This means we can safely store \0 in pos. Simplify xrealloc use in wrbuf_grow. Conflicts: src/wrbuf.c --- diff --git a/src/wrbuf.c b/src/wrbuf.c index 3c63170..21401c7 100644 --- a/src/wrbuf.c +++ b/src/wrbuf.c @@ -58,9 +58,8 @@ int wrbuf_grow(WRBUF b, size_t minsize) togrow = b->size; if (togrow < minsize) togrow = minsize; - if (b->size && !(b->buf =(char *)xrealloc(b->buf, b->size += togrow))) - abort(); - else if (!b->size && !(b->buf = (char *)xmalloc(b->size = togrow))) + b->buf = (char *) xrealloc(b->buf, 1 + (b->size += togrow)); + if (!b->buf) abort(); return 0; } @@ -70,7 +69,7 @@ void wrbuf_write(WRBUF b, const char *buf, size_t size) if (size <= 0) return; if (b->pos + size >= b->size) - wrbuf_grow(b, size + 1); + wrbuf_grow(b, size); memcpy(b->buf + b->pos, buf, size); b->pos += size; } @@ -80,7 +79,7 @@ void wrbuf_insert(WRBUF b, size_t pos, const char *buf, size_t size) if (size <= 0 || pos > b->pos) return; if (b->pos + size >= b->size) - wrbuf_grow(b, size + 1); + wrbuf_grow(b, size); memmove(b->buf + pos + size, b->buf + pos, b->pos - pos); memcpy(b->buf + pos, buf, size); b->pos += size; @@ -265,7 +264,7 @@ const char *wrbuf_cstr(WRBUF b) { if (b->pos == 0) return ""; - assert(b->pos < b->size); + assert(b->pos <= b->size); b->buf[b->pos] = '\0'; return b->buf; }