Skip to content

Commit 37faf3e

Browse files
authored
GH-47373: [C++] Raise for invalid decimal precision input from the C Data Interface (#47414)
### Rationale for this change This fixes an issue where invalid decimal precision data passed through the C Data Interface would cause the program to abort. ### What changes are included in this PR? The aborting decimal constructor is replaced with a factory function call that will validate the precision input and propogate up and error accordingly ### Are these changes tested? Yes ### Are there any user-facing changes? No * GitHub Issue: #47373 Authored-by: Will Ayd <[email protected]> Signed-off-by: AlenkaF <[email protected]>
1 parent 9306a11 commit 37faf3e

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

cpp/src/arrow/c/bridge.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,15 +1259,15 @@ struct SchemaImporter {
12591259
return f_parser_.Invalid();
12601260
}
12611261
if (prec_scale.size() == 2) {
1262-
type_ = decimal128(prec_scale[0], prec_scale[1]);
1262+
ARROW_ASSIGN_OR_RAISE(type_, Decimal128Type::Make(prec_scale[0], prec_scale[1]));
12631263
} else if (prec_scale[2] == 32) {
1264-
type_ = decimal32(prec_scale[0], prec_scale[1]);
1264+
ARROW_ASSIGN_OR_RAISE(type_, Decimal32Type::Make(prec_scale[0], prec_scale[1]));
12651265
} else if (prec_scale[2] == 64) {
1266-
type_ = decimal64(prec_scale[0], prec_scale[1]);
1266+
ARROW_ASSIGN_OR_RAISE(type_, Decimal64Type::Make(prec_scale[0], prec_scale[1]));
12671267
} else if (prec_scale[2] == 128) {
1268-
type_ = decimal128(prec_scale[0], prec_scale[1]);
1268+
ARROW_ASSIGN_OR_RAISE(type_, Decimal128Type::Make(prec_scale[0], prec_scale[1]));
12691269
} else if (prec_scale[2] == 256) {
1270-
type_ = decimal256(prec_scale[0], prec_scale[1]);
1270+
ARROW_ASSIGN_OR_RAISE(type_, Decimal256Type::Make(prec_scale[0], prec_scale[1]));
12711271
} else {
12721272
return f_parser_.Invalid();
12731273
}

cpp/src/arrow/c/bridge_test.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,34 @@ TEST_F(TestSchemaImport, DictionaryError) {
23572357
CheckImportError();
23582358
}
23592359

2360+
TEST_F(TestSchemaImport, DecimalError) {
2361+
// Decimal precision out of bounds
2362+
FillPrimitive("d:0,10");
2363+
CheckImportError();
2364+
FillPrimitive("d:39,10");
2365+
CheckImportError();
2366+
2367+
FillPrimitive("d:0,4,32");
2368+
CheckImportError();
2369+
FillPrimitive("d:10,4,32");
2370+
CheckImportError();
2371+
2372+
FillPrimitive("d:0,4,64");
2373+
CheckImportError();
2374+
FillPrimitive("d:19,4,64");
2375+
CheckImportError();
2376+
2377+
FillPrimitive("d:0,10,128");
2378+
CheckImportError();
2379+
FillPrimitive("d:39,10,128");
2380+
CheckImportError();
2381+
2382+
FillPrimitive("d:0,4,256");
2383+
CheckImportError();
2384+
FillPrimitive("d:77,4,256");
2385+
CheckImportError();
2386+
}
2387+
23602388
TEST_F(TestSchemaImport, ExtensionError) {
23612389
ExtensionTypeGuard guard(uuid());
23622390

0 commit comments

Comments
 (0)