Skip to content

Fix clang -Walloc-size error in CloseHandle test by removing unused WriteBuffer#125360

Open
Copilot wants to merge 3 commits intomainfrom
copilot/fix-alloc-size-error
Open

Fix clang -Walloc-size error in CloseHandle test by removing unused WriteBuffer#125360
Copilot wants to merge 3 commits intomainfrom
copilot/fix-alloc-size-error

Conversation

Copy link
Contributor

Copilot AI commented Mar 10, 2026

WriteBuffer was declared as LPDWORD but was allocated with sizeof(WORD) (2 bytes) and never actually used — it was never passed to WriteFile or any other function. This dead code triggered a hard -Werror,-Walloc-size error in newer clang builds targeting aarch64-linux-gnu.

Changes

  • src/coreclr/pal/tests/palsuite/miscellaneous/CloseHandle/test1/test.cpp: Remove the unused WriteBuffer variable entirely, along with its malloc, null-check, and free calls. Also updated the stale /* Depends on: CreateFile and WriteFile */ comment to /* Depends on: CreateFile */ since WriteFile was never called.
Original prompt

Problem

The build is failing with a clang -Walloc-size error in src/coreclr/pal/tests/palsuite/miscellaneous/CloseHandle/test1/test.cpp:

/__w/1/s/src/coreclr/pal/tests/palsuite/miscellaneous/CloseHandle/test1/test.cpp:32:28: error: allocation of insufficient size '2' for type 'DWORD' (aka 'unsigned int') with size '4' [-Werror,-Walloc-size]
   32 |     WriteBuffer = (LPDWORD)malloc(sizeof(WORD));

WriteBuffer is declared as LPDWORD (pointer to DWORD, which is 4 bytes), but the malloc call only allocates sizeof(WORD) (2 bytes). This is caught as an error by newer clang via -Walloc-size.

Fix

In src/coreclr/pal/tests/palsuite/miscellaneous/CloseHandle/test1/test.cpp, change line 32 from:

WriteBuffer = (LPDWORD)malloc(sizeof(WORD));

to:

WriteBuffer = (LPDWORD)malloc(sizeof(DWORD));

This ensures the allocation size matches the type that WriteBuffer points to.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: I'm getting following failure. can you put up a PR with a fix?

 /usr/local/bin/clang++ --target=aarch64-linux-gnu --gcc-toolchain=/crossrootfs/arm64/usr --sysroot=/crossrootfs/arm64 -DBUILDENV_DEBUG=1 -DCOMPILER_SUPPORTS_W_RESERVED_IDENTIFIER -DCORECLR -DDEBUG -DDISABLE_CONTRACTS -DHOST_64BIT -DHOST_ARM64 -DHOST_UNIX -DLP64COMPATIBLE -DPIC -DTARGET_64BIT -DTARGET_ARM64 -DTARGET_LINUX -DTARGET_UNIX -DURTBLDENV_FRIENDLY=Debug -D_DBG -D_DEBUG -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64 -I/__w/1/s/artifacts/obj/coreclr/linux.arm64.Debug/pal/tests/palsuite -I/__w/1/s/src/coreclr/pal/tests/palsuite -I/__w/1/s/src/native -I/__w/1/s/src/native/inc -I/__w/1/s/src/coreclr/runtime -I/__w/1/s/src/coreclr/pal/inc -I/__w/1/s/src/coreclr/pal/src -I/__w/1/s/src/coreclr/pal/../inc -I/__w/1/s/src/coreclr/pal/tests/palsuite/common -g -std=gnu++17 -fPIE -fPIE -O0 -Wall -Wno-null-conversion -glldb -fno-omit-frame-pointer -fno-strict-overflow -fno-strict-aliasing -fstack-protector-strong -Werror -Wno-unused-variable -Wno-unused-value -Wno-unused-function -Wno-tautological-compare -Wno-unknown-pragmas -Wimplicit-fallthrough -Wvla -Wno-invalid-offsetof -Wno-unused-but-set-variable -ffp-contract=off -fno-rtti -Wno-unknown-warning-option -ferror-limit=4096 -Wno-unused-private-field -Wno-constant-logical-operand -Wno-pragma-pack -Wno-incompatible-ms-struct -Wno-reserved-identifier -Wno-unsafe-buffer-usage -Wno-single-bit-bitfield-constant-conversion -Wno-cast-function-type-strict -Wno-switch-default -Wno-nontrivial-memaccess -fsigned-char -fvisibility=hidden -ffunction-sections -Wno-writable-strings -Wno-empty-body -Wno-incompatible-pointer-types-discards-qualifiers -Wno-int-to-void-pointer-cast -MD -MT pal/tests/palsuite/CMakeFiles/paltests.dir/miscellaneous/CloseHandle/test1/test.cpp.o -MF pal/tests/palsuite/CMakeFiles/paltests.dir/miscellaneous/CloseHandle/test1/test.cpp.o.d -o pal/tests/palsuite/CMakeFiles/paltests.dir/miscellaneous/CloseHandle/test1/test.cpp.o -c /__w/1/s/src/coreclr/pal/tests/palsuite/miscellaneous/CloseHandle/test1/test.cpp
  /__w/1/s/src/coreclr/pal/tests/palsuite/miscellaneous/CloseHandle/test1/test.cpp:32:28: error: allocation of insufficient size '2' for type 'DWORD' (aka 'unsigned int') with size '4' [-Werror,-Walloc-size]
     32 |     WriteBuffer = (LPDWORD)malloc(sizeof(WORD));
        |         

Assistant: The fix is straightforward: line 32 allocates sizeof(WORD) (2 bytes) for an LPDWORD pointer, but DWORD is 4 bytes — it should be sizeof(DWORD). Let me open a PR with that fix.

Assistant: We're waiting for your response to the tool call dialog to continue — once you accept it, the coding agent will create a PR that changes sizeof(WORD) to sizeof(DWORD) on line 32 of test.cpp, fixing the -Walloc-size clang error where a LPDWORD buffer was being under-allocated with only 2 bytes instead of the required 4.

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

… LPDWORD allocation

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
@MichalStrehovsky
Copy link
Member

@copilot is WriteBuffer necessary for something?

Copilot AI changed the title [WIP] Fix allocation size error in CloseHandle test Fix clang -Walloc-size error: sizeof(WORD) → sizeof(DWORD) in CloseHandle test Mar 10, 2026
Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 10, 2026

@copilot is WriteBuffer necessary for something?

No, WriteBuffer was never used — it was allocated but never passed to WriteFile or any other function. Removed it entirely in 34b02e0.

Copilot AI changed the title Fix clang -Walloc-size error: sizeof(WORD) → sizeof(DWORD) in CloseHandle test Fix clang -Walloc-size error in CloseHandle test by removing unused WriteBuffer Mar 10, 2026
@MichalStrehovsky MichalStrehovsky marked this pull request as ready for review March 10, 2026 03:43
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a clang -Werror,-Walloc-size build break in the PAL CloseHandle test by removing dead code that allocated an incorrectly sized buffer and was never used.

Changes:

  • Removed the unused WriteBuffer variable and its malloc/null-check/free code.
  • Updated the stale dependency comment to no longer mention WriteFile.

You can also share your feedback on Copilot code review. Take the survey.

FILE_ATTRIBUTE_NORMAL,
NULL);

Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

CreateFile return value isn't validated. If it returns INVALID_HANDLE_VALUE, the test will call CloseHandle on an invalid handle and then fail with a misleading error message (making failures hard to diagnose and potentially flaky in restricted environments). Add an explicit check after CreateFile and Fail/return when the handle is invalid (optionally including GetLastError() in the message).

Suggested change
if (FileHandle == INVALID_HANDLE_VALUE)
{
Fail("ERROR: (Test 1) CreateFile failed with error code %u.\n",
GetLastError());
PAL_Terminate();
return FAIL;
}

Copilot uses AI. Check for mistakes.
@MichalStrehovsky
Copy link
Member

/azp run runtime-coreclr outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants