Skip to content

Write cfa_pattern_dim in place as C++ does, without resizing it - #14

Open
kunpai wants to merge 1 commit into
google:masterfrom
kunpai:cfa-pattern-dim-in-place
Open

kunpai wants to merge 1 commit into
google:masterfrom
kunpai:cfa-pattern-dim-in-place

Conversation

@kunpai

@kunpai kunpai commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Write cfa_pattern_dim in place as C++ does, without resizing it

NOTE: #24 changes the C++ side: FillPreviewImageData now resizes a cfa_pattern_dim shorter than 2 before it writes the two values, so it no longer writes past the end. On master with #24, the first two rows below agree (C++ also gives {2, 2}, size 2). The third row still differs: when the file has no CFARepeatPatternDim, C++ leaves the caller's vector as it was ({2147483647}, size 1), and the bridge still resizes it to 2 and writes {0, 0}. This PR's fix no longer matches C++ after #24: it writes past the end where C++ now resizes (SIGSEGV for size 0, {2} for size 1). If C++'s behavior after #24 is the one we want, we can change the fix to keep the resize, and do it only when the Rust side found a CFA pattern:

-  dst->cfa_pattern_dim.resize(std::max<size_t>(dst->cfa_pattern_dim.size(), 2));
-  dst->cfa_pattern_dim[0] = src.cfa_pattern_dim[0];
-  dst->cfa_pattern_dim[1] = src.cfa_pattern_dim[1];
+  if (src.cfa_pattern_dim[0] != 0 || src.cfa_pattern_dim[1] != 0) {
+    dst->cfa_pattern_dim.resize(std::max<size_t>(dst->cfa_pattern_dim.size(), 2));
+    dst->cfa_pattern_dim[0] = src.cfa_pattern_dim[0];
+    dst->cfa_pattern_dim[1] = src.cfa_pattern_dim[1];
+  }

On master with #24 and this change, all three rows agree, nothing is written past the end, and the port's tests pass (44 + 174).

Problem

GetPreviewImageData writes the CFA repeat pattern's two dimensions into the caller's std::vector<std::uint32_t> cfa_pattern_dim (C++, Rust). PreviewImageData makes that vector 2 long, but a caller can hand in one of any size.

  • C++ writes cfa_pattern_dim[0] and cfa_pattern_dim[1] only when a directory has a two-value CFARepeatPatternDim, and it never checks the vector's size. A shorter vector is written past its end. An empty one has no buffer at all, so the write faults.
  • The bridge's CopyPreviewData resizes the vector to at least 2 and writes both values on every call, whether the file had the tag or not.

For example:

C++ Rust
cfa_pattern_dim of size 0, a DNG with CFARepeatPatternDim 2, 2 writes past the end of the empty vector: SIGSEGV returns kOk, cfa_pattern_dim {2, 2}
size 1, the same DNG writes [1] past the end: cfa_pattern_dim {2}, size 1 {2, 2}, size 2
size 1 holding {2147483647}, a file cut short before any IFD leaves the vector alone: {2147483647}, size 1 {0, 0}, size 2

Question

Which behaviour do we want: C++'s or the Rust port's? Specifically, should cfa_pattern_dim be written in place, only when the file has the tag and at whatever size the caller gave it (like the C++ library), or be resized to 2 and written on every call (like the Rust port)?

This PR implements C++'s behaviour.

Fix

CopyPreviewData no longer resizes the vector, and writes the two values only when the caller's vector was already at least 2 long or the Rust side found a CFA pattern. ToBridge hands a shorter vector to Rust as {0, 0}, so a nonzero value means the file had the tag:

-  dst->cfa_pattern_dim.resize(std::max<size_t>(dst->cfa_pattern_dim.size(), 2));
-  dst->cfa_pattern_dim[0] = src.cfa_pattern_dim[0];
-  dst->cfa_pattern_dim[1] = src.cfa_pattern_dim[1];
+  if (dst->cfa_pattern_dim.size() >= 2 || src.cfa_pattern_dim[0] != 0 ||
+      src.cfa_pattern_dim[1] != 0) {
+    dst->cfa_pattern_dim[0] = src.cfa_pattern_dim[0];
+    dst->cfa_pattern_dim[1] = src.cfa_pattern_dim[1];
+  }

The examples above then do what C++ does. Replayed before and after, on x86-64 and on 32-bit ARM:

C++ Rust main Rust patched
size 0, DNG with the tag SIGSEGV (11), both arches kOk, {2, 2} SIGSEGV (11), both arches
size 1, DNG with the tag {2}, size 1, both arches {2, 2}, size 2 {2}, size 1, both arches
size 1 {2147483647}, file cut short {2147483647}, size 1 {0, 0}, size 2 {2147483647}, size 1

Notes for review:

  • This patch reproduces C++'s undefined behaviour and must not be merged. For a cfa_pattern_dim shorter than 2 it writes past the end of the caller's vector: a heap overflow for size 1, a write through a null buffer for size 0. That is memory-unsafe, and the resize it removes is what keeps the port safe today. The PR exists to show the difference and to ask which behavior is intended: whether GetPreviewImageData should keep a caller's short vector as it is, or grow it.
  • A file whose CFARepeatPatternDim is 0, 0 and a caller's vector shorter than 2 still differ: C++ writes the zeros past the end, and the patched bridge cannot tell them from "no tag".
  • The bridge is C++ and has no unit tests; the port's Rust tests pass unchanged (44 + 174).

C++ writes cfa_pattern_dim[0] and [1] into the caller's vector only when
the file has the tag, and does not resize it. The bridge resized it to
at least 2 and always wrote both values.
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