Skip to content

Guard StringUtil::StartsWith against prefix longer than string#884

Open
Chessing234 wants to merge 1 commit intocmu-db:masterfrom
Chessing234:fix/stringutil-startswith-bounds
Open

Guard StringUtil::StartsWith against prefix longer than string#884
Chessing234 wants to merge 1 commit intocmu-db:masterfrom
Chessing234:fix/stringutil-startswith-bounds

Conversation

@Chessing234
Copy link
Copy Markdown

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`.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant