Skip to content
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

Merged

Conversation

gnawme
Copy link
Contributor

@gnawme gnawme commented Jul 30, 2020

Per C++ Coding Standards item 96:

memcpy and memcmp violate the type system. Using memcpy to copy objects is like making money using a photocopier. Using memcmp to compare objects is like comparing leopards by counting their spots. The tools and methods might appear to do the job, but they are too coarse to do it acceptably.

memset is also considered harmful.

For this PR:

  • For the most part, memset was replaced by std::fill_n or, in many cases, by zero initialization

  • There are many, many instances of memcpy in PCL that are used to copy various kinds of Point data as uint8_t to float; I left these alone.

  • Otherwise, where possible, memcpy was replaced by std::copy or std::copy_n

  • The only instances of memcmp (outside of third-party code) were in CUDA code; I left those alone.

#3368

@gnawme gnawme force-pushed the norm.evangelista/PCL-3368-change-memcpy branch from 3bfb1d7 to 97061fc Compare July 31, 2020 01:08
Copy link
Member

@kunaltyagi kunaltyagi left a 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)
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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

visualization/src/vtk/pcl_context_item.cpp Show resolved Hide resolved
Copy link
Member

@kunaltyagi kunaltyagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9/98

common/include/pcl/common/impl/io.hpp Outdated Show resolved Hide resolved
common/include/pcl/common/impl/io.hpp Outdated Show resolved Hide resolved
common/include/pcl/common/io.h Outdated Show resolved Hide resolved
common/include/pcl/common/io.h Outdated Show resolved Hide resolved
common/include/pcl/conversions.h Outdated Show resolved Hide resolved
common/include/pcl/pcl_macros.h Outdated Show resolved Hide resolved
common/include/pcl/pcl_macros.h Outdated Show resolved Hide resolved
common/include/pcl/range_image/impl/range_image.hpp Outdated Show resolved Hide resolved
common/include/pcl/range_image/impl/range_image.hpp Outdated Show resolved Hide resolved
Copy link
Member

@kunaltyagi kunaltyagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

15/100

common/include/pcl/common/io.h Show resolved Hide resolved
common/include/pcl/conversions.h Outdated Show resolved Hide resolved
common/include/pcl/range_image/impl/range_image.hpp Outdated Show resolved Hide resolved
common/src/io.cpp Outdated Show resolved Hide resolved
Copy link
Member

@kunaltyagi kunaltyagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

34/110

io/src/lzf.cpp Show resolved Hide resolved
io/src/lzf_image_io.cpp Outdated Show resolved Hide resolved
recognition/include/pcl/recognition/ransac_based/bvh.h Outdated Show resolved Hide resolved
@kunaltyagi
Copy link
Member

PS: Builds are failing (Ignore MSVC for a while)

@gnawme
Copy link
Contributor Author

gnawme commented Aug 2, 2020

PS: Builds are failing (Ignore MSVC for a while)

Yes, unit tests are failing in CovarianceSampling.Filters. So far, I'm only able to resolve it to something in Eigen.

Copy link
Member

@kunaltyagi kunaltyagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

43/110

gpu/people/src/cuda/nvidia/NPP_staging.cu Outdated Show resolved Hide resolved
io/src/ifs_io.cpp Outdated Show resolved Hide resolved
io/src/image_depth.cpp Outdated Show resolved Hide resolved
io/src/pcd_grabber.cpp Outdated Show resolved Hide resolved
Copy link
Member

@kunaltyagi kunaltyagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

56/110

common/src/io.cpp Outdated Show resolved Hide resolved
filters/include/pcl/filters/impl/covariance_sampling.hpp Outdated Show resolved Hide resolved
gpu/people/src/cuda/nvidia/NPP_staging.cu Outdated Show resolved Hide resolved
Copy link
Member

@kunaltyagi kunaltyagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

67/110

filters/include/pcl/filters/impl/covariance_sampling.hpp Outdated Show resolved Hide resolved
gpu/people/src/cuda/nvidia/NPP_staging.cu Outdated Show resolved Hide resolved
io/include/pcl/compression/impl/entropy_range_coder.hpp Outdated Show resolved Hide resolved
io/include/pcl/compression/impl/entropy_range_coder.hpp Outdated Show resolved Hide resolved
io/include/pcl/compression/impl/entropy_range_coder.hpp Outdated Show resolved Hide resolved
io/src/lzf_image_io.cpp Outdated Show resolved Hide resolved
io/src/pcd_grabber.cpp Outdated Show resolved Hide resolved
ml/src/svm.cpp Outdated Show resolved Hide resolved
Copy link
Member

@kunaltyagi kunaltyagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

76/110

surface/include/pcl/surface/impl/convex_hull.hpp Outdated Show resolved Hide resolved
surface/include/pcl/surface/impl/convex_hull.hpp Outdated Show resolved Hide resolved
surface/include/pcl/surface/3rdparty/poisson4/mat.hpp Outdated Show resolved Hide resolved
Copy link
Member

@kunaltyagi kunaltyagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

86/110

io/src/pcd_io.cpp Outdated Show resolved Hide resolved
io/src/pcd_io.cpp Outdated Show resolved Hide resolved
io/src/pcd_io.cpp Outdated Show resolved Hide resolved
io/src/ply_io.cpp Outdated Show resolved Hide resolved
keypoints/include/pcl/keypoints/impl/iss_3d.hpp Outdated Show resolved Hide resolved
octree/include/pcl/octree/octree_key.h Outdated Show resolved Hide resolved
outofcore/src/cJSON.cpp Outdated Show resolved Hide resolved
outofcore/src/cJSON.cpp Show resolved Hide resolved
surface/include/pcl/surface/impl/convex_hull.hpp Outdated Show resolved Hide resolved
surface/include/pcl/surface/impl/convex_hull.hpp Outdated Show resolved Hide resolved
@kunaltyagi kunaltyagi self-requested a review August 6, 2020 02:54
@gnawme gnawme force-pushed the norm.evangelista/PCL-3368-change-memcpy branch from 63472c7 to 5f1b5a4 Compare August 6, 2020 14:55
Copy link
Member

@kunaltyagi kunaltyagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

94/110

outofcore/src/cJSON.cpp Outdated Show resolved Hide resolved
gpu/people/src/cuda/nvidia/NPP_staging.cu Show resolved Hide resolved
surface/include/pcl/surface/impl/convex_hull.hpp Outdated Show resolved Hide resolved
@gnawme gnawme force-pushed the norm.evangelista/PCL-3368-change-memcpy branch 3 times, most recently from 15da3fd to 4898c95 Compare August 13, 2020 00:02
sample_consensus/include/pcl/sample_consensus/sac_model.h Outdated Show resolved Hide resolved
recognition/src/linemod.cpp Outdated Show resolved Hide resolved
recognition/src/linemod.cpp Outdated Show resolved Hide resolved
recognition/src/linemod.cpp Outdated Show resolved Hide resolved
recognition/src/linemod.cpp Outdated Show resolved Hide resolved
recognition/src/linemod.cpp Outdated Show resolved Hide resolved
Copy link
Member

@kunaltyagi kunaltyagi left a 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

surface/include/pcl/surface/impl/convex_hull.hpp Outdated Show resolved Hide resolved
@@ -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));
Copy link
Member

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).

Copy link
Contributor Author

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?

Copy link
Member

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

Copy link
Member

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)

io/src/pcd_io.cpp Outdated Show resolved Hide resolved
outofcore/src/cJSON.cpp Outdated Show resolved Hide resolved
io/src/ply_io.cpp Outdated Show resolved Hide resolved
io/src/ply_io.cpp Outdated Show resolved Hide resolved
io/src/ply_io.cpp Outdated Show resolved Hide resolved
outofcore/src/cJSON.cpp Outdated Show resolved Hide resolved
Copy link
Member

@kunaltyagi kunaltyagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files left

io/src/lzf_image_io.cpp Outdated Show resolved Hide resolved
@gnawme gnawme force-pushed the norm.evangelista/PCL-3368-change-memcpy branch from 1fbddd5 to e1b8278 Compare August 22, 2020 05:09
Copy link
Contributor

@SunBlack SunBlack left a 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]{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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]{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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]{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 ()]{};
Copy link
Contributor

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]{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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]{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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]{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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]{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@mvieth
Copy link
Member

mvieth commented Jul 26, 2022

Maybe could make the std::array chnges also in another MR - just saw in this MR, that we could modernize them here.

I would prefer to not make those changes here since this is already quite big and dragging on for two years 🙂

common/src/projection_matrix.cpp Outdated Show resolved Hide resolved
common/include/pcl/conversions.h Show resolved Hide resolved
ml/include/pcl/ml/permutohedral.h Show resolved Hide resolved
@gnawme
Copy link
Contributor Author

gnawme commented Jul 28, 2022

I still found instances of std::copy changed to std::copy_n

Sorry, I didn't realize that I had a filter set on my Find in Files...

Copy link
Member

@mvieth mvieth left a 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"):

apps/src/grabcut_2d.cpp Outdated Show resolved Hide resolved
Copy link
Member

@mvieth mvieth left a 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

keypoints/include/pcl/keypoints/impl/iss_3d.hpp Outdated Show resolved Hide resolved
@@ -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));
Copy link
Member

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)

Copy link
Member

@mvieth mvieth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recognition/src/linemod.cpp Outdated Show resolved Hide resolved
@gnawme
Copy link
Contributor Author

gnawme commented Aug 5, 2022

Please check and undo if you agree

I retained std::copy if it was like-type to like-type, otherwise I reverted

io/src/ply_io.cpp Outdated Show resolved Hide resolved
common/include/pcl/conversions.h Outdated Show resolved Hide resolved
io/src/hdl_grabber.cpp Show resolved Hide resolved
Copy link
Member

@mvieth mvieth left a 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

@mvieth mvieth added changelog: enhancement Meta-information for changelog generation and removed needs: more work Specify why not closed/merged yet labels Aug 16, 2022
@mvieth mvieth merged commit fa3da0b into PointCloudLibrary:master Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: enhancement Meta-information for changelog generation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants