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

Prevent exception in PCDReader for misformed PCD files #4566

Merged
merged 3 commits into from
Feb 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions io/src/pcd_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.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);

// Setting the is_dense property to true by default
cloud.is_dense = true;
Expand All @@ -447,6 +451,14 @@ 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 while accessing st
{
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;
}

if (idx >= nr_points)
{
PCL_WARN ("[pcl::PCDReader::read] input has more points (%d) than advertised (%d)!\n", idx, nr_points);
Expand Down