Skip to content

Commit 692c8b7

Browse files
authored
GH-46306: [C++][Parquet] Should use LoadEnumSafe for geo enum (#46307)
### Rationale for this change OSS-Fuzz reports an error about loading unknown enum. ### What changes are included in this PR? Add LoadEnumSafe for geo enum ### Are these changes tested? Covered by existing ### Are there any user-facing changes? no * GitHub Issue: #46306 Authored-by: mwish <[email protected]> Signed-off-by: mwish <[email protected]>
1 parent d9f74dc commit 692c8b7

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

cpp/src/parquet/thrift_internal.h

+28-19
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ static inline BoundaryOrder::type FromThriftUnsafe(format::BoundaryOrder::type t
106106
return static_cast<BoundaryOrder::type>(type);
107107
}
108108

109+
static inline GeometryLogicalType::EdgeInterpolationAlgorithm FromThriftUnsafe(
110+
format::EdgeInterpolationAlgorithm::type type) {
111+
switch (type) {
112+
case format::EdgeInterpolationAlgorithm::SPHERICAL:
113+
return GeometryLogicalType::EdgeInterpolationAlgorithm::SPHERICAL;
114+
case format::EdgeInterpolationAlgorithm::VINCENTY:
115+
return GeometryLogicalType::EdgeInterpolationAlgorithm::VINCENTY;
116+
case format::EdgeInterpolationAlgorithm::THOMAS:
117+
return GeometryLogicalType::EdgeInterpolationAlgorithm::THOMAS;
118+
case format::EdgeInterpolationAlgorithm::ANDOYER:
119+
return GeometryLogicalType::EdgeInterpolationAlgorithm::ANDOYER;
120+
case format::EdgeInterpolationAlgorithm::KARNEY:
121+
return GeometryLogicalType::EdgeInterpolationAlgorithm::KARNEY;
122+
default:
123+
ARROW_DCHECK(false) << "Cannot reach here";
124+
return GeometryLogicalType::EdgeInterpolationAlgorithm::UNKNOWN;
125+
}
126+
}
127+
109128
namespace internal {
110129

111130
template <typename T>
@@ -221,6 +240,15 @@ inline typename Compression::type LoadEnumSafe(const format::CompressionCodec::t
221240
return FromThriftUnsafe(*in);
222241
}
223242

243+
inline typename LogicalType::EdgeInterpolationAlgorithm LoadEnumSafe(
244+
const format::EdgeInterpolationAlgorithm::type* in) {
245+
if (ARROW_PREDICT_FALSE(*in < format::EdgeInterpolationAlgorithm::SPHERICAL ||
246+
*in > format::EdgeInterpolationAlgorithm::KARNEY)) {
247+
return LogicalType::EdgeInterpolationAlgorithm::UNKNOWN;
248+
}
249+
return FromThriftUnsafe(*in);
250+
}
251+
224252
// Safe non-enum converters
225253

226254
static inline AadMetadata FromThrift(format::AesGcmV1 aesGcmV1) {
@@ -281,25 +309,6 @@ static inline format::EdgeInterpolationAlgorithm::type ToThrift(
281309
}
282310
}
283311

284-
static inline LogicalType::EdgeInterpolationAlgorithm FromThrift(
285-
const format::EdgeInterpolationAlgorithm::type algorithm) {
286-
switch (algorithm) {
287-
case format::EdgeInterpolationAlgorithm::SPHERICAL:
288-
return LogicalType::EdgeInterpolationAlgorithm::SPHERICAL;
289-
case format::EdgeInterpolationAlgorithm::VINCENTY:
290-
return LogicalType::EdgeInterpolationAlgorithm::VINCENTY;
291-
case format::EdgeInterpolationAlgorithm::THOMAS:
292-
return LogicalType::EdgeInterpolationAlgorithm::THOMAS;
293-
case format::EdgeInterpolationAlgorithm::ANDOYER:
294-
return LogicalType::EdgeInterpolationAlgorithm::ANDOYER;
295-
case format::EdgeInterpolationAlgorithm::KARNEY:
296-
return LogicalType::EdgeInterpolationAlgorithm::KARNEY;
297-
default:
298-
throw ParquetException("Unknown value for geometry algorithm: ",
299-
static_cast<int>(algorithm));
300-
}
301-
}
302-
303312
static inline EncryptionAlgorithm FromThrift(format::EncryptionAlgorithm encryption) {
304313
EncryptionAlgorithm encryption_algorithm;
305314

cpp/src/parquet/types.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ std::shared_ptr<const LogicalType> LogicalType::FromThrift(
501501
if (!type.GEOGRAPHY.__isset.algorithm) {
502502
algorithm = LogicalType::EdgeInterpolationAlgorithm::SPHERICAL;
503503
} else {
504-
algorithm = ::parquet::FromThrift(type.GEOGRAPHY.algorithm);
504+
algorithm = LoadEnumSafe(&type.GEOGRAPHY.algorithm);
505505
}
506506

507507
return GeographyLogicalType::Make(std::move(crs), algorithm);

0 commit comments

Comments
 (0)