Skip to content

Commit a2bbf18

Browse files
author
Minh Vu
authored
fix: validate truncate widths in utility entrypoints (#739)
## Summary - Reject non-positive widths in the `TruncateLiteral` and `TruncateLiteralMax` utility entrypoints. - Reject zero width in `TruncateUTF8Max`, which returns a `Result` and can report invalid input directly. - Add regression coverage for zero and negative widths across numeric, decimal, string, and binary literal truncation. ## Why The truncate transform validates its width before creating a transform function, but the utility functions can also be called directly. Without a guard there, invalid widths can reach modulo operations or iterator arithmetic in the type-specific implementations. ## Testing - `cmake --build build --target util_test` - `ctest --test-dir build -R util_test --output-on-failure`
1 parent fc9781f commit a2bbf18

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎src/iceberg/test/truncate_util_test.cc‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ TEST(TruncateUtilTest, TruncateLiteral) {
5151
Literal::Binary(std::vector<uint8_t>(expected.begin(), expected.end())));
5252
}
5353

54+
TEST(TruncateUtilTest, TruncateLiteralRejectsInvalidWidth) {
55+
std::vector<uint8_t> data{1, 2, 3};
56+
57+
auto expect_invalid_width = [](const auto& result) {
58+
EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument));
59+
EXPECT_THAT(result, HasErrorMessage("Width must be positive"));
60+
};
61+
62+
for (int32_t width : {0, -1}) {
63+
SCOPED_TRACE(width);
64+
expect_invalid_width(TruncateUtils::TruncateLiteral(Literal::Int(1), width));
65+
expect_invalid_width(TruncateUtils::TruncateLiteral(Literal::Long(1), width));
66+
expect_invalid_width(
67+
TruncateUtils::TruncateLiteral(Literal::Decimal(1065, 4, 2), width));
68+
expect_invalid_width(
69+
TruncateUtils::TruncateLiteral(Literal::String("iceberg"), width));
70+
expect_invalid_width(TruncateUtils::TruncateLiteral(Literal::Binary(data), width));
71+
}
72+
}
73+
5474
TEST(TruncateUtilTest, TruncateBinaryMax) {
5575
std::vector<uint8_t> test1{1, 1, 2};
5676
std::vector<uint8_t> test2{1, 1, 0xFF, 2};
@@ -190,4 +210,26 @@ TEST(TruncateUtilTest, TruncateStringMax) {
190210
EXPECT_EQ(result9_2, Literal::String(test9_2_expected));
191211
}
192212

213+
TEST(TruncateUtilTest, TruncateLiteralMaxRejectsInvalidWidth) {
214+
std::vector<uint8_t> data{1, 2, 3};
215+
216+
auto expect_invalid_width = [](const auto& result) {
217+
EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument));
218+
EXPECT_THAT(result, HasErrorMessage("Width must be positive"));
219+
};
220+
221+
for (int32_t width : {0, -1}) {
222+
SCOPED_TRACE(width);
223+
expect_invalid_width(
224+
TruncateUtils::TruncateLiteralMax(Literal::String("iceberg"), width));
225+
expect_invalid_width(TruncateUtils::TruncateLiteralMax(Literal::Binary(data), width));
226+
}
227+
}
228+
229+
TEST(TruncateUtilTest, TruncateUTF8MaxRejectsZeroWidth) {
230+
auto result = TruncateUtils::TruncateUTF8Max("iceberg", 0);
231+
EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument));
232+
EXPECT_THAT(result, HasErrorMessage("Width must be positive"));
233+
}
234+
193235
} // namespace iceberg

‎src/iceberg/util/truncate_util.cc‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "iceberg/expression/literal.h"
2727
#include "iceberg/type.h"
2828
#include "iceberg/util/checked_cast.h"
29+
#include "iceberg/util/macros.h"
2930

3031
namespace iceberg {
3132

@@ -34,6 +35,20 @@ constexpr uint32_t kUtf8MaxCodePoint = 0x10FFFF;
3435
constexpr uint32_t kUtf8MinSurrogate = 0xD800;
3536
constexpr uint32_t kUtf8MaxSurrogate = 0xDFFF;
3637

38+
Status ValidateTruncateWidth(int32_t width) {
39+
if (width <= 0) {
40+
return InvalidArgument("Width must be positive, got {}", width);
41+
}
42+
return {};
43+
}
44+
45+
Status ValidateTruncateWidth(size_t width) {
46+
if (width == 0) {
47+
return InvalidArgument("Width must be positive, got 0");
48+
}
49+
return {};
50+
}
51+
3752
std::optional<uint32_t> DecodeUtf8CodePoint(std::string_view source) {
3853
if (source.empty()) {
3954
return std::nullopt;
@@ -205,6 +220,8 @@ Result<std::optional<Literal>> TruncateLiteralMaxImpl<TypeId::kBinary>(
205220

206221
Result<std::optional<std::string>> TruncateUtils::TruncateUTF8Max(
207222
const std::string& source, size_t L) {
223+
ICEBERG_RETURN_UNEXPECTED(ValidateTruncateWidth(L));
224+
208225
std::string truncated = TruncateUTF8(source, L);
209226
if (truncated == source) {
210227
return truncated;
@@ -253,6 +270,8 @@ Decimal TruncateUtils::TruncateDecimal(const Decimal& decimal, int32_t width) {
253270
return TruncateLiteralImpl<TYPE_ID>(literal, width);
254271

255272
Result<Literal> TruncateUtils::TruncateLiteral(const Literal& literal, int32_t width) {
273+
ICEBERG_RETURN_UNEXPECTED(ValidateTruncateWidth(width));
274+
256275
if (literal.IsNull()) [[unlikely]] {
257276
// Return null as is
258277
return literal;
@@ -280,6 +299,8 @@ Result<Literal> TruncateUtils::TruncateLiteral(const Literal& literal, int32_t w
280299

281300
Result<std::optional<Literal>> TruncateUtils::TruncateLiteralMax(const Literal& literal,
282301
int32_t width) {
302+
ICEBERG_RETURN_UNEXPECTED(ValidateTruncateWidth(width));
303+
283304
if (literal.IsNull()) [[unlikely]] {
284305
// Return null as is
285306
return literal;

0 commit comments

Comments
 (0)