Skip to content

Commit 7e8089c

Browse files
peffgitster
authored andcommitted
test-hashmap: use strbuf_getline rather than fgets
Using fgets() with a fixed-size buffer can lead to lines being accidentally split across two calls if they are larger than the buffer size. As this is just a test helper, this is unlikely to be a problem in practice. But since people may look at test helpers as reference code, it's a good idea for them to model the preferred behavior. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent cbadf0e commit 7e8089c

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

t/helper/test-hashmap.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "git-compat-util.h"
22
#include "hashmap.h"
3+
#include "strbuf.h"
34

45
struct test_entry
56
{
@@ -143,7 +144,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
143144
*/
144145
int cmd_main(int argc, const char **argv)
145146
{
146-
char line[1024];
147+
struct strbuf line = STRBUF_INIT;
147148
struct hashmap map;
148149
int icase;
149150

@@ -152,13 +153,13 @@ int cmd_main(int argc, const char **argv)
152153
hashmap_init(&map, test_entry_cmp, &icase, 0);
153154

154155
/* process commands from stdin */
155-
while (fgets(line, sizeof(line), stdin)) {
156+
while (strbuf_getline(&line, stdin) != EOF) {
156157
char *cmd, *p1 = NULL, *p2 = NULL;
157158
int l1 = 0, l2 = 0, hash = 0;
158159
struct test_entry *entry;
159160

160161
/* break line into command and up to two parameters */
161-
cmd = strtok(line, DELIM);
162+
cmd = strtok(line.buf, DELIM);
162163
/* ignore empty lines */
163164
if (!cmd || *cmd == '#')
164165
continue;
@@ -262,6 +263,7 @@ int cmd_main(int argc, const char **argv)
262263
}
263264
}
264265

266+
strbuf_release(&line);
265267
hashmap_free(&map, 1);
266268
return 0;
267269
}

0 commit comments

Comments
 (0)