Skip to content

Fix undefined behaviour in View::tryCopyTo overlap#1287

Merged
Simn merged 1 commit intoHaxeFoundation:masterfrom
tobil4sk:fix/view-copy-overlap
Jan 7, 2026
Merged

Fix undefined behaviour in View::tryCopyTo overlap#1287
Simn merged 1 commit intoHaxeFoundation:masterfrom
tobil4sk:fix/view-copy-overlap

Conversation

@tobil4sk
Copy link
Member

@tobil4sk tobil4sk commented Jan 6, 2026

This was causing random failures in the haxe test suite: #1286 (comment)

The part where it was going wrong was here: https://github.com/HaxeFoundation/haxe/blob/3aed86e98aaa6097c3b8c9ab5c1aee5f70faa2f9/tests/unit/src/unit/TestBytes.hx#L47

The new implementation uses memcpy for this blit, which is undefined behaviour since the source and destination regions overlap. See: https://en.cppreference.com/w/cpp/string/byte/memcpy.html

If any of the following conditions is satisfied, the behavior is undefined:
[...]
- Copying takes place between objects that overlap.

std::memmove is the alternative which is safe to use for overlapping blocks: https://en.cppreference.com/w/cpp/string/byte/memmove.html. This is also what is used in the ArrayBase::Blit method:

hxcpp/src/Array.cpp

Lines 179 to 182 in 4cff17a

if (src+len < dest || dest+len<src)
memcpy(dest,src,len);
else
memmove(dest,src,len);

@Aidan63
Copy link
Contributor

Aidan63 commented Jan 6, 2026

Ah, nice catch! I forgot that memcpy wasn't safe in overlaps and wasn't thinking about that case at all.

@tobil4sk
Copy link
Member Author

tobil4sk commented Jan 7, 2026

Looks like ci got reran but just in case it comes up again, we had this failure during execution of the mac arm64 "haxe" test:

Memory exhausted.
/Users/runner/work/_temp/d85ca6f0-43ca-4142-8d9d-60a39b2041fe.sh: line 1:  5788 Trace/BPT trap: 5       bin/TestMain
 try HXCPP_GC_BIG_BLOCKS.

@Simn Simn merged commit 3dc43ab into HaxeFoundation:master Jan 7, 2026
239 of 240 checks passed
@tobil4sk tobil4sk deleted the fix/view-copy-overlap branch January 7, 2026 11:06
ionosoft pushed a commit to ionosoft/hxcpp-ion that referenced this pull request Mar 9, 2026
Picks up 37 upstream commits since our last merge (Sep 2, 2025), including:
- Unconditional HXCPP_ALIGN_ALLOC (required for std::atomic in GC objects)
- Missing GCFreeZone calls for semaphores/condvars (HaxeFoundation#1296)
- C++11 std::mutex and condition variable for GC (HaxeFoundation#1298)
- Callable objects with GC write barriers (HaxeFoundation#1047)
- Root handle API (GCAddRoot/GCRemoveRoot) (HaxeFoundation#1292)
- Fix undefined behaviour in View::tryCopyTo overlap (HaxeFoundation#1287)
- Fix static target null Int problem
- Skip splicing when no changes (HaxeFoundation#1309)

Our ionosoft-specific changes preserved:
- GcRegCapture RSI register fix (d71061e)
- msvc-toolchain.xml debug build flags

Co-Authored-By: Claude Opus 4.6 <[email protected]>
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