diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index e41ad384b84f7..03bddbe3e983a 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -257,9 +257,6 @@ clang-format libclang -------- -- Fixed a buffer overflow in ``CXString`` implementation. The fix may result in - increased memory allocation. - Code Completion --------------- diff --git a/clang/tools/libclang/CXString.cpp b/clang/tools/libclang/CXString.cpp index aaa8f8eeb67a1..5e427957a1092 100644 --- a/clang/tools/libclang/CXString.cpp +++ b/clang/tools/libclang/CXString.cpp @@ -87,7 +87,19 @@ CXString createRef(StringRef String) { if (String.empty()) return createEmpty(); - return createDup(String); + // If the string is not nul-terminated, we have to make a copy. + + // FIXME: This is doing a one past end read, and should be removed! For memory + // we don't manage, the API string can become unterminated at any time outside + // our control. + + if (String.data()[String.size()] != 0) + return createDup(String); + + CXString Result; + Result.data = String.data(); + Result.private_flags = (unsigned) CXS_Unmanaged; + return Result; } CXString createDup(StringRef String) {