Repository navigation
Use .data() instead of const_cast(.c_str()) to fix undefined behavior - #1073
Merged
Merged
Conversation
…ties (CWE-120/CWE-676) Replace all uses of strcpy and strncpy with bounds-aware memcpy calls to eliminate potential buffer overflow vulnerabilities flagged by CodeQL. All call sites already know the exact copy length, so memcpy with explicit size is both safe and equivalent. Also simplify UTF8Util::FromSubstr to use the std::string(ptr, len) constructor instead of strncpy with const_cast (which was undefined behavior). https://claude.ai/code/session_01TeWHUg4WeAWkaumW2jWgzJ
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to remove unsafe C string copy APIs (strcpy/strncpy) by switching call sites to explicit-length copies (memcpy) and by simplifying UTF8Util::FromSubstr to a safer/cleaner std::string(ptr, len) construction.
Changes:
- Replace
strcpy/strncpywithmemcpyusing explicit byte counts in multiple conversion/buffer-building paths. - Simplify
UTF8Util::FromSubstrimplementation to avoidconst_cast+strncpy. - Add
include where needed formemcpyusage (partially; one TU still appears to rely on transitive headers).
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
src/tools/CommandLine.cpp |
Uses memcpy to restore UTF-8 remainder bytes between chunked reads. |
src/UTF8Util.hpp |
Simplifies substring creation to std::string(str, length). |
src/SimpleConverter.cpp |
Copies converted output into newly allocated C buffer via memcpy(len+1). |
src/SerializedValues.cpp |
Uses memcpy(len+1) when constructing serialized value buffer. |
src/Converter.cpp |
Switches from strcpy to memcpy(len+1) for caller-provided output buffer. |
src/BinaryDict.cpp |
Uses memcpy(len+1) when constructing key/value buffers for binary dict serialization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…tion Agent-Logs-Url: https://github.com/BYVoid/OpenCC/sessions/76b398c5-d1db-4adb-a59a-e7d476663626 Co-authored-by: BYVoid <245270+BYVoid@users.noreply.github.com>
…r access Address review comments: replace undefined behavior of writing through const_cast(str.c_str()) with C++17 non-const .data() accessor. Also cache Key()/Value() return values in BinaryDict::ConstructBuffer to avoid redundant by-value copies. https://claude.ai/code/session_01TeWHUg4WeAWkaumW2jWgzJ
Consistently use .data() for all memcpy calls to match the intent of raw byte copying rather than C-string access. https://claude.ai/code/session_01TeWHUg4WeAWkaumW2jWgzJ
frankslin
reviewed
Mar 27, 2026
Bind return-by-value results to const std::string& to extend temporary lifetime without copying, per review feedback. https://claude.ai/code/session_01TeWHUg4WeAWkaumW2jWgzJ
Revert the strcpy→memcpy and strncpy→memcpy replacements. The PR now focuses solely on replacing undefined behavior from const_cast(.c_str()) with C++17 non-const .data() for writable buffer access, and using .data() consistently for copy sources. https://claude.ai/code/session_01TeWHUg4WeAWkaumW2jWgzJ
Use .data() only for writable destination buffers where it replaces const_cast(.c_str()). Keep .c_str() for read-only source operands of strcpy/strncpy since it better conveys intent. https://claude.ai/code/session_01TeWHUg4WeAWkaumW2jWgzJ
Restore the original strncpy-based implementation of FromSubstr but use .data() instead of const_cast(.c_str()) for writable access. https://claude.ai/code/session_01TeWHUg4WeAWkaumW2jWgzJ
3 tasks done
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.
Summary
Replace
const_cast(str.c_str()) with C++17 non-conststr.data()to eliminate undefined behavior when writing intostd::stringbuffers. Also use.data()consistently for copy source operands, and cache by-value return results in localconst std::string&to avoid redundant temporaries.Why
.data()instead ofconst_cast(.c_str()) In standard C++,
std::string::c_str()returns aconst char*. Writing through aconst_cast-ed pointer obtained from.c_str()is undefined behavior — the implementation is not required to allow mutation through that pointer, and compilers may optimize assuming the pointed-to data is not modified.C++17 added a non-const overload of
std::string::data()that returnschar*, specifically designed for cases where you need writable access to the internal buffer afterresize(). This is the correct, well-defined way to obtain a mutable pointer to astd::string's storage.Changes
src/BinaryDict.cppconst_cast(keyBuf.c_str()) /valueBuf.c_str()→.data()for writable buffers; cacheKey()/Value()inconst std::string&; use.data()in offset calculations and assertssrc/SerializedValues.cppconst_cast(valueBuffer->c_str()) →valueBuffer->data(); use.data()consistentlysrc/UTF8Util.hppstrncpy(const_cast(newStr.c_str()), ...) withstd::string(str, length)constructorsrc/Converter.cpp.data()instead of.c_str()for copy sourcesrc/SimpleConverter.cpp.data()instead of.c_str()for copy sourcesrc/tools/CommandLine.cpp.data()instead of.c_str()for copy source; add explicit#includeTest plan
https://claude.ai/code/session_01TeWHUg4WeAWkaumW2jWgzJ