wrbuf_cstr bit more thread safe YAZ-753
authorHeikki Levanto <heikki@indexdata.dk>
Tue, 8 Apr 2014 11:44:58 +0000 (13:44 +0200)
committerHeikki Levanto <heikki@indexdata.dk>
Tue, 8 Apr 2014 11:44:58 +0000 (13:44 +0200)
Now it does not mess with the pos. Still not 100% safe, can have
a race condition when reallocating the buf, if it needs room for
the terminating null byte.

src/wrbuf.c

index 972acff..7015bad 100644 (file)
@@ -16,6 +16,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
+#include <assert.h>
 
 #include <yaz/wrbuf.h>
 #include <yaz/snprintf.h>
 
 #include <yaz/wrbuf.h>
 #include <yaz/snprintf.h>
@@ -262,8 +263,9 @@ void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd)
 
 const char *wrbuf_cstr(WRBUF b)
 {
 
 const char *wrbuf_cstr(WRBUF b)
 {
-    wrbuf_putc(b, '\0');   /* add '\0' */
-    (b->pos)--;           /* don't include '\0' in count */
+    if (b->pos >= b->size)
+        wrbuf_grow(b, 1);
+    b->buf[b->pos] = '\0';
     return b->buf;
 }
 
     return b->buf;
 }