Skip to content

Commit

Permalink
Fix PCD load crashes on empty files (#5855)
Browse files Browse the repository at this point in the history
* return -1 from PCDReader::readHeader() when input file is completely empty

* Return empty string from getFieldsList() if input cloud has no fields

* Update io/src/pcd_io.cpp

Co-authored-by: Markus Vieth <39675748+mvieth@users.noreply.github.com>

* Update common/include/pcl/common/io.h

---------

Co-authored-by: Markus Vieth <39675748+mvieth@users.noreply.github.com>
  • Loading branch information
themightyoarfish and mvieth committed Oct 30, 2023
1 parent 21dcddd commit 81b92a5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 8 additions & 2 deletions common/include/pcl/common/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ namespace pcl
inline std::string
getFieldsList (const pcl::PCLPointCloud2 &cloud)
{
return std::accumulate(std::next (cloud.fields.begin ()), cloud.fields.end (), cloud.fields[0].name,
[](const auto& acc, const auto& field) { return acc + " " + field.name; });
if (cloud.fields.empty())
{
return "";
} else
{
return std::accumulate(std::next (cloud.fields.begin ()), cloud.fields.end (), cloud.fields[0].name,
[](const auto& acc, const auto& field) { return acc + " " + field.name; });
}
}

/** \brief Obtains the size of a specific field data type in bytes
Expand Down
9 changes: 8 additions & 1 deletion io/src/pcd_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,14 @@ pcl::PCDReader::readHeader (const std::string &file_name, pcl::PCLPointCloud2 &c
fs.open (file_name.c_str (), std::ios::binary);
if (!fs.is_open () || fs.fail ())
{
PCL_ERROR ("[pcl::PCDReader::readHeader] Could not open file '%s'! Error : %s\n", file_name.c_str (), strerror (errno));
PCL_ERROR ("[pcl::PCDReader::readHeader] Could not open file '%s'! Error : %s\n", file_name.c_str (), strerror (errno));
fs.close ();
return (-1);
}

if (fs.peek() == std::ifstream::traits_type::eof())
{
PCL_ERROR ("[pcl::PCDReader::readHeader] File '%s' is empty.\n", file_name.c_str ());
fs.close ();
return (-1);
}
Expand Down

0 comments on commit 81b92a5

Please sign in to comment.