Check if read succedded before reading again/allocating memory
Your Environment
- PCL Version: HEAD
Context
Bad parsing is inevitable, specially if operations are performed on read data without validation
Expected Behavior
Graceful error detection
Current Behavior
Files: 273 instances. One sample provided:
Possible Solution
Sanity checks on input stream after reading before allocating memory
Should i fix the 273 instances ?
@kunaltyagi Could you expand upon what exactly you mean by "graceful error detection" as failure to read the stream can lead to unexpected behavior. Not sure what we be a good method to handle this.
failure to read the stream can lead to unexpected behavior
This should be factored in the operation. Failure to read should inform the user with proper debug information programmatically (like return a false, or raise exception or something) along with log in debug mode.
Raise exception and let the upper layers in the stack decide what to do with it.
Since there exists no exception for failure to read, @SergioRAgostinho should I go ahead and create a new exception in pcl/exceptions.h as it would be useful at multiple places?
What about this one https://github.com/PointCloudLibrary/pcl/blob/e5b8fe5051283263097f843f635c7ee94a09483b/common/include/pcl/exceptions.h#L176-L188
Do you feel it's too generic?
Yeah I thought it would be too generic but after looking at all the case, I guess most cases would fit under IOException If a need for a more specific error is needed will raise it over here, for now, will use IOException.
try {
file.read(reinterpret_cast<char*>(&surface_patch_pixel_size_), sizeof(surface_patch_pixel_size_));
surface_patch_ = new float[surface_patch_pixel_size_*surface_patch_pixel_size_];
}
catch(pcl::IOException& e)
{
std::cerr << e.what() << std::endl;
}
Is something like this alright? Just want to confirm before actually implementing it to the 273 instances.
Would it perhaps be better to write a function that performs this sanity check, instead of writing try and catch statements at all the instances?
In your example file is of std::istream type which knows nothing about pcl's exceptions and by default it does not throw exceptions. It uses a status bit to indicate if something went wrong.
If such case happens, then we want to throw a pcl::IOException.
Answering directly your question: the snippet is not what we want. Take some time to read https://en.cppreference.com/w/cpp/io/basic_istream/read carefully.
try
{
file.read(reinterpret_cast<char*>(&surface_patch_pixel_size_), ``sizeof(surface_patch_pixel_size_));
surface_patch_ = new float[surface_patch_pixel_size_*surface_patch_pixel_size_];
if (file.std::ios::bad())
{
PCL_THROW_EXCEPTION (pcl::IOException, "Failure reading data from file");
}
else if(file .std::ios::fail())
{
PCL_THROW_EXCEPTION (pcl::IOException, "Failure in opening file");
}
}
catch(pcl::IOException& e)
{
std::cerr << e.what() << std::endl;
}
What about this?
There's no value in catching an exception you just threw. This exception handling (try...catch) is for the person who called the function
Oh, right. So this snippet without the try and catch, and checking for error before allocating surface_patch_ looks okay?
No @0sidharth , it is syntactically wrong. Please try to compile your snippets. I'd recommend a Unix machine or Windows + MS VS 2019 or https://godbolt.org as good places to start.
file.read(reinterpret_cast<char*>(&surface_patch_pixel_size_), sizeof(surface_patch_pixel_size_));
if (file.std::ios::bad())
{
PCL_THROW_EXCEPTION (pcl::IOException, "Failure reading data from file");
}
else if(file .std::ios::fail())
{
PCL_THROW_EXCEPTION (pcl::IOException, "Failure in opening file");
}
else
{
surface_patch_ = new float[surface_patch_pixel_size_*surface_patch_pixel_size_];
}
I was talking about this @kunaltyagi , and it is compiling for me so I cannot figure out which syntatic error you are referring to?
The snippet provided here is correct. I've 2 more comments on this snippet:
- I'd use
file.fail()orfile.bad()instead of qualifying the function - Please also note that failbit can be set for reasons other than "failure to open", including failure in the call
file.read()
Marking this as stale due to 30 days of inactivity. It will be closed in 7 days if no further activity occurs.