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.1.1~5 X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=commitdiff_plain;h=365cdf77999caa4f9edb02cc954b1075d57f299e;hp=b80d372819c8c909455ca5868f6d7412dfbbef66 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. --- diff --git a/src/wrbuf.c b/src/wrbuf.c index e57aab7..fa42894 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; } @@ -274,7 +273,7 @@ const char *wrbuf_cstr_null(WRBUF b) { if (!b || b->pos == 0) return 0; - assert(b->pos < b->size); + assert(b->pos <= b->size); b->buf[b->pos] = '\0'; return b->buf; }