Skip to content

Commit d297c1e

Browse files
(Correctness) Bound the SIFT exponential-table lookup
## Summary Prevent an out-of-bounds read when a valid CPU SIFT descriptor reaches the last exponential-table entry. Add a regression test for that endpoint. ## Validation Incremental over 755248a; upstream pin fa08906. - Actual-source address and undefined-behavior sanitizer checks reproduce the original read at index 257, both directly and through a descriptor; both paths pass with the fix. - CPU SIFT tests pass 20/20 on the parent and 21/21 with the fix. Ordinary and sanitized differential fixtures each match 11,620 float descriptors. - The test source passes clang-format 23.1.3. The native executable relinks. The fix retains the existing interpolation and the behavior above the table range. Performance, complete reconstruction output and table-initialization thread safety were not measured by these checks. AI disclosure: Totally vibe coded.
1 parent 755248a commit d297c1e

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎src/colmap/feature/sift_test.cc‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "thirdparty/VLFeat/sift.h"
2222

2323
#include <algorithm>
24+
#include <cmath>
2425
#include <cstdint>
2526
#include <functional>
2627
#include <memory>
@@ -203,6 +204,45 @@ TEST(SiftCPUDescriptorSupport, KeepsHalfBinBoundaryAndIgnoresCorners) {
203204
}
204205
}
205206

207+
TEST(SiftCPUDescriptorSupport, ExponentialTableEndpointIsFinite) {
208+
constexpr int kWidth = 96;
209+
constexpr int kHeight = 96;
210+
std::unique_ptr<VlSiftFilt, decltype(&vl_sift_delete)> filter(
211+
vl_sift_new(kWidth, kHeight, 1, 3, 0), &vl_sift_delete);
212+
ASSERT_NE(filter, nullptr);
213+
const std::vector<vl_sift_pix> pixels(kWidth * kHeight, 0.5f);
214+
ASSERT_EQ(vl_sift_process_first_octave(filter.get(), pixels.data()), 0);
215+
vl_sift_set_magnif(filter.get(), 3);
216+
VlSiftKeypoint keypoint;
217+
vl_sift_keypoint_init(filter.get(), &keypoint, 48, 48, 2);
218+
ASSERT_EQ(keypoint.o, filter->o_cur);
219+
ASSERT_GE(keypoint.is, filter->s_min + 1);
220+
ASSERT_LE(keypoint.is, filter->s_max - 2);
221+
std::vector<vl_sift_pix> initial(128);
222+
vl_sift_calc_keypoint_descriptor(filter.get(), initial.data(), &keypoint, 0);
223+
ASSERT_NE(filter->grad, nullptr);
224+
auto* gradient =
225+
filter->grad + (keypoint.is - filter->s_min - 1) * 2 * kWidth * kHeight;
226+
std::fill(gradient, gradient + 2 * kWidth * kHeight, 0);
227+
gradient[2 * (48 * kWidth + 48)] = 1;
228+
vl_sift_set_window_size(filter.get(), 0.5);
229+
// SBP=6 and offset(-15,-15) give nx=ny=-2.5. Its bins(-3,-3)
230+
// survive support rejection and fast_expn receives exactly25. The second
231+
// spatial neighbor is valid but has zero weight in each dimension.
232+
std::vector<vl_sift_pix> reference(128);
233+
vl_sift_calc_keypoint_descriptor(
234+
filter.get(), reference.data(), &keypoint, 0);
235+
ASSERT_GT(*std::max_element(reference.begin(), reference.end()), 0);
236+
gradient[2 * (33 * kWidth + 33)] = 1;
237+
std::vector<vl_sift_pix> descriptor(128);
238+
vl_sift_calc_keypoint_descriptor(
239+
filter.get(), descriptor.data(), &keypoint, 0);
240+
for (const vl_sift_pix value : descriptor) {
241+
EXPECT_TRUE(std::isfinite(value));
242+
}
243+
EXPECT_EQ(descriptor, reference);
244+
}
245+
206246
Bitmap CreateImageWithSquare(const int size) {
207247
Bitmap bitmap(size, size, false);
208248
bitmap.Fill(BitmapColor<uint8_t>(0, 0, 0));

‎src/thirdparty/VLFeat/sift.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,8 @@ fast_expn (double x)
701701
i = (int)vl_floor_d (x) ;
702702
r = x - i ;
703703
a = expn_tab [i ] ;
704-
b = expn_tab [i + 1] ;
704+
/* The last table point has no right neighbor, including after rounding. */
705+
b = expn_tab [VL_MIN(i + 1, EXPN_SZ)] ;
705706
return a + r * (b - a) ;
706707
}
707708

0 commit comments

Comments
 (0)