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>
Jeff King committed
Feb 14, 2018 at 13:07 UTC
7e8089c986790fd8ef9d89bf71c9a91901d7f884
1 file changed
+5
-3
t/helper/test-hashmap.c
+5
-3
@@ -1,5 +1,6 @@
1
#include "git-compat-util.h"
2
#include "hashmap.h"
3
+#include "strbuf.h"
4
5
struct test_entry
6
{
@@ -143,7 +144,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
144
*/
145
int cmd_main(int argc, const char **argv)
146
{
146
- char line[1024];
147
+ struct strbuf line = STRBUF_INIT;
148
struct hashmap map;
149
int icase;
150
@@ -152,13 +153,13 @@ int cmd_main(int argc, const char **argv)
153
hashmap_init(&map, test_entry_cmp, &icase, 0);
154
155
/* process commands from stdin */
155
- while (fgets(line, sizeof(line), stdin)) {
156
+ while (strbuf_getline(&line, stdin) != EOF) {
157
char *cmd, *p1 = NULL, *p2 = NULL;
158
int l1 = 0, l2 = 0, hash = 0;
159
struct test_entry *entry;
160
161
/* break line into command and up to two parameters */
161
- cmd = strtok(line, DELIM);
162
+ cmd = strtok(line.buf, DELIM);
163
/* ignore empty lines */
164
if (!cmd || *cmd == '#')
165
continue;
@@ -262,6 +263,7 @@ int cmd_main(int argc, const char **argv)
263
}
264
}
265
266
+ strbuf_release(&line);
267
hashmap_free(&map, 1);
268
return 0;
269
}