Re: [PATCH v2 3/4] strbuf_setlen: don't write to strbuf_slopbuf
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Aug 23, 2017, 21:04 UTC
- Message-ID
- <xmqqh8wyxag1.fsf@gitster.mtv.corp.google.com>
- In-Reply-To
- <CA+sFfMdXv+nqpXmwfLTHtkRLuGkAEAwWXZCvOryVZ=aLb_UmbA@mail.gmail.com>
Brandon Casey <drafnel@gmail.com> writes:
> So is there any reason why didn't do something like the following in > the first place?
My guess is that we didn't bother; if we cared, we would have used a single instance of const char in a read-only segment, instead of such a macro.
Show 25 quoted lines
> diff --git a/strbuf.h b/strbuf.h
> index e705b94..fcca618 100644
> --- a/strbuf.h
> +++ b/strbuf.h
> @@ -67,7 +67,7 @@ struct strbuf {
> char *buf;
> };
>
> -extern char strbuf_slopbuf[];
> +#define strbuf_slopbuf ""
> #define STRBUF_INIT { .alloc = 0, .len = 0, .buf = strbuf_slopbuf }
>
> /**
> @@ -147,7 +147,9 @@ static inline void strbuf_setlen(struct strbuf
> *sb, size_t len)
> if (len > (sb->alloc ? sb->alloc - 1 : 0))
> die("BUG: strbuf_setlen() beyond buffer");
> sb->len = len;
> - sb->buf[len] = '\0';
> + if (sb->alloc) {
> + sb->buf[len] = '\0';
> + }
> }
>
> -Brandon