Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions c/lang/security/insecure-use-strtok-fn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ rules:
- id: insecure-use-strtok-fn
pattern: strtok(...)
message: >-
Avoid using 'strtok()'. This function directly modifies the first argument buffer,
permanently erasing the
delimiter character. Use 'strtok_r()' instead.
Avoid 'strtok()': it is not reentrant or thread-safe, since it keeps the parser
position in a hidden static buffer, so interleaved or concurrent calls (even on

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Describe the saved parser state rather than a buffer

When this diagnostic is shown for any strtok() call, it incorrectly implies that the function stores the parser position in a hidden character buffer. The hidden object is parser state—typically a static pointer into the caller-provided input—while the input itself remains the only token buffer and is modified in place. Since this change is specifically intended to explain the real hazard, calling that state a “static buffer” gives users an inaccurate memory model; describe it as hidden static state or a saved pointer instead.

Useful? React with 👍 / 👎.

unrelated strings) clobber each other's state. It also modifies its input in

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Qualify the thread-safety claim as a portability guarantee

For implementations that keep the tokenizer state in thread-local storage, concurrent calls from different threads do not clobber one another, so the unconditional statement that strtok() is not thread-safe is stronger than the C interface guarantees. The portable problem is that C does not require strtok() to avoid data races, while interleaved calls in the same thread still share state. Phrase this as “not guaranteed to be thread-safe” rather than promising that every implementation exhibits concurrent state corruption.

Useful? React with 👍 / 👎.

place, overwriting each delimiter with a NUL byte. Use the reentrant

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Limit the NUL-overwrite claim to token-ending delimiters

For inputs with leading, trailing, or consecutive delimiters, strtok() skips some delimiters without modifying them and overwrites only the delimiter that terminates each returned token. Thus “overwriting each delimiter” is observably false—for example, tokenizing ",,a,,b,," leaves several commas intact—and can mislead users inspecting or reusing the mutated buffer. State that token-ending delimiters are replaced with NUL bytes instead.

Useful? React with 👍 / 👎.

'strtok_r()' (POSIX) or 'strtok_s()' (C11 Annex K), which keep the scan state in
a caller-provided context.
metadata:
cwe:
- 'CWE-676: Use of Potentially Dangerous Function'
Expand Down