summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Junio C Hamano <gitster@pobox.com>2007-09-26 02:26:06 -0700
committerLibravatar Junio C Hamano <gitster@pobox.com>2007-09-26 02:27:05 -0700
commita8f3e2219c237661a30b54fe23d58e055f0b548c (patch)
tree440d74bf40b2ac091d625edeb72b43e47609e59a
parentsq_quote_argv and add_to_string rework with strbuf's. (diff)
downloadtgif-a8f3e2219c237661a30b54fe23d58e055f0b548c.tar.xz
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>
-rw-r--r--strbuf.h9
1 files changed, 6 insertions, 3 deletions
diff --git a/strbuf.h b/strbuf.h
index 567e2d17d1..3b19de3048 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -65,14 +65,17 @@ static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
static inline size_t strbuf_avail(struct strbuf *sb) {
return sb->alloc ? sb->alloc - sb->len - 1 : 0;
}
+
+extern void strbuf_grow(struct strbuf *, size_t);
+
static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
- assert (len < sb->alloc);
+ if (!sb->alloc)
+ strbuf_grow(sb, 0);
+ assert(len < sb->alloc);
sb->len = len;
sb->buf[len] = '\0';
}
-extern void strbuf_grow(struct strbuf *, size_t);
-
/*----- content related -----*/
extern void strbuf_rtrim(struct strbuf *);