From 35fc8f80c0d755dba0637bd588dcd7fb8ad09464 Mon Sep 17 00:00:00 2001 From: Markus Vieth Date: Tue, 5 Jan 2021 15:56:31 +0100 Subject: [PATCH 1/3] Prevent exception in PCDReader for misformed PCD files --- io/src/pcd_io.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/io/src/pcd_io.cpp b/io/src/pcd_io.cpp index a2518655462..3719bc2dd2c 100644 --- a/io/src/pcd_io.cpp +++ b/io/src/pcd_io.cpp @@ -424,6 +424,10 @@ pcl::PCDReader::readBodyASCII (std::istream &fs, pcl::PCLPointCloud2 &cloud, int { // Get the number of points the cloud should have unsigned int nr_points = cloud.width * cloud.height; + // The number of elements each line/point should have + const unsigned int elems_per_line = std::accumulate (cloud.fields.begin (), cloud.fields.end (), 0u, + [](const auto& i, const auto& field){ return (i + field.count); }); + PCL_DEBUG ("[pcl::PCDReader::readBodyASCII] Will check that each line in the PCD file has %u elements.\n", elems_per_line); // Setting the is_dense property to true by default cloud.is_dense = true; @@ -447,6 +451,13 @@ pcl::PCDReader::readBodyASCII (std::istream &fs, pcl::PCLPointCloud2 &cloud, int boost::trim (line); boost::split (st, line, boost::is_any_of ("\t\r "), boost::token_compress_on); + if (st.size () != elems_per_line) // If this is not checked, an exception might occur when accessing st + { + PCL_WARN ("[pcl::PCDReader::readBodyASCII] Possibly misformed PCD file: line has %zu elements, but should have %u!\n", st.size (), elems_per_line); + ++idx; // Skip this line/point, but read all others + continue; + } + if (idx >= nr_points) { PCL_WARN ("[pcl::PCDReader::read] input has more points (%d) than advertised (%d)!\n", idx, nr_points); From bef71d30c3a1af1ed8d7e9e13b9d64d66a3b8915 Mon Sep 17 00:00:00 2001 From: Markus Vieth Date: Wed, 6 Jan 2021 11:50:27 +0100 Subject: [PATCH 2/3] Print pointer number --- io/src/pcd_io.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/io/src/pcd_io.cpp b/io/src/pcd_io.cpp index 3719bc2dd2c..622d514196b 100644 --- a/io/src/pcd_io.cpp +++ b/io/src/pcd_io.cpp @@ -453,7 +453,8 @@ pcl::PCDReader::readBodyASCII (std::istream &fs, pcl::PCLPointCloud2 &cloud, int if (st.size () != elems_per_line) // If this is not checked, an exception might occur when accessing st { - PCL_WARN ("[pcl::PCDReader::readBodyASCII] Possibly misformed PCD file: line has %zu elements, but should have %u!\n", st.size (), elems_per_line); + PCL_WARN ("[pcl::PCDReader::readBodyASCII] Possibly misformed PCD file: line has %zu elements, but should have %u! (point %u)\n", + st.size (), elems_per_line, idx+1); ++idx; // Skip this line/point, but read all others continue; } From 3129025101bac93a639bb84778fdfba53f2abe72 Mon Sep 17 00:00:00 2001 From: Markus Vieth Date: Tue, 2 Feb 2021 11:55:14 +0100 Subject: [PATCH 3/3] Minor changes/rewordings --- io/src/pcd_io.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/io/src/pcd_io.cpp b/io/src/pcd_io.cpp index 622d514196b..b5898b3d3ed 100644 --- a/io/src/pcd_io.cpp +++ b/io/src/pcd_io.cpp @@ -425,7 +425,7 @@ pcl::PCDReader::readBodyASCII (std::istream &fs, pcl::PCLPointCloud2 &cloud, int // Get the number of points the cloud should have unsigned int nr_points = cloud.width * cloud.height; // The number of elements each line/point should have - const unsigned int elems_per_line = std::accumulate (cloud.fields.begin (), cloud.fields.end (), 0u, + const unsigned int elems_per_line = std::accumulate (cloud.fields.cbegin (), cloud.fields.cend (), 0u, [](const auto& i, const auto& field){ return (i + field.count); }); PCL_DEBUG ("[pcl::PCDReader::readBodyASCII] Will check that each line in the PCD file has %u elements.\n", elems_per_line); @@ -451,10 +451,10 @@ pcl::PCDReader::readBodyASCII (std::istream &fs, pcl::PCLPointCloud2 &cloud, int boost::trim (line); boost::split (st, line, boost::is_any_of ("\t\r "), boost::token_compress_on); - if (st.size () != elems_per_line) // If this is not checked, an exception might occur when accessing st + if (st.size () != elems_per_line) // If this is not checked, an exception might occur while accessing st { - PCL_WARN ("[pcl::PCDReader::readBodyASCII] Possibly misformed PCD file: line has %zu elements, but should have %u! (point %u)\n", - st.size (), elems_per_line, idx+1); + PCL_WARN ("[pcl::PCDReader::readBodyASCII] Possibly malformed PCD file: point number %u has %zu elements, but should have %u\n", + idx+1, st.size (), elems_per_line); ++idx; // Skip this line/point, but read all others continue; }