Skip to content

Commit 60ff56f

Browse files
ttaylorrgitster
authored andcommitted
banned.h: mark strtok() and strtok_r() as banned
`strtok()` has a couple of drawbacks that make it undesirable to have any new instances. In addition to being thread-unsafe, it also encourages confusing data flows, where `strtok()` may be called from multiple functions with its first argument as NULL, making it unclear from the immediate context which string is being tokenized. Now that we have removed all instances of `strtok()` from the tree, let's ban `strtok()` to avoid introducing new ones in the future. If new callers should arise, they are encouraged to use `string_list_split_in_place()` (and `string_list_remove_empty_items()`, if applicable). string_list_split_in_place() is not a perfect drop-in replacement for `strtok_r()`, particularly if the caller is processing a string with an arbitrary number of tokens, and wants to process each token one at a time. But there are no instances of this in Git's tree which are more well-suited to `strtok_r()` than the friendlier `string_list_split_in_place()`, so ban `strtok_r()`, too. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a2742f8 commit 60ff56f

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

banned.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#define strncpy(x,y,n) BANNED(strncpy)
1919
#undef strncat
2020
#define strncat(x,y,n) BANNED(strncat)
21+
#undef strtok
22+
#define strtok(x,y) BANNED(strtok)
23+
#undef strtok_r
24+
#define strtok_r(x,y,z) BANNED(strtok_r)
2125

2226
#undef sprintf
2327
#undef vsprintf

0 commit comments

Comments
 (0)