From 10a905a4ba3ab4a87d35e759d177b3faaccca455 Mon Sep 17 00:00:00 2001 From: aditi-pandit Date: Tue, 3 Dec 2024 21:52:11 +0530 Subject: [PATCH] refactor: Change C style casts to C++ style (Part 7) As per the security guideline in https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es49-if-you-must-use-a-cast-use-a-named-cast --- velox/connectors/hive/SplitReader.cpp | 2 +- .../hive/iceberg/PositionalDeleteFileReader.cpp | 2 +- velox/dwio/dwrf/reader/DwrfReader.cpp | 2 +- velox/dwio/dwrf/test/FloatColumnWriterBenchmark.cpp | 2 +- velox/dwio/dwrf/test/TestIntegerDictionaryEncoder.cpp | 3 ++- velox/dwio/dwrf/test/TestOrcColumnStatistics.cpp | 2 +- velox/dwio/dwrf/writer/Writer.cpp | 4 ++-- velox/dwio/parquet/reader/ParquetTypeWithId.cpp | 4 ++-- velox/dwio/parquet/reader/ParquetTypeWithId.h | 3 ++- velox/dwio/parquet/reader/RleBpDecoder.cpp | 3 ++- velox/dwio/parquet/writer/arrow/util/CompressionZlib.cpp | 4 ++-- velox/dwio/parquet/writer/arrow/util/Crc32.cpp | 9 +++++---- velox/external/date/tz.cpp | 4 ++-- velox/external/timsort/TimSort.hpp | 6 +++--- velox/parse/Expressions.cpp | 2 +- velox/serializers/CompactRowSerializer.cpp | 3 ++- 16 files changed, 30 insertions(+), 25 deletions(-) diff --git a/velox/connectors/hive/SplitReader.cpp b/velox/connectors/hive/SplitReader.cpp index 7b357ea09746..05d4ae4d145c 100644 --- a/velox/connectors/hive/SplitReader.cpp +++ b/velox/connectors/hive/SplitReader.cpp @@ -42,7 +42,7 @@ VectorPtr newConstantFromString( } if (type->isDate()) { - auto days = DATE()->toDays((folly::StringPiece)value.value()); + auto days = DATE()->toDays(static_cast(value.value())); return std::make_shared>( pool, size, false, type, std::move(days)); } diff --git a/velox/connectors/hive/iceberg/PositionalDeleteFileReader.cpp b/velox/connectors/hive/iceberg/PositionalDeleteFileReader.cpp index 94828d136e6b..166614f2aecd 100644 --- a/velox/connectors/hive/iceberg/PositionalDeleteFileReader.cpp +++ b/velox/connectors/hive/iceberg/PositionalDeleteFileReader.cpp @@ -233,7 +233,7 @@ void PositionalDeleteFileReader::updateDeleteBitmap( // There might be multiple delete files for a single base file. The size of // the deleteBitmapBuffer should be the largest position among all delte files deleteBitmapBuffer->setSize(std::max( - (uint64_t)deleteBitmapBuffer->size(), + static_cast(deleteBitmapBuffer->size()), deletePositionsOffset_ == 0 || (deletePositionsOffset_ < deletePositionsVector->size() && deletePositions[deletePositionsOffset_] > rowNumberUpperBound) diff --git a/velox/dwio/dwrf/reader/DwrfReader.cpp b/velox/dwio/dwrf/reader/DwrfReader.cpp index 42fd560a5830..4bd265f9bfb1 100644 --- a/velox/dwio/dwrf/reader/DwrfReader.cpp +++ b/velox/dwio/dwrf/reader/DwrfReader.cpp @@ -973,7 +973,7 @@ uint64_t DwrfReader::getMemoryUse( uint64_t memoryBytes = hasStringColumn ? 2 * maxDataLength : std::min( - uint64_t(maxDataLength), + static_cast(maxDataLength), numSelectedStreams * readerBase.bufferedInput().getReadFile()->getNaturalReadSize()); diff --git a/velox/dwio/dwrf/test/FloatColumnWriterBenchmark.cpp b/velox/dwio/dwrf/test/FloatColumnWriterBenchmark.cpp index 30f70a2bc529..b536d48ecac6 100644 --- a/velox/dwio/dwrf/test/FloatColumnWriterBenchmark.cpp +++ b/velox/dwio/dwrf/test/FloatColumnWriterBenchmark.cpp @@ -30,7 +30,7 @@ constexpr vector_size_t kVectorSize = 10000; vector_size_t kNumIterations = 1000; float genData(float pos) { - return float(pos * (float)3.14); + return float(pos * static_cast(3.14)); } bool isNotNull(int32_t pos, int32_t nullEvery) { diff --git a/velox/dwio/dwrf/test/TestIntegerDictionaryEncoder.cpp b/velox/dwio/dwrf/test/TestIntegerDictionaryEncoder.cpp index 8f1e1675c584..bfb4177dc845 100644 --- a/velox/dwio/dwrf/test/TestIntegerDictionaryEncoder.cpp +++ b/velox/dwio/dwrf/test/TestIntegerDictionaryEncoder.cpp @@ -347,7 +347,8 @@ TEST_F(TestIntegerDictionaryEncoder, ShortIntegerDictionary) { intDictEncoder.addKey(key); } - int32_t dictSize = (int32_t)2 + std::numeric_limits::max(); + int32_t dictSize = + static_cast(2) + std::numeric_limits::max(); std::vector dictValues; auto actualSize = diff --git a/velox/dwio/dwrf/test/TestOrcColumnStatistics.cpp b/velox/dwio/dwrf/test/TestOrcColumnStatistics.cpp index db11bf2439aa..a90ffa603c13 100644 --- a/velox/dwio/dwrf/test/TestOrcColumnStatistics.cpp +++ b/velox/dwio/dwrf/test/TestOrcColumnStatistics.cpp @@ -88,7 +88,7 @@ TEST_F(ColumnStatisticsTest, stringEmptyStats) { proto.set_numberofvalues(0); auto columnStatisticsWrapper = ColumnStatisticsWrapper(&proto); testStringEmptyStats( - columnStatisticsWrapper, (void*)&proto, DwrfFormat::kOrc); + columnStatisticsWrapper, static_cast(&proto), DwrfFormat::kOrc); } TEST_F(ColumnStatisticsTest, stringLengthThreshold) { diff --git a/velox/dwio/dwrf/writer/Writer.cpp b/velox/dwio/dwrf/writer/Writer.cpp index e63666190e36..a86a7733ff92 100644 --- a/velox/dwio/dwrf/writer/Writer.cpp +++ b/velox/dwio/dwrf/writer/Writer.cpp @@ -239,7 +239,7 @@ void Writer::ensureWriteFits(size_t appendBytes, size_t appendRows) { const size_t estimatedAppendMemoryBytes = std::max(appendBytes, context.estimateNextWriteSize(appendRows)); const double estimatedMemoryGrowthRatio = - (double)estimatedAppendMemoryBytes / totalMemoryUsage; + static_cast(estimatedAppendMemoryBytes) / totalMemoryUsage; if (!maybeReserveMemory( MemoryUsageCategory::GENERAL, estimatedMemoryGrowthRatio)) { return; @@ -281,7 +281,7 @@ void Writer::ensureStripeFlushFits() { .maybeReserve(outputMemoryToReserve); } else { const double estimatedMemoryGrowthRatio = - (double)estimateFlushMemoryBytes / outputMemoryUsage; + static_cast(estimateFlushMemoryBytes) / outputMemoryUsage; maybeReserveMemory( MemoryUsageCategory::OUTPUT_STREAM, estimatedMemoryGrowthRatio); } diff --git a/velox/dwio/parquet/reader/ParquetTypeWithId.cpp b/velox/dwio/parquet/reader/ParquetTypeWithId.cpp index 888be67fd586..25ec85f10b25 100644 --- a/velox/dwio/parquet/reader/ParquetTypeWithId.cpp +++ b/velox/dwio/parquet/reader/ParquetTypeWithId.cpp @@ -34,14 +34,14 @@ bool containsList(const ParquetTypeWithId& type) { } // namespace std::vector> -ParquetTypeWithId::moveChildren() && { +ParquetTypeWithId::moveChildren() const&& { std::vector> children; for (auto& child : getChildren()) { auto type = child->type(); auto id = child->id(); auto maxId = child->maxId(); auto column = child->column(); - auto* parquetChild = (ParquetTypeWithId*)child.get(); + auto* parquetChild = dynamic_cast(child.get()); auto name = parquetChild->name_; auto parquetType = parquetChild->parquetType_; auto logicalType = parquetChild->logicalType_; diff --git a/velox/dwio/parquet/reader/ParquetTypeWithId.h b/velox/dwio/parquet/reader/ParquetTypeWithId.h index e01db3eef20f..a87d52203b57 100644 --- a/velox/dwio/parquet/reader/ParquetTypeWithId.h +++ b/velox/dwio/parquet/reader/ParquetTypeWithId.h @@ -78,7 +78,8 @@ class ParquetTypeWithId : public dwio::common::TypeWithId { /// Fills 'info' and returns the mode for interpreting levels. LevelMode makeLevelInfo(LevelInfo& info) const; - std::vector> moveChildren() &&; + std::vector> moveChildren() + const&&; const std::string name_; const std::optional parquetType_; diff --git a/velox/dwio/parquet/reader/RleBpDecoder.cpp b/velox/dwio/parquet/reader/RleBpDecoder.cpp index 4b044da9ac27..0e7fb52a1a0f 100644 --- a/velox/dwio/parquet/reader/RleBpDecoder.cpp +++ b/velox/dwio/parquet/reader/RleBpDecoder.cpp @@ -85,7 +85,8 @@ void RleBpDecoder::readBits( void RleBpDecoder::readHeader() { bitOffset_ = 0; auto maxVarIntLen = std::min( - (uint64_t)folly::kMaxVarintLength64, bufferEnd_ - bufferStart_); + static_cast(folly::kMaxVarintLength64), + bufferEnd_ - bufferStart_); folly::ByteRange headerRange( reinterpret_cast(bufferStart_), reinterpret_cast(bufferStart_ + maxVarIntLen)); diff --git a/velox/dwio/parquet/writer/arrow/util/CompressionZlib.cpp b/velox/dwio/parquet/writer/arrow/util/CompressionZlib.cpp index 82cb2cb778fb..5f7b2b2150e0 100644 --- a/velox/dwio/parquet/writer/arrow/util/CompressionZlib.cpp +++ b/velox/dwio/parquet/writer/arrow/util/CompressionZlib.cpp @@ -368,7 +368,7 @@ class GZipCodec : public Codec { void EndCompressor() { if (compressor_initialized_) { - (void)deflateEnd(&stream_); + static_cast(deflateEnd(&stream_)); } compressor_initialized_ = false; } @@ -389,7 +389,7 @@ class GZipCodec : public Codec { void EndDecompressor() { if (decompressor_initialized_) { - (void)inflateEnd(&stream_); + static_cast(inflateEnd(&stream_)); } decompressor_initialized_ = false; } diff --git a/velox/dwio/parquet/writer/arrow/util/Crc32.cpp b/velox/dwio/parquet/writer/arrow/util/Crc32.cpp index 5a4937623d91..9b00633f32fd 100644 --- a/velox/dwio/parquet/writer/arrow/util/Crc32.cpp +++ b/velox/dwio/parquet/writer/arrow/util/Crc32.cpp @@ -864,16 +864,17 @@ uint32_t crc32(uint32_t prev, const void* data, size_t length) { const uint8_t* current_char; const uint32_t* current; - unaligned = ALIGNOF_UINT32_T - ((uintptr_t)data % ALIGNOF_UINT32_T); + unaligned = + ALIGNOF_UINT32_T - (reinterpret_cast(data) % ALIGNOF_UINT32_T); if (unaligned == ALIGNOF_UINT32_T) unaligned = 0; /* process a byte at a time until we hit an alignment boundary (max 3) */ - current_char = (const uint8_t*)data; + current_char = reinterpret_cast(data); for (; unaligned && length; unaligned--, length--) crc = (crc >> 8) ^ crc32_lookup[0][(crc & 0xFF) ^ *current_char++]; - current = (const uint32_t*)current_char; + current = reinterpret_cast(current_char); /* process 64 bytes at once (Slicing-by-16) */ @@ -969,7 +970,7 @@ uint32_t crc32(uint32_t prev, const void* data, size_t length) { /* Finish with any remaining bytes one by one */ - current_char = (const uint8_t*)current; + current_char = reinterpret_cast(current); /* remaining 1 to 3 bytes (standard algorithm) */ while (length-- != 0) crc = (crc >> 8) ^ crc32_lookup[0][(crc & 0xFF) ^ *current_char++]; diff --git a/velox/external/date/tz.cpp b/velox/external/date/tz.cpp index 13ebe93561da..ce67dc2f1515 100644 --- a/velox/external/date/tz.cpp +++ b/velox/external/date/tz.cpp @@ -2833,8 +2833,8 @@ static std::unique_ptr curl_init() { - static const auto curl_is_now_initiailized = curl_global(); - (void)curl_is_now_initiailized; + static const auto curl_is_now_initialized = curl_global(); + static_cast(curl_is_now_initialized); return std::unique_ptr{::curl_easy_init()}; } diff --git a/velox/external/timsort/TimSort.hpp b/velox/external/timsort/TimSort.hpp index 501ca3c0df82..f422aa302fee 100644 --- a/velox/external/timsort/TimSort.hpp +++ b/velox/external/timsort/TimSort.hpp @@ -53,13 +53,13 @@ #ifdef GFX_TIMSORT_ENABLE_ASSERT #define GFX_TIMSORT_ASSERT(expr) assert(expr) #else -#define GFX_TIMSORT_ASSERT(expr) ((void)0) +#define GFX_TIMSORT_ASSERT(expr) (static_cast(0)) #endif #ifdef GFX_TIMSORT_ENABLE_AUDIT #define GFX_TIMSORT_AUDIT(expr) assert(expr) #else -#define GFX_TIMSORT_AUDIT(expr) ((void)0) +#define GFX_TIMSORT_AUDIT(expr) (static_cast(0)) #endif #ifdef GFX_TIMSORT_ENABLE_LOG @@ -67,7 +67,7 @@ #define GFX_TIMSORT_LOG(expr) \ (std::clog << "# " << __func__ << ": " << expr << std::endl) #else -#define GFX_TIMSORT_LOG(expr) ((void)0) +#define GFX_TIMSORT_LOG(expr) (static_cast(0)) #endif namespace gfx { diff --git a/velox/parse/Expressions.cpp b/velox/parse/Expressions.cpp index feb87a3801f6..bc7513480ebc 100644 --- a/velox/parse/Expressions.cpp +++ b/velox/parse/Expressions.cpp @@ -78,7 +78,7 @@ std::vector implicitCastTargets(const TypePtr& type) { break; } default: // make compilers happy - (void)0; // Statement to avoid empty semicolon warning + break; } return targetTypes; } diff --git a/velox/serializers/CompactRowSerializer.cpp b/velox/serializers/CompactRowSerializer.cpp index 6bddcbbbf3dd..1d5b8538e753 100644 --- a/velox/serializers/CompactRowSerializer.cpp +++ b/velox/serializers/CompactRowSerializer.cpp @@ -40,7 +40,8 @@ class CompactRowVectorSerializer : public RowSerializer { for (const auto& range : ranges) { if (range.size == 1) { // Fast path for single-row serialization. - *(TRowSize*)(rawBuffer + offset) = folly::Endian::big(rowSize[index]); + *reinterpret_cast(rawBuffer + offset) = + folly::Endian::big(rowSize[index]); auto size = row.serialize(range.begin, rawBuffer + offset + sizeof(TRowSize)); offset += size + sizeof(TRowSize);