From 22b90c700ca98f59a1605f1f57b55f8121d200fd Mon Sep 17 00:00:00 2001 From: Milan Kriz Date: Fri, 2 Aug 2024 16:26:25 +0200 Subject: [PATCH] Revert "[#586] Partially fix SC issue "reinterpret_cast should not be used"" This reverts commit 8ca2cf35887fd7dccfe39b28e160dc71202f7124. And part of commit 7a85da43fdaa09f7db71a3943ad34bf5ca44453b. --- compiler/extensions/cpp/freemarker/SqlTable.cpp.ftl | 8 ++++---- .../extensions/cpp/runtime/ClangTidySuppressions.txt | 11 +++++++++++ .../extensions/cpp/runtime/src/zserio/FloatUtil.cpp | 8 ++++---- .../cpp/runtime/src/zserio/ValidationSqliteUtil.h | 6 ++---- .../cpp/runtime/test/zserio/SqliteConnectionTest.cpp | 3 +-- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/compiler/extensions/cpp/freemarker/SqlTable.cpp.ftl b/compiler/extensions/cpp/freemarker/SqlTable.cpp.ftl index 6c75a1ed2..945ad690e 100644 --- a/compiler/extensions/cpp/freemarker/SqlTable.cpp.ftl +++ b/compiler/extensions/cpp/freemarker/SqlTable.cpp.ftl @@ -211,7 +211,7 @@ ${name}::Row ${name}::Reader::next() <#else> const unsigned char* textValue = sqlite3_column_text(m_stmt.get(), ${field?index}); row.${field.setterName}(${field.typeInfo.typeFullName}( - static_cast(static_cast(textValue)), get_allocator_ref())); + reinterpret_cast(textValue), get_allocator_ref())); } @@ -671,7 +671,7 @@ bool ${name}::validateField${field.name?cap_first}(::zserio::IValidationObserver <#else> const unsigned char* textValue = sqlite3_column_text(statement, ${field?index}); row.${field.setterName}(${field.typeInfo.typeFullName}( - static_cast(static_cast(textValue)), get_allocator_ref())); + reinterpret_cast(textValue), get_allocator_ref())); return true; @@ -703,13 +703,13 @@ bool ${name}::validateField${field.name?cap_first}(::zserio::IValidationObserver rowKeyValuesHolder.emplace_back("BLOB"); <#else> const unsigned char* strValue${field.name?cap_first} = sqlite3_column_text(statement, ${field?index}); - rowKeyValuesHolder.emplace_back(static_cast(static_cast(strValue${field.name?cap_first}))); + rowKeyValuesHolder.emplace_back(reinterpret_cast(strValue${field.name?cap_first})); <#else> const unsigned char* strValueRowId = sqlite3_column_text(statement, ${fields?size}); - rowKeyValuesHolder.emplace_back(static_cast(static_cast(strValueRowId))); + rowKeyValuesHolder.emplace_back(reinterpret_cast(strValueRowId)); return rowKeyValuesHolder; diff --git a/compiler/extensions/cpp/runtime/ClangTidySuppressions.txt b/compiler/extensions/cpp/runtime/ClangTidySuppressions.txt index 30ecb329f..d37a86685 100644 --- a/compiler/extensions/cpp/runtime/ClangTidySuppressions.txt +++ b/compiler/extensions/cpp/runtime/ClangTidySuppressions.txt @@ -82,9 +82,17 @@ cppcoreguidelines-pro-type-reinterpret-cast:src/zserio/AnyHolder.h:869 cppcoreguidelines-pro-type-reinterpret-cast:src/zserio/AnyHolder.h:876 cppcoreguidelines-pro-type-reinterpret-cast:src/zserio/OptionalHolder.h:658 cppcoreguidelines-pro-type-reinterpret-cast:src/zserio/OptionalHolder.h:668 +# This is necessary for implementation of reading and writing of floats to the binary stream. +cppcoreguidelines-pro-type-reinterpret-cast:src/zserio/FloatUtil.cpp:169 +cppcoreguidelines-pro-type-reinterpret-cast:src/zserio/FloatUtil.cpp:176 +cppcoreguidelines-pro-type-reinterpret-cast:src/zserio/FloatUtil.cpp:183 +cppcoreguidelines-pro-type-reinterpret-cast:src/zserio/FloatUtil.cpp:190 # This is necessary for implementation of reading and writing to the file. cppcoreguidelines-pro-type-reinterpret-cast:src/zserio/FileUtil.cpp:19 cppcoreguidelines-pro-type-reinterpret-cast:src/zserio/FileUtil.cpp:49 +# This is necessary for cast the return value from SQLite3 low level API call (sqlite3_column_text). +cppcoreguidelines-pro-type-reinterpret-cast:src/zserio/ValidationSqliteUtil.h:99 +cppcoreguidelines-pro-type-reinterpret-cast:src/zserio/ValidationSqliteUtil.h:100 # This multiple inheritance is intended and we think that to avoid it would mean much more obscure design. fuchsia-multiple-inheritance:src/zserio/Reflectable.h:2023 @@ -155,6 +163,9 @@ cppcoreguidelines-pro-bounds-pointer-arithmetic:test/zserio/StringViewTest.cpp:9 cppcoreguidelines-pro-bounds-pointer-arithmetic:test/zserio/StringViewTest.cpp:156 cppcoreguidelines-pro-bounds-pointer-arithmetic:test/zserio/StringViewTest.cpp:209 +# Intentional tests. +cppcoreguidelines-pro-type-reinterpret-cast:test/zserio/SqliteConnectionTest.cpp:320 + # Intentional tests. It is necessary for readability. google-build-using-namespace:test/zserio/ReflectableTest.cpp:22 diff --git a/compiler/extensions/cpp/runtime/src/zserio/FloatUtil.cpp b/compiler/extensions/cpp/runtime/src/zserio/FloatUtil.cpp index b7b7d92e2..a71a48004 100644 --- a/compiler/extensions/cpp/runtime/src/zserio/FloatUtil.cpp +++ b/compiler/extensions/cpp/runtime/src/zserio/FloatUtil.cpp @@ -166,28 +166,28 @@ uint16_t convertFloatToUInt16(float float32) float convertUInt32ToFloat(uint32_t float32Value) { - const float* convertedFloat = static_cast(static_cast(&float32Value)); + const float* convertedFloat = reinterpret_cast(&float32Value); return *convertedFloat; } uint32_t convertFloatToUInt32(float float32) { - const uint32_t* float32ValuePtr = static_cast(static_cast(&float32)); + const uint32_t* float32ValuePtr = reinterpret_cast(&float32); return *float32ValuePtr; } double convertUInt64ToDouble(uint64_t float64Value) { - const double* convertedDouble = static_cast(static_cast(&float64Value)); + const double* convertedDouble = reinterpret_cast(&float64Value); return *convertedDouble; } uint64_t convertDoubleToUInt64(double float64) { - const uint64_t* float64ValuePtr = static_cast(static_cast(&float64)); + const uint64_t* float64ValuePtr = reinterpret_cast(&float64); return *float64ValuePtr; } diff --git a/compiler/extensions/cpp/runtime/src/zserio/ValidationSqliteUtil.h b/compiler/extensions/cpp/runtime/src/zserio/ValidationSqliteUtil.h index 240f5c194..9c767c21e 100644 --- a/compiler/extensions/cpp/runtime/src/zserio/ValidationSqliteUtil.h +++ b/compiler/extensions/cpp/runtime/src/zserio/ValidationSqliteUtil.h @@ -96,10 +96,8 @@ struct ValidationSqliteUtil int result = SQLITE_OK; while ((result = sqlite3_step(statement.get())) == SQLITE_ROW) { - const unsigned char* columnNameText = sqlite3_column_text(statement.get(), 1); - const char* columnName = static_cast(static_cast(columnNameText)); - const unsigned char* columnTypeText = sqlite3_column_text(statement.get(), 2); - const char* columnType = static_cast(static_cast(columnTypeText)); + const char* columnName = reinterpret_cast(sqlite3_column_text(statement.get(), 1)); + const char* columnType = reinterpret_cast(sqlite3_column_text(statement.get(), 2)); tableSchema.emplace(string_type(columnName, allocator), ColumnDescription{ string_type(columnName, allocator), string_type(columnType, allocator), diff --git a/compiler/extensions/cpp/runtime/test/zserio/SqliteConnectionTest.cpp b/compiler/extensions/cpp/runtime/test/zserio/SqliteConnectionTest.cpp index 9fb3e03d4..6151d8458 100644 --- a/compiler/extensions/cpp/runtime/test/zserio/SqliteConnectionTest.cpp +++ b/compiler/extensions/cpp/runtime/test/zserio/SqliteConnectionTest.cpp @@ -317,8 +317,7 @@ TEST(SqliteConnectionTest, prepareStatement) ASSERT_EQ(SQLITE_ROW, result); ASSERT_EQ(1, sqlite3_column_count(statement)); - const unsigned char* textValue = sqlite3_column_text(statement, 0); - const std::string resultString(static_cast(static_cast(textValue))); + const std::string resultString(reinterpret_cast(sqlite3_column_text(statement, 0))); ASSERT_EQ("1", resultString); result = sqlite3_step(statement);