Skip to content

Commit

Permalink
fix(perception): remove UB reinterpret_cast (#3383)
Browse files Browse the repository at this point in the history
* fix(perception): remove UB reinterpret_cast

see #3215

Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>

* fix(pointcloud_preprocessor): remove UB reinterpret_cast

Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>

* refactor

Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>

---------

Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>
  • Loading branch information
VRichardJP authored May 9, 2023
1 parent f368b2a commit e7544bc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions perception/tensorrt_yolo/lib/src/plugins/nms_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@

#include <cuda_runtime_api.h>
#include <stdio.h>
#include <string.h>

#include <cassert>
#include <cmath>
#include <cstring>

using nvinfer1::DataType;
using nvinfer1::DimsExprs;
Expand All @@ -62,14 +62,14 @@ const char * NMS_PLUGIN_NAMESPACE{""};
template <typename T>
void write(char *& buffer, const T & val)
{
*reinterpret_cast<T *>(buffer) = val;
std::memcpy(buffer, &val, sizeof(T));
buffer += sizeof(T);
}

template <typename T>
void read(const char *& buffer, T & val)
{
val = *reinterpret_cast<const T *>(buffer);
std::memcpy(&val, buffer, sizeof(T));
buffer += sizeof(T);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@

#include <cuda_runtime_api.h>
#include <stdio.h>
#include <string.h>

#include <cassert>
#include <cmath>
#include <cstring>
#include <vector>

using nvinfer1::DataType;
Expand All @@ -87,14 +87,14 @@ const char * YOLO_LAYER_PLUGIN_NAMESPACE{""};
template <typename T>
void write(char *& buffer, const T & val)
{
*reinterpret_cast<T *>(buffer) = val;
std::memcpy(buffer, &val, sizeof(T));
buffer += sizeof(T);
}

template <typename T>
void read(const char *& buffer, T & val)
{
val = *reinterpret_cast<const T *>(buffer);
std::memcpy(&val, buffer, sizeof(T));
buffer += sizeof(T);
}
} // namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ void CropBoxFilterComponent::faster_filter(

for (size_t global_offset = 0; global_offset + input->point_step <= input->data.size();
global_offset += input->point_step) {
Eigen::Vector4f point(
*reinterpret_cast<const float *>(&input->data[global_offset + x_offset]),
*reinterpret_cast<const float *>(&input->data[global_offset + y_offset]),
*reinterpret_cast<const float *>(&input->data[global_offset + z_offset]), 1);
Eigen::Vector4f point;
std::memcpy(&point[0], &input->data[global_offset + x_offset], sizeof(float));
std::memcpy(&point[1], &input->data[global_offset + y_offset], sizeof(float));
std::memcpy(&point[2], &input->data[global_offset + z_offset], sizeof(float));
point[3] = 1;

if (!std::isfinite(point[0]) || !std::isfinite(point[1]) || !std::isfinite(point[2])) {
RCLCPP_WARN(this->get_logger(), "Ignoring point containing NaN values");
Expand All @@ -148,9 +149,9 @@ void CropBoxFilterComponent::faster_filter(
memcpy(&output.data[output_size], &input->data[global_offset], input->point_step);

if (transform_info.need_transform) {
*reinterpret_cast<float *>(&output.data[output_size + x_offset]) = point[0];
*reinterpret_cast<float *>(&output.data[output_size + y_offset]) = point[1];
*reinterpret_cast<float *>(&output.data[output_size + z_offset]) = point[2];
std::memcpy(&output.data[output_size + x_offset], &point[0], sizeof(float));
std::memcpy(&output.data[output_size + y_offset], &point[1], sizeof(float));
std::memcpy(&output.data[output_size + z_offset], &point[2], sizeof(float));
}

output_size += input->point_step;
Expand Down
4 changes: 2 additions & 2 deletions system/system_monitor/reader/hdd_reader/hdd_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t *>(&data[48]));
std::memcpy(&info->total_data_written_, &data[48], sizeof(info->total_data_written_));

// Bytes 143:128 Power On Hours
info->is_valid_power_on_hours_ = true;
info->power_on_hours_ = *(reinterpret_cast<uint64_t *>(&data[128]));
std::memcpy(&info->power_on_hours_, &data[128], sizeof(info->power_on_hours_));

// NVMe S.M.A.R.T has no information of recovered error count
info->is_valid_recovered_error_ = false;
Expand Down

0 comments on commit e7544bc

Please sign in to comment.