Skip to content

test(image): fix unspecified evaluation order in ImageTest.CopyOnWrite - #166

Merged
transfix merged 1 commit into
masterfrom
fix/image-cow-test-eval-order
Aug 4, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/image-cow-test-eval-order

Conversation

@transfix

@transfix transfix commented Aug 4, 2026

Copy link
Copy Markdown
Owner

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.

Cause

EXPECT_EQ(a.storage().get(), a.data());   // image_test.cpp:93
boost::shared_array<unsigned char> storage() const { return _data; }   // BY VALUE -> use_count++
void image::detach() { if (_data && _data.use_count() > 1) { ...copy... } }
unsigned char *image::data() { detach(); return _data.get(); }

storage() returns the shared_array by value, so the returned temporary holds a reference and pushes use_count() to 2. data() detaches whenever use_count() > 1. Both calls are arguments of one full-expression, and argument evaluation order is unspecified in C++:

order result
data() first no extra ref, no detach → pointers equal
storage() first temporary still alive → data() detaches → pointers differ

gcc picked the first, clang the second.

Reproduced locally

Same expression shape, minimal shared_array model, both compilers on this machine:

gcc  : EXPECT_EQ(a.storage().get(), a.data()) -> PASS
clang: EXPECT_EQ(a.storage().get(), a.data()) -> FAIL

That matches the CI pointers exactly — 0x1070a5940 is the buffer storage() captured, 0x1070a5b40 the fresh one data() allocated.

Fix

Sequence data() into its own statement before storage(). 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 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 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.

Why now

Only reachable because #165 fixed the macOS dependency closure. The macOS build previously died in cvcpkg install-deps and never ran a single test, so this has likely been latent since cvc::image landed (#156/#158).

With this merged, package-macos should go green. package-windows remains red for an unrelated reason: curl, freetype and libpng have no windows bundles at all.

@transfix

transfix commented Aug 4, 2026

Copy link
Copy Markdown
Owner Author

Ordering: this PR cannot prove itself until #165 merges

package-macos is red here, but not on the test this PR fixes — it fails at Configure (step 6), with Run tests skipped entirely:

4 Install dependencies (libcvc): success   (falls back after cvcpkg install fails)
5 Fetch libcvc-deps:            success
6 Configure:                    failure
7 Build:                        skipped
8 Run tests:                    skipped

This branch is cut from master, which does not yet carry #165's dependency-closure fixes (openblas scoped off macOS, fontconfig + expat declared). So the macOS build still dies before it ever runs a test — the exact pre-#165 failure.

Merge order: #165 first, then this. Once #165 is in master, re-running CI here should take package-macos green, since the closure work gets the build to test execution and this PR fixes the only test that then fails.

The fix itself is verified independently of CI — the identical expression reproduced under both compilers on a local machine (g++ PASS / clang++ FAIL before, both PASS after), which is what pins the diagnosis to argument evaluation order rather than anything macOS-specific.

package-windows stays red regardless: curl, freetype and libpng have no windows bundles at all.

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.
@transfix
transfix force-pushed the fix/image-cow-test-eval-order branch from 94ce489 to a913d51 Compare August 4, 2026 07:19
@transfix
transfix merged commit c391f8c into master Aug 4, 2026
6 of 7 checks passed
@transfix
transfix deleted the fix/image-cow-test-eval-order branch August 4, 2026 08:26
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.

1 participant