pcl icon indicating copy to clipboard operation
pcl copied to clipboard

Check if read succedded before reading again/allocating memory

Open kunaltyagi opened this issue 6 years ago • 16 comments

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

kunaltyagi avatar Jan 29 '20 15:01 kunaltyagi

Should i fix the 273 instances ?

omar-bakr avatar Feb 25 '20 15:02 omar-bakr

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

shrijitsingh99 avatar Feb 27 '20 04:02 shrijitsingh99

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.

kunaltyagi avatar Feb 27 '20 04:02 kunaltyagi

Raise exception and let the upper layers in the stack decide what to do with it.

SergioRAgostinho avatar Feb 27 '20 09:02 SergioRAgostinho

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?

shrijitsingh99 avatar Feb 27 '20 21:02 shrijitsingh99

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?

SergioRAgostinho avatar Feb 28 '20 07:02 SergioRAgostinho

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.

shrijitsingh99 avatar Mar 03 '20 04:03 shrijitsingh99

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?

0sidharth avatar Mar 10 '20 12:03 0sidharth

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.

SergioRAgostinho avatar Mar 10 '20 13:03 SergioRAgostinho

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?

0sidharth avatar Mar 10 '20 14:03 0sidharth

There's no value in catching an exception you just threw. This exception handling (try...catch) is for the person who called the function

kunaltyagi avatar Mar 10 '20 14:03 kunaltyagi

Oh, right. So this snippet without the try and catch, and checking for error before allocating surface_patch_ looks okay?

0sidharth avatar Mar 10 '20 14:03 0sidharth

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.

kunaltyagi avatar Mar 10 '20 14:03 kunaltyagi

  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?

0sidharth avatar Mar 14 '20 16:03 0sidharth

The snippet provided here is correct. I've 2 more comments on this snippet:

  • I'd use file.fail() or file.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()

kunaltyagi avatar Mar 15 '20 02:03 kunaltyagi

Marking this as stale due to 30 days of inactivity. It will be closed in 7 days if no further activity occurs.

stale[bot] avatar May 19 '20 06:05 stale[bot]