llvm
5af20e1f - [LLVM] Don't memcpy from a null pointer for empty byte-array properties (#23077)

Commit
9 days ago
[LLVM] Don't memcpy from a null pointer for empty byte-array properties (#23077) ## Problem `PropertyValue::PropertyValue(const byte *Data, SizeTy DataBitSize)` in `llvm/lib/Support/PropertySetIO.cpp` ends with: ```cpp // Append data. std::memcpy(Val.ByteArrayVal + SizeFieldSize, Data, DataSize); ``` `Data` is null whenever the property value is an empty container: the templated constructor in `llvm/include/llvm/Support/PropertySetIO.h` forwards `Data.data()`, and for an empty `std::vector<char>`/`SmallVector` that is `nullptr`. `memcpy`'s parameters are declared `__attribute__((nonnull))`, so `memcpy(dst, nullptr, 0)` is undefined behaviour even though the length is zero (https://en.cppreference.com/cpp/string/byte/memcpy) UBSan diagnostic (build configured with `-DLLVM_USE_SANITIZER=Address;Undefined`): ``` llvm/lib/Support/PropertySetIO.cpp:171:49: runtime error: null pointer passed as argument 2, which is declared to never be null /usr/include/string.h:44:28: note: nonnull attribute specified here SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ``` ## Fix Skip the copy when there is nothing to copy: ```cpp // Append data. Data may be null when DataSize is zero, and memcpy declares // its source as nonnull, so guard the call. if (DataSize > 0) std::memcpy(Val.ByteArrayVal + SizeFieldSize, Data, DataSize); ``` ---- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Author
Parents
Loading