Skip to content

Commit

Permalink
strbuf_setlen(): do not barf on setting length of an empty buffer to 0
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
gitster committed Sep 26, 2007
1 parent 7a33bcb commit a8f3e22
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions strbuf.h
Expand Up @@ -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 *);

Expand Down

0 comments on commit a8f3e22

Please sign in to comment.