-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changed memcpy/memset for non-POD objects [PCL-3368] #4299
Changed memcpy/memset for non-POD objects [PCL-3368] #4299
Conversation
3bfb1d7
to
97061fc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking the initiative 😄
if (isSamePointType<PointInT, PointOutT> ()) | ||
// Copy the whole memory block | ||
memcpy (&cloud_out[0], &cloud_in[0], cloud_in.size () * sizeof (PointInT)); | ||
#if (__cplusplus == 201703L) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use PCL_MACROS
for a PCL_CONSTEXPR
like solution.
Moreover, why did you stick with memcpy in non C++17 branch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This relates to the issue I mentioned where compilation failed due to trying to use std::copy
on two different Point
objects, despite the isSamePoint
test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ofc. Silly of me. Please let this issue still remain unresolved till I get to think over it a bit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
9/98
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
15/100
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
34/110
PS: Builds are failing (Ignore MSVC for a while) |
Yes, unit tests are failing in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
43/110
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
56/110
visualization/include/pcl/visualization/impl/point_cloud_color_handlers.hpp
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
67/110
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
76/110
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
86/110
63472c7
to
5f1b5a4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
94/110
segmentation/include/pcl/segmentation/impl/sac_segmentation.hpp
Outdated
Show resolved
Hide resolved
segmentation/include/pcl/segmentation/impl/unary_classifier.hpp
Outdated
Show resolved
Hide resolved
15da3fd
to
4898c95
Compare
segmentation/include/pcl/segmentation/impl/unary_classifier.hpp
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
102/110 files are good to go
@@ -309,8 +307,6 @@ pcl::UnaryClassifier<PointT>::queryFeatureDistances (std::vector<pcl::PointCloud | |||
{ | |||
// Query point | |||
flann::Matrix<float> p = flann::Matrix<float>(new float[n_col], 1, n_col); | |||
memcpy (&p.ptr ()[0], (*query_features)[i].histogram, p.cols * p.rows * sizeof (float)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should not be removed (I think).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of the other flann::Matrix
variables are initialized; does knnSearch
return garbage if it's not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤷♂️ I don't know. Will take a deeper look and get back to you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This definitely has to be initialized since its an input for knnSearch
(indices
and distances
are output).
std::copy((*query_features)[i].histogram, (*query_features)[i].histogram + n_col, p.ptr())
should work (1*n_col
is the same as p.cols*p.rows
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files left
1fbddd5
to
e1b8278
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe could make the std::array
chnges also in another MR - just saw in this MR, that we could modernize them here.
@@ -186,15 +185,13 @@ pcl::ISSKeypoint3D<PointInT, PointOutT, NormalT>::getScatterMatrix (const int& c | |||
if (n_neighbors < min_neighbors_) | |||
return; | |||
|
|||
double cov[9]; | |||
memset(cov, 0, sizeof(double) * 9); | |||
double cov[9]{}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
double cov[9]{}; | |
std::array<double, 9> cov{}; |
@@ -166,8 +166,7 @@ pcl::ISSKeypoint3D<PointInT, PointOutT, NormalT>::getScatterMatrix (const int& c | |||
{ | |||
const PointInT& current_point = (*input_)[current_index]; | |||
|
|||
double central_point[3]; | |||
memset(central_point, 0, sizeof(double) * 3); | |||
double central_point[3]{}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
double central_point[3]{}; | |
std::array<double, 3> central_point{}; |
|
||
for (const auto& n_idx : nn_indices) | ||
{ | ||
const PointInT& n_point = (*input_)[n_idx]; | ||
|
||
double neigh_point[3]; | ||
memset(neigh_point, 0, sizeof(double) * 3); | ||
double neigh_point[3]{}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
double neigh_point[3]{}; | |
std::array<double, 3> neigh_point{}; |
@@ -252,8 +249,7 @@ pcl::ISSKeypoint3D<PointInT, PointOutT, NormalT>::initCompute () | |||
|
|||
delete[] third_eigen_value_; | |||
|
|||
third_eigen_value_ = new double[input_->size ()]; | |||
memset(third_eigen_value_, 0, sizeof(double) * input_->size ()); | |||
third_eigen_value_ = new double[input_->size ()]{}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the performance, but couldn't you use here std::vector
? Same below
@@ -185,8 +185,7 @@ void pcl::ColorModality<PointInT>::extractFeatures (const MaskMap & mask, | |||
for (std::size_t map_index = 0; map_index < 8; ++map_index) | |||
mask_maps[map_index].resize (width, height); | |||
|
|||
unsigned char map[255]; | |||
memset(map, 0, 255); | |||
unsigned char map[255]{}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsigned char map[255]{}; | |
std::array<unsigned char, 255> map{}; |
@@ -931,8 +930,7 @@ pcl::SurfaceNormalModality<PointInT>::computeAndQuantizeSurfaceNormals2 () | |||
} | |||
/*cvSmooth(m_dep[0], m_dep[0], CV_MEDIAN, 5, 5);*/ | |||
|
|||
unsigned char map[255]; | |||
memset(map, 0, 255); | |||
unsigned char map[255]{}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsigned char map[255]{}; | |
std::array<unsigned char, 255> map{}; |
@@ -981,8 +979,7 @@ pcl::SurfaceNormalModality<PointInT>::extractFeatures (const MaskMap & mask, | |||
for (auto &mask_map : mask_maps) | |||
mask_map.resize (width, height); | |||
|
|||
unsigned char map[255]; | |||
memset(map, 0, 255); | |||
unsigned char map[255]{}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsigned char map[255]{}; | |
std::array<unsigned char, 255> map{}; |
@@ -1252,8 +1249,7 @@ pcl::SurfaceNormalModality<PointInT>::extractAllFeatures ( | |||
for (auto &mask_map : mask_maps) | |||
mask_map.resize (width, height); | |||
|
|||
unsigned char map[255]; | |||
memset(map, 0, 255); | |||
unsigned char map[255]{}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsigned char map[255]{}; | |
std::array<unsigned char, 255> map{}; |
@@ -226,8 +226,7 @@ main (int argc, char** argv) | |||
} | |||
|
|||
// Retrieve the entries from the image data and copy them into the output RGB cloud | |||
double* pixel = new double [4]; | |||
memset (pixel, 0, sizeof (double) * 4); | |||
double* pixel = new double [4]{0.0}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
double* pixel = new double [4]{0.0}; | |
std::array<double, 4> pixel{}; |
0.0147f, 0.1222f, 0.3568f, 0.4348f, 0.0149f, 0.0806f, 0.2787f, 0.6864f}; | ||
std::copy_n (data, 32, correct_rift_feature_values); | ||
} | ||
const float correct_rift_feature_values[32] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const float correct_rift_feature_values[32] = | |
std::array<float, 32> correct_rift_feature_values{}; |
On this way you can use a for-ranges loop below.
I would prefer to not make those changes here since this is already quite big and dragging on for two years 🙂 |
Sorry, I didn't realize that I had a filter set on my Find in Files... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found some more instances where e.g. floats are copied to or from a data blob (vector of std::uint8_t). I would prefer to keep memcpy for such cases (makes no sense to see it as "copy four individual and independent std::uint8_t"):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
About 20 files left to review
@@ -309,8 +307,6 @@ pcl::UnaryClassifier<PointT>::queryFeatureDistances (std::vector<pcl::PointCloud | |||
{ | |||
// Query point | |||
flann::Matrix<float> p = flann::Matrix<float>(new float[n_col], 1, n_col); | |||
memcpy (&p.ptr ()[0], (*query_features)[i].histogram, p.cols * p.rows * sizeof (float)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This definitely has to be initialized since its an input for knnSearch
(indices
and distances
are output).
std::copy((*query_features)[i].histogram, (*query_features)[i].histogram + n_col, p.ptr())
should work (1*n_col
is the same as p.cols*p.rows
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a few more places (mostly in the io module) where bytes/data blobs are copied and IMO it doesn't make much sense to replace memcpy
with copy
(in ply_io.cpp: memset
/fill_n
). Please check and undo if you agree:
- common/include/pcl/conversions.h
- cuda/apps/src/kinect_debayering.cpp
- io/src/hdl_grabber.cpp
- io/src/image_depth.cpp
- io/src/image_ir.cpp
- io/src/image_rgb24.cpp
- io/src/libpng_wrapper.cpp
- io/src/openni_camera/openni_depth_image.cpp
- io/src/openni_camera/openni_image_rgb24.cpp
- io/src/openni_camera/openni_ir_image.cpp
- io/src/ply_io.cpp
- surface/src/vtk_smoothing/vtk_utils.cpp
- tools/openni_image.cpp
I retained |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ready to merge from my side! Thank you for your endurance 😄
I will wait a few more days and then merge if nothing has come up until then
* Fix multiple memory corruption errors revealed by fuzzing Fixes the following crashes: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=32178 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=32228 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=33217 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=33762 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=34134 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=35640 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=36663 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=37041 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=37125 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=39438 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=40058 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=40120 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=41152 * Merge with ply_io.cpp from #4299 * Added PCLPointCloud2::at * more `at` * more memcpy eliminated * remove spaces * Update io/src/ply_io.cpp * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Markus Vieth <39675748+mvieth@users.noreply.github.com> * Fix origins of warnings Co-authored-by: Markus Vieth <39675748+mvieth@users.noreply.github.com>
Per C++ Coding Standards item 96:
memset
is also considered harmful.For this PR:
For the most part,
memset
was replaced bystd::fill_n
or, in many cases, by zero initializationThere are many, many instances of
memcpy
in PCL that are used to copy various kinds ofPoint
data asuint8_t
tofloat
; I left these alone.Otherwise, where possible,
memcpy
was replaced bystd::copy
orstd::copy_n
The only instances of
memcmp
(outside of third-party code) were in CUDA code; I left those alone.#3368