Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Write cfa_pattern_dim in place as C++ does, without resizing it
NOTE: #24 changes the C++ side:
FillPreviewImageDatanow resizes acfa_pattern_dimshorter 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: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
GetPreviewImageDatawrites the CFA repeat pattern's two dimensions into the caller'sstd::vector<std::uint32_t> cfa_pattern_dim(C++, Rust).PreviewImageDatamakes that vector 2 long, but a caller can hand in one of any size.cfa_pattern_dim[0]andcfa_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.CopyPreviewDataresizes the vector to at least 2 and writes both values on every call, whether the file had the tag or not.For example:
cfa_pattern_dimof size 0, a DNG with CFARepeatPatternDim 2, 2cfa_pattern_dim{2, 2}[1]past the end:cfa_pattern_dim{2}, size 1Question
Which behaviour do we want: C++'s or the Rust port's? Specifically, should
cfa_pattern_dimbe 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
CopyPreviewDatano 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.ToBridgehands a shorter vector to Rust as {0, 0}, so a nonzero value means the file had the tag:The examples above then do what C++ does. Replayed before and after, on x86-64 and on 32-bit ARM:
Notes for review:
cfa_pattern_dimshorter 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: whetherGetPreviewImageDatashould keep a caller's short vector as it is, or grow it.