@matheusbsbs / matheus-bagliano / commits / a8f3e2219c
Fork 0

strbuf_setlen(): do not barf on setting length of an empty buffer to 0

strbuf_setlen() expect to be able to NUL terminate the buffer, but a completely empty strbuf could have an empty buffer with 0 allocation; both the assert() and the assignment for NUL termination would fail. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 26, 2007 at 02:26 UTC a8f3e2219c237661a30b54fe23d58e055f0b548c
1 file changed +6 -3
strbuf.h
+6 -3
@@ -65,14 +65,17 @@ static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
65 static inline size_t strbuf_avail(struct strbuf *sb) {
66 return sb->alloc ? sb->alloc - sb->len - 1 : 0;
67 }
68 +
69 +extern void strbuf_grow(struct strbuf *, size_t);
70 +
71 static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
69 - assert (len < sb->alloc);
72 + if (!sb->alloc)
73 + strbuf_grow(sb, 0);
74 + assert(len < sb->alloc);
75 sb->len = len;
76 sb->buf[len] = '\0';
77 }
78
74 -extern void strbuf_grow(struct strbuf *, size_t);
75 -
79 /*----- content related -----*/
80 extern void strbuf_rtrim(struct strbuf *);
81