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>
This commit is contained in:
parent
cbadf0ee37
commit
7e8089c986
@ -1,5 +1,6 @@
|
|||||||
#include "git-compat-util.h"
|
#include "git-compat-util.h"
|
||||||
#include "hashmap.h"
|
#include "hashmap.h"
|
||||||
|
#include "strbuf.h"
|
||||||
|
|
||||||
struct test_entry
|
struct test_entry
|
||||||
{
|
{
|
||||||
@ -143,7 +144,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
|
|||||||
*/
|
*/
|
||||||
int cmd_main(int argc, const char **argv)
|
int cmd_main(int argc, const char **argv)
|
||||||
{
|
{
|
||||||
char line[1024];
|
struct strbuf line = STRBUF_INIT;
|
||||||
struct hashmap map;
|
struct hashmap map;
|
||||||
int icase;
|
int icase;
|
||||||
|
|
||||||
@ -152,13 +153,13 @@ int cmd_main(int argc, const char **argv)
|
|||||||
hashmap_init(&map, test_entry_cmp, &icase, 0);
|
hashmap_init(&map, test_entry_cmp, &icase, 0);
|
||||||
|
|
||||||
/* process commands from stdin */
|
/* process commands from stdin */
|
||||||
while (fgets(line, sizeof(line), stdin)) {
|
while (strbuf_getline(&line, stdin) != EOF) {
|
||||||
char *cmd, *p1 = NULL, *p2 = NULL;
|
char *cmd, *p1 = NULL, *p2 = NULL;
|
||||||
int l1 = 0, l2 = 0, hash = 0;
|
int l1 = 0, l2 = 0, hash = 0;
|
||||||
struct test_entry *entry;
|
struct test_entry *entry;
|
||||||
|
|
||||||
/* break line into command and up to two parameters */
|
/* break line into command and up to two parameters */
|
||||||
cmd = strtok(line, DELIM);
|
cmd = strtok(line.buf, DELIM);
|
||||||
/* ignore empty lines */
|
/* ignore empty lines */
|
||||||
if (!cmd || *cmd == '#')
|
if (!cmd || *cmd == '#')
|
||||||
continue;
|
continue;
|
||||||
@ -262,6 +263,7 @@ int cmd_main(int argc, const char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
strbuf_release(&line);
|
||||||
hashmap_free(&map, 1);
|
hashmap_free(&map, 1);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user