Repository navigation
test(image): fix unspecified evaluation order in ImageTest.CopyOnWrite - #166
Conversation
Ordering: this PR cannot prove itself until #165 merges
This branch is cut from Merge order: #165 first, then this. Once #165 is in The fix itself is verified independently of CI — the identical expression reproduced under both compilers on a local machine (
|
ImageTest.CopyOnWrite was the only failing test of 2080 in package-macos.
It is a test bug, not a COW bug, and it is not macOS-specific -- it is
clang vs gcc.
EXPECT_EQ(a.storage().get(), a.data()); // image_test.cpp:93
storage() returns the shared_array BY VALUE, so the returned temporary holds
a reference and pushes use_count() to 2. data() calls detach(), which copies
whenever use_count() > 1. Both calls are arguments of ONE full-expression,
and argument evaluation order is UNSPECIFIED in C++:
data() first -> no extra ref, no detach -> pointers equal
storage() first -> temporary still alive -> data() DETACHES
gcc picked the first order, clang the second. Reproduced locally with the
identical expression against a minimal shared_array model:
gcc : EXPECT_EQ(a.storage().get(), a.data()) -> PASS
clang: EXPECT_EQ(a.storage().get(), a.data()) -> FAIL
matching the CI pointers exactly (0x1070a5940 = the buffer storage() captured,
0x1070a5b40 = the fresh one data() allocated).
Fix is to sequence data() before storage() in its own statement. Verified the
corrected sequence passes under BOTH g++ and clang++.
The implementation is correct and unchanged. Detaching while a storage()
handle is outstanding is exactly right: that handle may be a live zero-copy
view (pycvc numpy, cvcGL vtkTexture) and a mutable data() must not write
through it.
Also replaced the preceding EXPECT_EQ(a.data(), a.data()), which compared one
call to itself and so could never fail. Its comment claimed the copy "shares
the buffer", so it now asserts that via storage() -- which does not detach.
Checking sharing with data() is impossible: the first call detaches and
destroys the aliasing being observed.
Swept the tree for the same trap (a detaching and a non-detaching accessor in
one EXPECT); this was the only instance.
Only reachable now because PR #165 fixed the macOS dependency closure -- the
macOS build previously died in cvcpkg install-deps and never ran a test.
94ce489 to
a913d51
Compare
ImageTest.CopyOnWritewas the only failing test of 2080 inpackage-macos. It is a test bug, not a COW bug — and it is not macOS-specific. It is clang vs gcc.Cause
storage()returns theshared_arrayby value, so the returned temporary holds a reference and pushesuse_count()to 2.data()detaches wheneveruse_count() > 1. Both calls are arguments of one full-expression, and argument evaluation order is unspecified in C++:data()firststorage()firstdata()detaches → pointers differgcc picked the first, clang the second.
Reproduced locally
Same expression shape, minimal
shared_arraymodel, both compilers on this machine:That matches the CI pointers exactly —
0x1070a5940is the bufferstorage()captured,0x1070a5b40the fresh onedata()allocated.Fix
Sequence
data()into its own statement beforestorage(). Verified the corrected sequence passes under both g++ and clang++.The implementation is correct and unchanged. Detaching while a
storage()handle is outstanding is exactly right — that handle may be a live zero-copy view (pycvcnumpy, cvcGLvtkTexture), and a mutabledata()must not write through it.Also
EXPECT_EQ(a.data(), a.data()), which compared one call to itself and could never fail. Its comment claimed the copy "shares the buffer", so it now asserts that viastorage()(which does not detach). Checking sharing withdata()is impossible — the first call detaches and destroys the aliasing being observed.EXPECT). This was the only instance.Why now
Only reachable because #165 fixed the macOS dependency closure. The macOS build previously died in
cvcpkg install-depsand never ran a single test, so this has likely been latent sincecvc::imagelanded (#156/#158).With this merged,
package-macosshould go green.package-windowsremains red for an unrelated reason:curl,freetypeandlibpnghave no windows bundles at all.