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

strbuf change: be sure ->buf is never ever NULL.

For that purpose, the ->buf is always initialized with a char * buf living in the strbuf module. It is made a char * so that we can sloppily accept things that perform: sb->buf[0] = '\0', and because you can't pass "" as an initializer for ->buf without making gcc unhappy for very good reasons. strbuf_init/_detach/_grow have been fixed to trust ->alloc and not ->buf anymore. as a consequence strbuf_detach is _mandatory_ to detach a buffer, copying ->buf isn't an option anymore, if ->buf is going to escape from the scope, and eventually be free'd. API changes: * strbuf_setlen now always works, so just make strbuf_reset a convenience macro. * strbuf_detatch takes a size_t* optional argument (meaning it can be NULL) to copy the buffer's len, as it was needed for this refactor to make the code more readable, and working like the callers. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Sep 27, 2007 at 12:58 UTC b315c5c08139c0d3c1e4867a305334e29da01d07
13 files changed +48 -47
builtin-apply.c
+7 -9
@@ -178,14 +178,13 @@ static void say_patch_name(FILE *output, const char *pre, struct patch *patch, c
178 #define CHUNKSIZE (8192)
179 #define SLOP (16)
180
181 -static void *read_patch_file(int fd, unsigned long *sizep)
181 +static void *read_patch_file(int fd, size_t *sizep)
182 {
183 struct strbuf buf;
184
185 strbuf_init(&buf, 0);
186 if (strbuf_read(&buf, fd, 0) < 0)
187 die("git-apply: read returned %s", strerror(errno));
188 - *sizep = buf.len;
188
189 /*
190 * Make sure that we have some slop in the buffer
@@ -194,7 +193,7 @@ static void *read_patch_file(int fd, unsigned long *sizep)
193 */
194 strbuf_grow(&buf, SLOP);
195 memset(buf.buf + buf.len, 0, SLOP);
197 - return strbuf_detach(&buf);
196 + return strbuf_detach(&buf, sizep);
197 }
198
199 static unsigned long linelen(const char *buffer, unsigned long size)
@@ -253,7 +252,7 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
252 */
253 strbuf_remove(&name, 0, cp - name.buf);
254 free(def);
256 - return name.buf;
255 + return strbuf_detach(&name, NULL);
256 }
257 }
258 strbuf_release(&name);
@@ -607,7 +606,7 @@ static char *git_header_name(char *line, int llen)
606 if (strcmp(cp + 1, first.buf))
607 goto free_and_fail1;
608 strbuf_release(&sp);
610 - return first.buf;
609 + return strbuf_detach(&first, NULL);
610 }
611
612 /* unquoted second */
@@ -618,7 +617,7 @@ static char *git_header_name(char *line, int llen)
617 if (line + llen - cp != first.len + 1 ||
618 memcmp(first.buf, cp, first.len))
619 goto free_and_fail1;
621 - return first.buf;
620 + return strbuf_detach(&first, NULL);
621
622 free_and_fail1:
623 strbuf_release(&first);
@@ -655,7 +654,7 @@ static char *git_header_name(char *line, int llen)
654 isspace(name[len])) {
655 /* Good */
656 strbuf_remove(&sp, 0, np - sp.buf);
658 - return sp.buf;
657 + return strbuf_detach(&sp, NULL);
658 }
659
660 free_and_fail2:
@@ -1968,8 +1967,7 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
1967
1968 if (apply_fragments(&buf, patch) < 0)
1969 return -1; /* note with --reject this succeeds. */
1971 - patch->result = buf.buf;
1972 - patch->resultsize = buf.len;
1970 + patch->result = strbuf_detach(&buf, &patch->resultsize);
1971
1972 if (0 < patch->is_delete && patch->resultsize)
1973 return error("removal patch leaves file contents");
builtin-archive.c
+2 -3
@@ -89,7 +89,7 @@ static void format_subst(const struct commit *commit,
89 struct strbuf fmt;
90
91 if (src == buf->buf)
92 - to_free = strbuf_detach(buf);
92 + to_free = strbuf_detach(buf, NULL);
93 strbuf_init(&fmt, 0);
94 for (;;) {
95 const char *b, *c;
@@ -153,8 +153,7 @@ void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
153 strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
154 convert_to_working_tree(path, buf.buf, buf.len, &buf);
155 convert_to_archive(path, buf.buf, buf.len, &buf, commit);
156 - *sizep = buf.len;
157 - buffer = buf.buf;
156 + buffer = strbuf_detach(&buf, sizep);
157 }
158
159 return buffer;
builtin-fetch--tool.c
+1 -1
@@ -10,7 +10,7 @@ static char *get_stdin(void)
10 if (strbuf_read(&buf, 0, 1024) < 0) {
11 die("error reading standard input: %s", strerror(errno));
12 }
13 - return strbuf_detach(&buf);
13 + return strbuf_detach(&buf, NULL);
14 }
15
16 static void show_new(enum object_type type, unsigned char *sha1_new)
commit.c
+1 -1
@@ -663,7 +663,7 @@ static char *replace_encoding_header(char *buf, const char *encoding)
663 len - strlen("encoding \n"),
664 encoding, strlen(encoding));
665 }
666 - return tmp.buf;
666 + return strbuf_detach(&tmp, NULL);
667 }
668
669 static char *logmsg_reencode(const struct commit *commit,
convert.c
+2 -2
@@ -168,7 +168,7 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len,
168
169 /* are we "faking" in place editing ? */
170 if (src == buf->buf)
171 - to_free = strbuf_detach(buf);
171 + to_free = strbuf_detach(buf, NULL);
172
173 strbuf_grow(buf, len + stats.lf - stats.crlf);
174 for (;;) {
@@ -464,7 +464,7 @@ static int ident_to_worktree(const char *path, const char *src, size_t len,
464
465 /* are we "faking" in place editing ? */
466 if (src == buf->buf)
467 - to_free = strbuf_detach(buf);
467 + to_free = strbuf_detach(buf, NULL);
468 hash_sha1_file(src, len, "blob", sha1);
469
470 strbuf_grow(buf, len + cnt * 43);
diff.c
+6 -8
@@ -197,7 +197,7 @@ static char *quote_two(const char *one, const char *two)
197 strbuf_addstr(&res, one);
198 strbuf_addstr(&res, two);
199 }
200 - return res.buf;
200 + return strbuf_detach(&res, NULL);
201 }
202
203 static const char *external_diff(void)
@@ -662,7 +662,7 @@ static char *pprint_rename(const char *a, const char *b)
662 quote_c_style(a, &name, NULL, 0);
663 strbuf_addstr(&name, " => ");
664 quote_c_style(b, &name, NULL, 0);
665 - return name.buf;
665 + return strbuf_detach(&name, NULL);
666 }
667
668 /* Find common prefix */
@@ -710,7 +710,7 @@ static char *pprint_rename(const char *a, const char *b)
710 strbuf_addch(&name, '}');
711 strbuf_add(&name, a + len_a - sfx_length, sfx_length);
712 }
713 - return name.buf;
713 + return strbuf_detach(&name, NULL);
714 }
715
716 struct diffstat_t {
@@ -827,7 +827,7 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
827 strbuf_init(&buf, 0);
828 if (quote_c_style(file->name, &buf, NULL, 0)) {
829 free(file->name);
830 - file->name = buf.buf;
830 + file->name = strbuf_detach(&buf, NULL);
831 } else {
832 strbuf_release(&buf);
833 }
@@ -1519,8 +1519,7 @@ static int populate_from_stdin(struct diff_filespec *s)
1519 strerror(errno));
1520
1521 s->should_munmap = 0;
1522 - s->size = buf.len;
1523 - s->data = strbuf_detach(&buf);
1522 + s->data = strbuf_detach(&buf, &s->size);
1523 s->should_free = 1;
1524 return 0;
1525 }
@@ -1612,8 +1611,7 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
1611 if (convert_to_git(s->path, s->data, s->size, &buf)) {
1612 munmap(s->data, s->size);
1613 s->should_munmap = 0;
1615 - s->data = buf.buf;
1616 - s->size = buf.len;
1614 + s->data = strbuf_detach(&buf, &s->size);
1615 s->should_free = 1;
1616 }
1617 }
entry.c
+1 -2
@@ -120,8 +120,7 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
120 strbuf_init(&buf, 0);
121 if (convert_to_working_tree(ce->name, new, size, &buf)) {
122 free(new);
123 - new = buf.buf;
124 - size = buf.len;
123 + new = strbuf_detach(&buf, &size);
124 }
125
126 if (to_tempfile) {
fast-import.c
+1 -1
@@ -1562,7 +1562,7 @@ static int read_next_command(void)
1562 } else {
1563 struct recent_command *rc;
1564
1565 - strbuf_detach(&command_buf);
1565 + strbuf_detach(&command_buf, NULL);
1566 stdin_eof = strbuf_getline(&command_buf, stdin, '\n');
1567 if (stdin_eof)
1568 return EOF;
imap-send.c
+1 -1
@@ -1180,7 +1180,7 @@ read_message( FILE *f, msg_data_t *msg )
1180 } while (!feof(f));
1181
1182 msg->len = buf.len;
1183 - msg->data = strbuf_detach(&buf);
1183 + msg->data = strbuf_detach(&buf, NULL);
1184 return msg->len;
1185 }
1186
quote.c
+1 -1
@@ -22,7 +22,7 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
22 char *to_free = NULL;
23
24 if (dst->buf == src)
25 - to_free = strbuf_detach(dst);
25 + to_free = strbuf_detach(dst, NULL);
26
27 strbuf_addch(dst, '\'');
28 while (*src) {
sha1_file.c
+1 -2
@@ -2340,8 +2340,7 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2340 strbuf_init(&nbuf, 0);
2341 if (convert_to_git(path, buf, size, &nbuf)) {
2342 munmap(buf, size);
2343 - size = nbuf.len;
2344 - buf = nbuf.buf;
2343 + buf = strbuf_detach(&nbuf, &size);
2344 re_allocated = 1;
2345 }
2346 }
strbuf.c
+19 -11
@@ -1,27 +1,33 @@
1 #include "cache.h"
2
3 +/*
4 + * Used as the default ->buf value, so that people can always assume
5 + * buf is non NULL and ->buf is NUL terminated even for a freshly
6 + * initialized strbuf.
7 + */
8 +char strbuf_slopbuf[1];
9 +
10 void strbuf_init(struct strbuf *sb, size_t hint)
11 {
5 - memset(sb, 0, sizeof(*sb));
12 + sb->alloc = sb->len = 0;
13 + sb->buf = strbuf_slopbuf;
14 if (hint)
15 strbuf_grow(sb, hint);
16 }
17
18 void strbuf_release(struct strbuf *sb)
19 {
12 - free(sb->buf);
13 - memset(sb, 0, sizeof(*sb));
14 -}
15 -
16 -void strbuf_reset(struct strbuf *sb)
17 -{
18 - if (sb->len)
19 - strbuf_setlen(sb, 0);
20 + if (sb->alloc) {
21 + free(sb->buf);
22 + strbuf_init(sb, 0);
23 + }
24 }
25
22 -char *strbuf_detach(struct strbuf *sb)
26 +char *strbuf_detach(struct strbuf *sb, size_t *sz)
27 {
24 - char *res = sb->buf;
28 + char *res = sb->alloc ? sb->buf : NULL;
29 + if (sz)
30 + *sz = sb->len;
31 strbuf_init(sb, 0);
32 return res;
33 }
@@ -40,6 +46,8 @@ void strbuf_grow(struct strbuf *sb, size_t extra)
46 {
47 if (sb->len + extra + 1 <= sb->len)
48 die("you want to use way too much memory");
49 + if (!sb->alloc)
50 + sb->buf = NULL;
51 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
52 }
53
strbuf.h
+5 -5
@@ -10,8 +10,7 @@
10 * 1. the ->buf member is always malloc-ed, hence strbuf's can be used to
11 * build complex strings/buffers whose final size isn't easily known.
12 *
13 - * It is legal to copy the ->buf pointer away. Though if you want to reuse
14 - * the strbuf after that, setting ->buf to NULL isn't legal.
13 + * It is NOT legal to copy the ->buf pointer away.
14 * `strbuf_detach' is the operation that detachs a buffer from its shell
15 * while keeping the shell valid wrt its invariants.
16 *
@@ -41,19 +40,19 @@
40
41 #include <assert.h>
42
43 +extern char strbuf_slopbuf[];
44 struct strbuf {
45 size_t alloc;
46 size_t len;
47 char *buf;
48 };
49
50 -#define STRBUF_INIT { 0, 0, NULL }
50 +#define STRBUF_INIT { 0, 0, strbuf_slopbuf }
51
52 /*----- strbuf life cycle -----*/
53 extern void strbuf_init(struct strbuf *, size_t);
54 extern void strbuf_release(struct strbuf *);
55 -extern void strbuf_reset(struct strbuf *);
56 -extern char *strbuf_detach(struct strbuf *);
55 +extern char *strbuf_detach(struct strbuf *, size_t *);
56 extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
57 static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
58 struct strbuf tmp = *a;
@@ -75,6 +74,7 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
74 sb->len = len;
75 sb->buf[len] = '\0';
76 }
77 +#define strbuf_reset(sb) strbuf_setlen(sb, 0)
78
79 /*----- content related -----*/
80 extern void strbuf_rtrim(struct strbuf *);