Skip to content

[LLVM] Don't memcpy from a null pointer for empty byte-array properties - #23077

Merged
uditagarwal97 merged 1 commit into
intel:syclfrom
uditagarwal97:private/udit/propertysetio-null-memcpy
Sep 2, 2026
Merged

[LLVM] Don't memcpy from a null pointer for empty byte-array properties#23077
uditagarwal97 merged 1 commit into
intel:syclfrom
uditagarwal97:private/udit/propertysetio-null-memcpy

Conversation

@uditagarwal97

@uditagarwal97 uditagarwal97 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Problem

PropertyValue::PropertyValue(const byte *Data, SizeTy DataBitSize) in llvm/lib/Support/PropertySetIO.cpp ends with:

// 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:

// 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

PropertyValue's byte-array constructor called std::memcpy unconditionally, but
an empty container yields a null data pointer and memcpy declares both operands
as nonnull, so every -fsycl compilation that serialized an empty byte array hit
undefined behaviour. Skip the copy when there is nothing to copy, and add the
unit test that would have caught it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused fix correctly addresses the undefined behavior and includes suitable regression coverage.

Pull request overview

Prevents undefined behavior when constructing empty byte-array properties.

Changes:

  • Guards memcpy when the data size is zero.
  • Adds a regression test for empty containers.
File summaries
File Description
llvm/lib/Support/PropertySetIO.cpp Skips zero-length byte-array copies.
llvm/unittests/Support/PropertySetIOTest.cpp Tests empty byte-array construction.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@uditagarwal97
uditagarwal97 marked this pull request as ready for review September 1, 2026 23:21
@uditagarwal97
uditagarwal97 requested a review from a team as a code owner September 1, 2026 23:21

@YuriPlyakhin YuriPlyakhin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@uditagarwal97

Copy link
Copy Markdown
Contributor Author

CI failure is unrelated. Merging!

  Timed Out Tests (1):
    SYCL :: Basic/accessor/accessor.cpp

@uditagarwal97
uditagarwal97 merged commit 5af20e1 into intel:sycl Sep 2, 2026
30 of 31 checks passed
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.

3 participants