Skip to content

Commit 826f0e3

Browse files
ttaylorrgitster
authored andcommitted
t/helper/test-hashmap.c: avoid using strtok()
Avoid using the non-reentrant `strtok()` to separate the parts of each incoming command. Instead of replacing it with `strtok_r()`, let's instead use the more friendly pair of `string_list_split_in_place()` and `string_list_remove_empty_items()`. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 492ba81 commit 826f0e3

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

t/helper/test-hashmap.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "git-compat-util.h"
33
#include "hashmap.h"
44
#include "strbuf.h"
5+
#include "string-list.h"
56

67
struct test_entry
78
{
@@ -150,6 +151,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
150151
*/
151152
int cmd__hashmap(int argc, const char **argv)
152153
{
154+
struct string_list parts = STRING_LIST_INIT_NODUP;
153155
struct strbuf line = STRBUF_INIT;
154156
int icase;
155157
struct hashmap map = HASHMAP_INIT(test_entry_cmp, &icase);
@@ -159,21 +161,26 @@ int cmd__hashmap(int argc, const char **argv)
159161

160162
/* process commands from stdin */
161163
while (strbuf_getline(&line, stdin) != EOF) {
162-
char *cmd, *p1 = NULL, *p2 = NULL;
164+
char *cmd, *p1, *p2;
163165
unsigned int hash = 0;
164166
struct test_entry *entry;
165167

166168
/* break line into command and up to two parameters */
167-
cmd = strtok(line.buf, DELIM);
169+
string_list_setlen(&parts, 0);
170+
string_list_split_in_place(&parts, line.buf, DELIM, 2);
171+
string_list_remove_empty_items(&parts, 0);
172+
168173
/* ignore empty lines */
169-
if (!cmd || *cmd == '#')
174+
if (!parts.nr)
175+
continue;
176+
if (!*parts.items[0].string || *parts.items[0].string == '#')
170177
continue;
171178

172-
p1 = strtok(NULL, DELIM);
173-
if (p1) {
179+
cmd = parts.items[0].string;
180+
p1 = parts.nr >= 1 ? parts.items[1].string : NULL;
181+
p2 = parts.nr >= 2 ? parts.items[2].string : NULL;
182+
if (p1)
174183
hash = icase ? strihash(p1) : strhash(p1);
175-
p2 = strtok(NULL, DELIM);
176-
}
177184

178185
if (!strcmp("add", cmd) && p1 && p2) {
179186

@@ -260,6 +267,7 @@ int cmd__hashmap(int argc, const char **argv)
260267
}
261268
}
262269

270+
string_list_clear(&parts, 0);
263271
strbuf_release(&line);
264272
hashmap_clear_and_free(&map, struct test_entry, ent);
265273
return 0;

0 commit comments

Comments
 (0)