Subject: Re: [PATCH] strbuf: allocate enough space when strbuf_setlen() is called first time

From: Nguyen Thai Ngoc Duy <[email-masked]>
Date: Tue, 26 Apr 2011 22:09:33 +0700

On Tue, Apr 26, 2011 at 07:24:20PM +0700, Nguyễn Thái Ngọc Duy wrote:
>  static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
>  	if (!sb->alloc)
> -		strbuf_grow(sb, 0);
> +		strbuf_grow(sb, len);
>  	assert(len < sb->alloc);
>  	sb->len = len;
>  	sb->buf[len] = '\0';

This should be better. But I fail to see why it corrupts memory :(

@@ -33,9 +33,8 @@ static inline size_t strbuf_avail(const struct strbuf *sb) {
 extern void strbuf_grow(struct strbuf *, size_t);
 
 static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
-	if (!sb->alloc)
-		strbuf_grow(sb, 0);
-	assert(len < sb->alloc);
+	if (sb->alloc < len + 1)
+		strbuf_grow(sb, len - sb->alloc);
 	sb->len = len;
 	sb->buf[len] = '\0';
 }

-- 
Duy