From 2a49ee0f0b656e80759ee8a1ea997122b8a3f090 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Wed, 12 Apr 2023 19:46:41 +0900 Subject: [PATCH] fix(perception): remove UB reinterpret_cast see https://github.com/autowarefoundation/autoware.universe/issues/3215 Signed-off-by: Vincent Richard --- perception/tensorrt_yolo/lib/src/plugins/nms_plugin.cpp | 6 +++--- .../tensorrt_yolo/lib/src/plugins/yolo_layer_plugin.cpp | 6 +++--- system/system_monitor/reader/hdd_reader/hdd_reader.cpp | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/perception/tensorrt_yolo/lib/src/plugins/nms_plugin.cpp b/perception/tensorrt_yolo/lib/src/plugins/nms_plugin.cpp index 4ff96bf15257b..b486595a2cb7b 100644 --- a/perception/tensorrt_yolo/lib/src/plugins/nms_plugin.cpp +++ b/perception/tensorrt_yolo/lib/src/plugins/nms_plugin.cpp @@ -39,10 +39,10 @@ #include #include -#include #include #include +#include using nvinfer1::DataType; using nvinfer1::DimsExprs; @@ -62,14 +62,14 @@ const char * NMS_PLUGIN_NAMESPACE{""}; template void write(char *& buffer, const T & val) { - *reinterpret_cast(buffer) = val; + std::memcpy(buffer, &val, sizeof(T)); buffer += sizeof(T); } template void read(const char *& buffer, T & val) { - val = *reinterpret_cast(buffer); + std::memcpy(&val, buffer, sizeof(T)); buffer += sizeof(T); } diff --git a/perception/tensorrt_yolo/lib/src/plugins/yolo_layer_plugin.cpp b/perception/tensorrt_yolo/lib/src/plugins/yolo_layer_plugin.cpp index 78edc568bb7c4..dc12921dc1e96 100644 --- a/perception/tensorrt_yolo/lib/src/plugins/yolo_layer_plugin.cpp +++ b/perception/tensorrt_yolo/lib/src/plugins/yolo_layer_plugin.cpp @@ -63,10 +63,10 @@ #include #include -#include #include #include +#include #include using nvinfer1::DataType; @@ -87,14 +87,14 @@ const char * YOLO_LAYER_PLUGIN_NAMESPACE{""}; template void write(char *& buffer, const T & val) { - *reinterpret_cast(buffer) = val; + std::memcpy(buffer, &val, sizeof(T)); buffer += sizeof(T); } template void read(const char *& buffer, T & val) { - val = *reinterpret_cast(buffer); + std::memcpy(&val, buffer, sizeof(T)); buffer += sizeof(T); } } // namespace diff --git a/system/system_monitor/reader/hdd_reader/hdd_reader.cpp b/system/system_monitor/reader/hdd_reader/hdd_reader.cpp index 9d8b54de0773e..43cf0394e742d 100644 --- a/system/system_monitor/reader/hdd_reader/hdd_reader.cpp +++ b/system/system_monitor/reader/hdd_reader/hdd_reader.cpp @@ -385,11 +385,11 @@ int get_nvme_smart_data(int fd, HddInfo * info) // is from 1 to 1,000, three indicates that the number of 512 byte data // units written is from 2,001 to 3,000) info->is_valid_total_data_written_ = true; - info->total_data_written_ = *(reinterpret_cast(&data[48])); + std::memcpy(&info->total_data_written_, &data[48], sizeof(uint64_t)); // Bytes 143:128 Power On Hours info->is_valid_power_on_hours_ = true; - info->power_on_hours_ = *(reinterpret_cast(&data[128])); + std::memcpy(&info->power_on_hours_, &data[128], sizeof(uint64_t)); // NVMe S.M.A.R.T has no information of recovered error count info->is_valid_recovered_error_ = false;