-
Notifications
You must be signed in to change notification settings - Fork 584
fix(c): describe the real hazard in insecure-use-strtok-fn #4022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| unrelated strings) clobber each other's state. It also modifies its input in | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 Useful? React with 👍 / 👎. |
||
| place, overwriting each delimiter with a NUL byte. Use the reentrant | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For inputs with leading, trailing, or consecutive delimiters, 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' | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 👍 / 👎.