Guard StringUtil::StartsWith against prefix longer than string#884
Open
Chessing234 wants to merge 1 commit intocmu-db:masterfrom
Open
Guard StringUtil::StartsWith against prefix longer than string#884Chessing234 wants to merge 1 commit intocmu-db:masterfrom
Chessing234 wants to merge 1 commit intocmu-db:masterfrom
Conversation
StringUtil::StartsWith calls the 3-argument form of std::equal:
return std::equal(prefix.begin(), prefix.end(), str.begin());
which reads prefix.size() characters starting at str.begin() without
bounds-checking str. When prefix is longer than str, this reads past
the end of str and is undefined behaviour.
This path is easy to hit from the shell: the interactive loop in
tools/shell/shell.cpp and tools/nc-shell/nc-shell.cpp ends the read
when 'EndsWith(query, ";") || StartsWith(query, "\\")' is true, and
query can still be empty (e.g. the user presses Enter on a fresh
prompt). Other call sites (bustub_instance.cpp's '\\dbgmvcc'/'\\txn'
checks, plan_table_ref.cpp's '__' / '__mock' checks, bind_insert.cpp,
and sqllogictest's option parsing) similarly pass user-controlled
strings that can be shorter than the prefix.
Add the same size guard that sibling EndsWith already has: return
false when prefix is longer than str, then fall through to std::equal
on the remaining (safe) range.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
`StringUtil::StartsWith` in `src/common/util/string_util.cpp` uses the 3-argument form of `std::equal`:
```cpp
auto StringUtil::StartsWith(const std::string &str, const std::string &prefix) -> bool {
return std::equal(prefix.begin(), prefix.end(), str.begin());
}
```
When `prefix.size() > str.size()`, this reads `prefix.size()` characters starting at `str.begin()`, walking past the end of `str`. That's undefined behaviour.
Root cause
No length check. The sibling `EndsWith` just below it already has the guard:
```cpp
auto StringUtil::EndsWith(const std::string &str, const std::string &suffix) -> bool {
if (suffix.size() > str.size()) {
return false;
}
return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
}
```
`StartsWith` was written without it.
The bad path is trivially reachable from the interactive shells. In `tools/shell/shell.cpp` and `tools/nc-shell/nc-shell.cpp` the read loop ends when:
```cpp
if (bustub::StringUtil::EndsWith(query, ";") || bustub::StringUtil::StartsWith(query, "\\")) {
break;
}
```
and `query` can still be empty (e.g. the user presses Enter on a fresh prompt), which means `StartsWith("", "\\")` reads one byte past the start of an empty string. Other call sites (`bustub_instance.cpp` checking `\\dbgmvcc`/`\\txn`, `plan_table_ref.cpp` checking `__`/`__mock` table-name prefixes, `bind_insert.cpp`, `sqllogictest/sqllogictest.cpp` option parsing) all pass user-controlled strings that can be shorter than the prefix.
Fix
Add the same size guard `EndsWith` already uses:
```cpp
if (prefix.size() > str.size()) {
return false;
}
```
3 lines added, matches the `EndsWith` pattern a few lines below, keeps behaviour identical whenever `str` is at least as long as `prefix`.