secure all >> in readers

This commit is contained in:
Maxime Gimeno 2020-05-13 11:46:18 +02:00
parent d6812d9561
commit 640f34c975
6 changed files with 339 additions and 59 deletions

View File

@ -134,7 +134,7 @@ if (VTK_FOUND)
include_directories(${3MF_INCLUDE_DIR}) include_directories(${3MF_INCLUDE_DIR})
create_single_source_cgal_program( test_3mf_to_sm.cpp ) create_single_source_cgal_program( test_3mf_to_sm.cpp )
target_link_libraries(test_3mf_to_sm PRIVATE ${3MF_LIBRARIES}) target_link_libraries(test_3mf_to_sm PRIVATE ${3MF_LIBRARIES})
add_definitions(-DCGAL_LINKED_WITH_3MF) target_compile_definitions(test_3mf_to_sm PRIVATE -DCGAL_LINKED_WITH_3MF)
else() else()
message(STATUS "NOTICE : This program requires the lib3MF library, and will not be compiled.") message(STATUS "NOTICE : This program requires the lib3MF library, and will not be compiled.")
endif() endif()

View File

@ -93,7 +93,7 @@ if(3MF_LIBRARIES AND 3MF_INCLUDE_DIR AND EXISTS "${3MF_INCLUDE_DIR}/Model/COM/NM
include_directories(${3MF_INCLUDE_DIR}) include_directories(${3MF_INCLUDE_DIR})
polyhedron_demo_plugin(3mf_io_plugin 3mf_io_plugin KEYWORDS IO PMP) polyhedron_demo_plugin(3mf_io_plugin 3mf_io_plugin KEYWORDS IO PMP)
target_link_libraries(3mf_io_plugin PRIVATE scene_surface_mesh_item scene_points_with_normal_item scene_polylines_item ${3MF_LIBRARIES}) target_link_libraries(3mf_io_plugin PRIVATE scene_surface_mesh_item scene_points_with_normal_item scene_polylines_item ${3MF_LIBRARIES})
add_definitions(-DCGAL_LINKED_WITH_3MF) target_compile_definitions(3mf_io_plugin PRIVATE -DCGAL_LINKED_WITH_3MF)
else() else()
message(STATUS "NOTICE : The 3mf_io_plugin requires the lib3MF library in a version < 2.0, and will not be compiled.") message(STATUS "NOTICE : The 3mf_io_plugin requires the lib3MF library in a version < 2.0, and will not be compiled.")
endif() endif()

View File

@ -61,13 +61,23 @@ bool read_GOCAD(std::istream& input,
if((idx = s.find("name")) != std::string::npos) if((idx = s.find("name")) != std::string::npos)
{ {
std::istringstream str(s.substr(idx + 5)); std::istringstream str(s.substr(idx + 5));
str >> name_and_color.first; if(!(str >> name_and_color.first))
{
if(verbose)
std::cerr<<"error while reading expected name. "<<std::endl;
return false;
}
} }
if((idx = s.find("color")) != std::string::npos) if((idx = s.find("color")) != std::string::npos)
{ {
std::istringstream str(s.substr(idx + 6)); std::istringstream str(s.substr(idx + 6));
str >> name_and_color.second; if(!(str >> name_and_color.second))
{
if(verbose)
std::cerr<<"error while reading expected color. "<<std::endl;
return false;
}
} }
} }
std::getline(input, s); std::getline(input, s);
@ -76,7 +86,12 @@ bool read_GOCAD(std::istream& input,
{ {
if((c == 'V') || (c == 'P')) if((c == 'V') || (c == 'P'))
{ {
input >> s >> i >> p; // @fixme check for failure if(!(input >> s >> i >> p))
{
if(verbose)
std::cerr<<"error while reading vertex. "<<std::endl;
return false;
}
if(!vertices_read) if(!vertices_read)
{ {
vertices_read = true; vertices_read = true;
@ -87,7 +102,12 @@ bool read_GOCAD(std::istream& input,
} }
else if(vertices_read && (c == 'T')) else if(vertices_read && (c == 'T'))
{ {
input >> c >> c >> c >> i >> j >> k; if(!(input >> c >> c >> c >> i >> j >> k))
{
if(verbose)
std::cerr<<"error while reading triangle. "<<std::endl;
return false;
}
CGAL_Polygon new_face(3); CGAL_Polygon new_face(3);
new_face[0] = offset+i; new_face[0] = offset+i;
new_face[1] = offset+j; new_face[1] = offset+j;

View File

@ -54,9 +54,12 @@ bool read_OBJ(std::istream& is,
if(line[0] == 'v' && line[1] == ' ') if(line[0] == 'v' && line[1] == ' ')
{ {
std::istringstream iss(line.substr(1)); std::istringstream iss(line.substr(1));
iss >> p; // @fixme check successful reading if(!(iss >> p) || !iss)
if(!iss) {
if(verbose)
std::cerr<<"error while reading OBJ vertex. "<<std::endl;
return false; return false;
}
points.push_back(p); points.push_back(p);
} }
@ -89,7 +92,13 @@ bool read_OBJ(std::istream& is,
{ {
std::istringstream iss(line); std::istringstream iss(line);
std::string dummy; std::string dummy;
iss >> dummy; if(!(iss >> dummy))
{
if(verbose)
std::cerr<<"error while reading OBJ vertex normal. "<<std::endl;
return false;
}
double nx, ny, nz; // @fixme double? double nx, ny, nz; // @fixme double?
if(iss >> nx >> ny >> nz) if(iss >> nx >> ny >> nz)
*vn_out++ = Normal(nx, ny, nz); // @fixme check that every vertex has a normal? *vn_out++ = Normal(nx, ny, nz); // @fixme check that every vertex has a normal?

View File

@ -71,11 +71,23 @@ public:
else else
{ {
skip_comment(); skip_comment();
m_in >> iformat(x) >> iformat(y) >> iformat(z); if(!(m_in >> iformat(x) >> iformat(y) >> iformat(z)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
if(is_homogeneous()) if(is_homogeneous())
{ {
float w; float w;
m_in >> iformat(w); if(!(m_in >> iformat(w)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
x /= w; x /= w;
y /= w; y /= w;
z /= w; z /= w;
@ -105,11 +117,24 @@ public:
else else
{ {
skip_comment(); skip_comment();
m_in >> iformat(x) >> iformat(y) >> iformat(z); if(!(m_in >> iformat(x) >> iformat(y) >> iformat(z)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
if(is_homogeneous()) if(is_homogeneous())
{ {
double w; double w;
m_in >> iformat(w); m_in >> iformat(w);
if(!(m_in >> iformat(w)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
x /= w; x /= w;
y /= w; y /= w;
z /= w; z /= w;
@ -143,7 +168,13 @@ public:
if(is_homogeneous()) if(is_homogeneous())
{ {
double fx, fy, fz, fw; double fx, fy, fz, fw;
m_in >> iformat(fx) >> iformat(fy) >> iformat(fz) >> iformat(fw); if(!(m_in >> iformat(fx) >> iformat(fy) >> iformat(fz) >> iformat(fw)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
x = int(fx / fw); x = int(fx / fw);
y = int(fy / fw); y = int(fy / fw);
z = int(fz / fw); z = int(fz / fw);
@ -151,11 +182,29 @@ public:
else else
{ {
double d; double d;
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
x = int(d); x = int(d);
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
y = int(d); y = int(d);
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
z = int(d); z = int(d);
} }
} }
@ -175,9 +224,21 @@ public:
else else
{ {
skip_comment(); skip_comment();
m_in >> iformat(x) >> iformat(y) >> iformat(z); if(!(m_in >> iformat(x) >> iformat(y) >> iformat(z)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
if(is_homogeneous()) if(is_homogeneous())
m_in >> iformat(w); if(!(m_in >> iformat(w)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
} }
} }
void scan_vertex(double& x, double& y, double& z, double& w) void scan_vertex(double& x, double& y, double& z, double& w)
@ -201,11 +262,22 @@ public:
else else
{ {
skip_comment(); skip_comment();
m_in >> iformat(x); if(!(m_in >> iformat(x) >> iformat(y) >> iformat(z)))
m_in >> iformat(y); {
m_in >> iformat(z); m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
if(is_homogeneous()) if(is_homogeneous())
m_in >> iformat(w); if(!(m_in >> iformat(w)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
} }
} }
@ -231,15 +303,39 @@ public:
{ {
skip_comment(); skip_comment();
double d; double d;
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
x = int(d); x = int(d);
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
y = int(d); y = int(d);
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
z = int(d); z = int(d);
if(is_homogeneous()) if(is_homogeneous())
{ {
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading vertex."<<std::endl;
return;
}
w = int(d); w = int(d);
} }
} }
@ -271,13 +367,25 @@ public:
if(is_homogeneous()) if(is_homogeneous())
{ {
float fx, fy, fw; float fx, fy, fw;
m_in >> iformat(fx) >> iformat(fy) >> iformat(fw); if(!(m_in >> iformat(fx) >> iformat(fy) >> iformat(fw)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading texture."<<std::endl;
return;
}
x = fx / fw; x = fx / fw;
y = fy / fw; y = fy / fw;
} }
else else
{ {
m_in >> iformat(x) >> iformat(y); if(!(m_in >> iformat(x) >> iformat(y)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading texture."<<std::endl;
return;
}
} }
} }
} }
@ -310,13 +418,25 @@ public:
if(is_homogeneous()) if(is_homogeneous())
{ {
float fx, fy, fw; float fx, fy, fw;
m_in >> iformat(fx) >> iformat(fy) >> iformat(fw); if(!(m_in >> iformat(fx) >> iformat(fy) >> iformat(fw)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading texture."<<std::endl;
return;
}
x = fx / fw; x = fx / fw;
y = fy / fw; y = fy / fw;
} }
else else
{ {
m_in >> iformat(x) >> iformat(y); if(!(m_in >> iformat(x) >> iformat(y)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading texture."<<std::endl;
return;
}
} }
} }
} }
@ -343,11 +463,23 @@ public:
} }
else else
{ {
m_in >> iformat(x) >> iformat(y) >> iformat(z); if(!(m_in >> iformat(x) >> iformat(y) >> iformat(z)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
if(is_homogeneous()) if(is_homogeneous())
{ {
float w; float w;
m_in >> iformat(w); if(!(m_in >> iformat(w)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
x /= w; x /= w;
y /= w; y /= w;
z /= w; z /= w;
@ -386,14 +518,27 @@ public:
if(is_homogeneous()) if(is_homogeneous())
{ {
float fx, fy, fz, fw; float fx, fy, fz, fw;
m_in >> iformat(fx) >> iformat(fy) >> iformat(fz) >> iformat(fw); if(!(m_in >> iformat(fx) >> iformat(fy) >> iformat(fz) >> iformat(fw)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
x = fx / fw; x = fx / fw;
y = fy / fw; y = fy / fw;
z = fz / fw; z = fz / fw;
} }
else else
{ {
m_in >> iformat(x) >> iformat(y) >> iformat(z); if(!(m_in >> iformat(x) >> iformat(y) >> iformat(z)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
} }
} }
} }
@ -427,7 +572,13 @@ public:
if(is_homogeneous()) if(is_homogeneous())
{ {
float fx, fy, fz, fw; float fx, fy, fz, fw;
m_in >> iformat(fx) >> iformat(fy) >> iformat(fz) >> iformat(fw); if(!(m_in >> iformat(fx) >> iformat(fy) >> iformat(fz) >> iformat(fw)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
x = int(fx / fw); x = int(fx / fw);
y = int(fy / fw); y = int(fy / fw);
z = int(fz / fw); z = int(fz / fw);
@ -435,11 +586,30 @@ public:
else else
{ {
double d; double d;
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
x = int(d); x = int(d);
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
y = int(d); y = int(d);
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
z = int(d); z = int(d);
} }
} }
@ -462,9 +632,21 @@ public:
} }
else else
{ {
m_in >> iformat(x) >> iformat(y) >> iformat(z); if(!(m_in >> iformat(x) >> iformat(y) >> iformat(z)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
if(is_homogeneous()) if(is_homogeneous())
m_in >> iformat(w); if(!(m_in >> iformat(w)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
} }
} }
} }
@ -492,9 +674,21 @@ public:
} }
else else
{ {
m_in >> iformat(x) >> iformat(y) >> iformat(z); if(!(m_in >> iformat(x) >> iformat(y) >> iformat(z)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
if(is_homogeneous()) if(is_homogeneous())
m_in >> iformat(w); if(!(m_in >> iformat(w)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
} }
} }
} }
@ -522,15 +716,39 @@ public:
else else
{ {
double d; double d;
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
x = int(d); x = int(d);
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
y = int(d); y = int(d);
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
z = int(d); z = int(d);
if(is_homogeneous()) if(is_homogeneous())
{ {
m_in >> iformat(d); if(!(m_in >> iformat(d)))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading normal."<<std::endl;
return;
}
w = int(d); w = int(d);
} }
} }
@ -818,9 +1036,21 @@ public:
{ {
double dummy; double dummy;
if(is_homogeneous()) { if(is_homogeneous()) {
m_in >> dummy >> dummy >> dummy >> dummy; if(!(m_in >> dummy >> dummy >> dummy >> dummy))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading. Missing expected characters."<<std::endl;
return;
}
} else { } else {
m_in >> dummy >> dummy >> dummy; if(!(m_in >> dummy >> dummy >> dummy))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading. Missing expected characters."<<std::endl;
return;
}
} }
} }
@ -858,7 +1088,13 @@ public:
else else
{ {
skip_comment(); skip_comment();
m_in >> size; if(!(m_in >> size))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading facet. Missing size."<<std::endl;
return;
}
} }
} }
@ -872,7 +1108,13 @@ public:
} }
else else
{ {
m_in >> index; if(!(m_in >> index))
{
m_in.clear(std::ios::badbit);
if(verbose())
std::cerr<<"error while reading facet. Missing index."<<std::endl;
return;
}
} }
if(m_in.fail()) if(m_in.fail())

View File

@ -155,37 +155,46 @@ public:
void read_ascii(std::istream& stream, char& c) const void read_ascii(std::istream& stream, char& c) const
{ {
short s; short s;
stream >> s; if(stream >> s)
c = static_cast<char>(s); c = static_cast<char>(s);
else
stream.clear(std::ios::badbit);
} }
void read_ascii(std::istream& stream, signed char& c) const void read_ascii(std::istream& stream, signed char& c) const
{ {
short s; short s;
stream >> s; if(stream >> s)
c = static_cast<signed char>(s); c = static_cast<signed char>(s);
else
stream.clear(std::ios::badbit);
} }
void read_ascii(std::istream& stream, unsigned char& c) const void read_ascii(std::istream& stream, unsigned char& c) const
{ {
unsigned short s; unsigned short s;
stream >> s; if(stream >> s)
c = static_cast<unsigned char>(s); c = static_cast<unsigned char>(s);
else
stream.clear(std::ios::badbit);
} }
void read_ascii(std::istream& stream, float& t) const void read_ascii(std::istream& stream, float& t) const
{ {
stream >> iformat(t); if(!(stream >> iformat(t)))
stream.clear(std::ios::badbit);
} }
void read_ascii(std::istream& stream, double& t) const void read_ascii(std::istream& stream, double& t) const
{ {
stream >> iformat(t); if(!(stream >> iformat(t)))
stream.clear(std::ios::badbit);
} }
// Default template when Type is not a char type // Default template when Type is not a char type
template <typename Type> template <typename Type>
void read_ascii(std::istream& stream, Type& t) const void read_ascii(std::istream& stream, Type& t) const
{ {
stream >> t; if(!(stream >> t))
stream.clear(std::ios::badbit);
} }