Skip to content

Commit b449f4c

Browse files
MadCodergitster
authored andcommitted
Rework strbuf API and semantics.
The gory details are explained in strbuf.h. The change of semantics this patch enforces is that the embeded buffer has always a '\0' character after its last byte, to always make it a C-string. The offs-by-one changes are all related to that very change. A strbuf can be used to store byte arrays, or as an extended string library. The `buf' member can be passed to any C legacy string function, because strbuf operations always ensure there is a terminating \0 at the end of the buffer, not accounted in the `len' field of the structure. A strbuf can be used to generate a string/buffer whose final size is not really known, and then "strbuf_detach" can be used to get the built buffer, and keep the wrapping "strbuf" structure usable for further work again. Other interesting feature: strbuf_grow(sb, size) ensure that there is enough allocated space in `sb' to put `size' new octets of data in the buffer. It helps avoiding reallocating data for nothing when the problem the strbuf helps to solve has a known typical size. Signed-off-by: Pierre Habouzit <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b5ef6ac commit b449f4c

File tree

5 files changed

+180
-28
lines changed

5 files changed

+180
-28
lines changed

archive-tar.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
166166
sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
167167
} else {
168168
if (verbose)
169-
fprintf(stderr, "%.*s\n", path->len, path->buf);
169+
fprintf(stderr, "%.*s\n", (int)path->len, path->buf);
170170
if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
171171
*header.typeflag = TYPEFLAG_DIR;
172172
mode = (mode | 0777) & ~tar_umask;

fast-import.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ static void read_next_command(void)
15951595
} else {
15961596
struct recent_command *rc;
15971597

1598-
command_buf.buf = NULL;
1598+
strbuf_detach(&command_buf);
15991599
read_line(&command_buf, stdin, '\n');
16001600
if (command_buf.eof)
16011601
return;
@@ -1649,19 +1649,18 @@ static void *cmd_data (size_t *size)
16491649
size_t sz = 8192, term_len = command_buf.len - 5 - 2;
16501650
length = 0;
16511651
buffer = xmalloc(sz);
1652-
command_buf.buf = NULL;
16531652
for (;;) {
16541653
read_line(&command_buf, stdin, '\n');
16551654
if (command_buf.eof)
16561655
die("EOF in data (terminator '%s' not found)", term);
16571656
if (term_len == command_buf.len
16581657
&& !strcmp(term, command_buf.buf))
16591658
break;
1660-
ALLOC_GROW(buffer, length + command_buf.len, sz);
1659+
ALLOC_GROW(buffer, length + command_buf.len + 1, sz);
16611660
memcpy(buffer + length,
16621661
command_buf.buf,
1663-
command_buf.len - 1);
1664-
length += command_buf.len - 1;
1662+
command_buf.len);
1663+
length += command_buf.len;
16651664
buffer[length++] = '\n';
16661665
}
16671666
free(term);
@@ -2101,7 +2100,7 @@ static void cmd_new_commit(void)
21012100
}
21022101

21032102
/* file_change* */
2104-
while (!command_buf.eof && command_buf.len > 1) {
2103+
while (!command_buf.eof && command_buf.len > 0) {
21052104
if (!prefixcmp(command_buf.buf, "M "))
21062105
file_change_m(b);
21072106
else if (!prefixcmp(command_buf.buf, "D "))
@@ -2256,7 +2255,7 @@ static void cmd_reset_branch(void)
22562255
else
22572256
b = new_branch(sp);
22582257
read_next_command();
2259-
if (!cmd_from(b) && command_buf.len > 1)
2258+
if (!cmd_from(b) && command_buf.len > 0)
22602259
unread_command_buf = 1;
22612260
}
22622261

@@ -2273,7 +2272,7 @@ static void cmd_checkpoint(void)
22732272

22742273
static void cmd_progress(void)
22752274
{
2276-
fwrite(command_buf.buf, 1, command_buf.len - 1, stdout);
2275+
fwrite(command_buf.buf, 1, command_buf.len, stdout);
22772276
fputc('\n', stdout);
22782277
fflush(stdout);
22792278
skip_optional_lf();

mktree.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ int main(int ac, char **av)
9292

9393
strbuf_init(&sb);
9494
while (1) {
95-
int len;
9695
char *ptr, *ntr;
9796
unsigned mode;
9897
enum object_type type;
@@ -101,7 +100,6 @@ int main(int ac, char **av)
101100
read_line(&sb, stdin, line_termination);
102101
if (sb.eof)
103102
break;
104-
len = sb.len;
105103
ptr = sb.buf;
106104
/* Input is non-recursive ls-tree output format
107105
* mode SP type SP sha1 TAB name
@@ -111,7 +109,7 @@ int main(int ac, char **av)
111109
die("input format error: %s", sb.buf);
112110
ptr = ntr + 1; /* type */
113111
ntr = strchr(ptr, ' ');
114-
if (!ntr || sb.buf + len <= ntr + 41 ||
112+
if (!ntr || sb.buf + sb.len <= ntr + 40 ||
115113
ntr[41] != '\t' ||
116114
get_sha1_hex(ntr + 1, sha1))
117115
die("input format error: %s", sb.buf);

strbuf.c

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,113 @@
22
#include "strbuf.h"
33

44
void strbuf_init(struct strbuf *sb) {
5-
sb->buf = NULL;
6-
sb->eof = sb->alloc = sb->len = 0;
5+
memset(sb, 0, sizeof(*sb));
76
}
87

9-
static void strbuf_begin(struct strbuf *sb) {
8+
void strbuf_release(struct strbuf *sb) {
109
free(sb->buf);
10+
memset(sb, 0, sizeof(*sb));
11+
}
12+
13+
void strbuf_reset(struct strbuf *sb) {
14+
if (sb->len)
15+
strbuf_setlen(sb, 0);
16+
sb->eof = 0;
17+
}
18+
19+
char *strbuf_detach(struct strbuf *sb) {
20+
char *res = sb->buf;
1121
strbuf_init(sb);
22+
return res;
23+
}
24+
25+
void strbuf_grow(struct strbuf *sb, size_t extra) {
26+
if (sb->len + extra + 1 <= sb->len)
27+
die("you want to use way too much memory");
28+
ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
29+
}
30+
31+
void strbuf_add(struct strbuf *sb, const void *data, size_t len) {
32+
strbuf_grow(sb, len);
33+
memcpy(sb->buf + sb->len, data, len);
34+
strbuf_setlen(sb, sb->len + len);
35+
}
36+
37+
void strbuf_addf(struct strbuf *sb, const char *fmt, ...) {
38+
int len;
39+
va_list ap;
40+
41+
va_start(ap, fmt);
42+
len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
43+
va_end(ap);
44+
if (len < 0) {
45+
len = 0;
46+
}
47+
if (len >= strbuf_avail(sb)) {
48+
strbuf_grow(sb, len);
49+
va_start(ap, fmt);
50+
len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
51+
va_end(ap);
52+
if (len >= strbuf_avail(sb)) {
53+
die("this should not happen, your snprintf is broken");
54+
}
55+
}
56+
strbuf_setlen(sb, sb->len + len);
1257
}
1358

14-
static void inline strbuf_add(struct strbuf *sb, int ch) {
15-
if (sb->alloc <= sb->len) {
16-
sb->alloc = sb->alloc * 3 / 2 + 16;
17-
sb->buf = xrealloc(sb->buf, sb->alloc);
59+
size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f) {
60+
size_t res;
61+
62+
strbuf_grow(sb, size);
63+
res = fread(sb->buf + sb->len, 1, size, f);
64+
if (res > 0) {
65+
strbuf_setlen(sb, sb->len + res);
1866
}
19-
sb->buf[sb->len++] = ch;
67+
return res;
2068
}
2169

22-
static void strbuf_end(struct strbuf *sb) {
23-
strbuf_add(sb, 0);
70+
ssize_t strbuf_read(struct strbuf *sb, int fd)
71+
{
72+
size_t oldlen = sb->len;
73+
74+
for (;;) {
75+
ssize_t cnt;
76+
77+
strbuf_grow(sb, 8192);
78+
cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
79+
if (cnt < 0) {
80+
strbuf_setlen(sb, oldlen);
81+
return -1;
82+
}
83+
if (!cnt)
84+
break;
85+
sb->len += cnt;
86+
}
87+
88+
sb->buf[sb->len] = '\0';
89+
return sb->len - oldlen;
2490
}
2591

2692
void read_line(struct strbuf *sb, FILE *fp, int term) {
2793
int ch;
28-
strbuf_begin(sb);
2994
if (feof(fp)) {
95+
strbuf_release(sb);
3096
sb->eof = 1;
3197
return;
3298
}
99+
100+
strbuf_reset(sb);
33101
while ((ch = fgetc(fp)) != EOF) {
34102
if (ch == term)
35103
break;
36-
strbuf_add(sb, ch);
104+
strbuf_grow(sb, 1);
105+
sb->buf[sb->len++] = ch;
37106
}
38-
if (ch == EOF && sb->len == 0)
107+
if (ch == EOF && sb->len == 0) {
108+
strbuf_release(sb);
39109
sb->eof = 1;
40-
strbuf_end(sb);
110+
}
111+
112+
strbuf_grow(sb, 1);
113+
sb->buf[sb->len] = '\0';
41114
}

strbuf.h

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,95 @@
11
#ifndef STRBUF_H
22
#define STRBUF_H
3+
4+
/*
5+
* Strbuf's can be use in many ways: as a byte array, or to store arbitrary
6+
* long, overflow safe strings.
7+
*
8+
* Strbufs has some invariants that are very important to keep in mind:
9+
*
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.
15+
* `strbuf_detach' is the operation that detachs a buffer from its shell
16+
* while keeping the shell valid wrt its invariants.
17+
*
18+
* 2. the ->buf member is a byte array that has at least ->len + 1 bytes
19+
* allocated. The extra byte is used to store a '\0', allowing the ->buf
20+
* member to be a valid C-string. Every strbuf function ensure this
21+
* invariant is preserved.
22+
*
23+
* Note that it is OK to "play" with the buffer directly if you work it
24+
* that way:
25+
*
26+
* strbuf_grow(sb, SOME_SIZE);
27+
* // ... here the memory areay starting at sb->buf, and of length
28+
* // sb_avail(sb) is all yours, and you are sure that sb_avail(sb) is at
29+
* // least SOME_SIZE
30+
* strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
31+
*
32+
* Of course, SOME_OTHER_SIZE must be smaller or equal to sb_avail(sb).
33+
*
34+
* Doing so is safe, though if it has to be done in many places, adding the
35+
* missing API to the strbuf module is the way to go.
36+
*
37+
* XXX: do _not_ assume that the area that is yours is of size ->alloc - 1
38+
* even if it's true in the current implementation. Alloc is somehow a
39+
* "private" member that should not be messed with.
40+
*/
41+
42+
#include <assert.h>
43+
344
struct strbuf {
4-
int alloc;
5-
int len;
45+
size_t alloc;
46+
size_t len;
647
int eof;
748
char *buf;
849
};
950

51+
#define STRBUF_INIT { 0, 0, 0, NULL }
52+
53+
/*----- strbuf life cycle -----*/
1054
extern void strbuf_init(struct strbuf *);
55+
extern void strbuf_release(struct strbuf *);
56+
extern void strbuf_reset(struct strbuf *);
57+
extern char *strbuf_detach(struct strbuf *);
58+
59+
/*----- strbuf size related -----*/
60+
static inline size_t strbuf_avail(struct strbuf *sb) {
61+
return sb->alloc ? sb->alloc - sb->len - 1 : 0;
62+
}
63+
static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
64+
assert (len < sb->alloc);
65+
sb->len = len;
66+
sb->buf[len] = '\0';
67+
}
68+
69+
extern void strbuf_grow(struct strbuf *, size_t);
70+
71+
/*----- add data in your buffer -----*/
72+
static inline void strbuf_addch(struct strbuf *sb, int c) {
73+
strbuf_grow(sb, 1);
74+
sb->buf[sb->len++] = c;
75+
sb->buf[sb->len] = '\0';
76+
}
77+
78+
extern void strbuf_add(struct strbuf *, const void *, size_t);
79+
static inline void strbuf_addstr(struct strbuf *sb, const char *s) {
80+
strbuf_add(sb, s, strlen(s));
81+
}
82+
static inline void strbuf_addbuf(struct strbuf *sb, struct strbuf *sb2) {
83+
strbuf_add(sb, sb2->buf, sb2->len);
84+
}
85+
86+
__attribute__((format(printf,2,3)))
87+
extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
88+
89+
extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
90+
/* XXX: if read fails, any partial read is undone */
91+
extern ssize_t strbuf_read(struct strbuf *, int fd);
92+
1193
extern void read_line(struct strbuf *, FILE *, int);
1294

1395
#endif /* STRBUF_H */

0 commit comments

Comments
 (0)