Commit 6c7744e2 authored by Mark Roszko's avatar Mark Roszko Committed by Wayne Stambaugh

VRML file parser fixes.

* Fix coverity scan error for array comparison.
* Check for buffer length overflow that fixes segfault.
parent 04c55f09
...@@ -94,13 +94,13 @@ static int SkipGetChar( FILE* File ) ...@@ -94,13 +94,13 @@ static int SkipGetChar( FILE* File )
} }
char* GetNextTag( FILE* File, char* tag ) bool GetNextTag( FILE* File, char* tag, size_t len )
{ {
int c = SkipGetChar( File ); int c = SkipGetChar( File );
if( c == EOF ) if( c == EOF )
{ {
return NULL; return false;
} }
tag[0] = c; tag[0] = c;
...@@ -109,9 +109,10 @@ char* GetNextTag( FILE* File, char* tag ) ...@@ -109,9 +109,10 @@ char* GetNextTag( FILE* File, char* tag )
// DBG( printf( "tag[0] %c\n", tag[0] ) ); // DBG( printf( "tag[0] %c\n", tag[0] ) );
if( (c != '}') && (c != ']') ) if( (c != '}') && (c != ']') )
{ {
len--;
char* dst = &tag[1]; char* dst = &tag[1];
while( fscanf( File, "%c", dst ) ) while( fscanf( File, "%c", dst ) && len > 0 )
{ {
if( (*dst == ' ') || (*dst == '[') || (*dst == '{') if( (*dst == ' ') || (*dst == '[') || (*dst == '{')
|| (*dst == '\t') || (*dst == '\n')|| (*dst == '\r') ) || (*dst == '\t') || (*dst == '\n')|| (*dst == '\r') )
...@@ -121,6 +122,7 @@ char* GetNextTag( FILE* File, char* tag ) ...@@ -121,6 +122,7 @@ char* GetNextTag( FILE* File, char* tag )
} }
dst++; dst++;
len--;
} }
...@@ -134,7 +136,7 @@ char* GetNextTag( FILE* File, char* tag ) ...@@ -134,7 +136,7 @@ char* GetNextTag( FILE* File, char* tag )
} }
} }
return tag; return true;
} }
......
...@@ -52,6 +52,6 @@ int read_NotImplemented( FILE* File, char closeChar); ...@@ -52,6 +52,6 @@ int read_NotImplemented( FILE* File, char closeChar);
int parseVertexList( FILE* File, std::vector< glm::vec3 > &dst_vector); int parseVertexList( FILE* File, std::vector< glm::vec3 > &dst_vector);
int parseVertex( FILE* File, glm::vec3 &dst_vertex ); int parseVertex( FILE* File, glm::vec3 &dst_vertex );
int parseFloat( FILE* File, float *dst_float ); int parseFloat( FILE* File, float *dst_float );
char* GetNextTag( FILE* File, char* tag ); bool GetNextTag( FILE* File, char* tag, size_t len );
#endif #endif
...@@ -88,7 +88,7 @@ void VRML1_MODEL_PARSER::Load( const wxString& aFilename, double aVrmlunits_to_3 ...@@ -88,7 +88,7 @@ void VRML1_MODEL_PARSER::Load( const wxString& aFilename, double aVrmlunits_to_3
childs.clear(); childs.clear();
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( ( *text == '}' ) || ( *text == ']' ) ) if( ( *text == '}' ) || ( *text == ']' ) )
{ {
...@@ -123,7 +123,7 @@ int VRML1_MODEL_PARSER::read_separator() ...@@ -123,7 +123,7 @@ int VRML1_MODEL_PARSER::read_separator()
// DBG( printf( "Separator\n" ) ); // DBG( printf( "Separator\n" ) );
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( strcmp( text, "Material" ) == 0 ) if( strcmp( text, "Material" ) == 0 )
{ {
...@@ -182,7 +182,7 @@ int VRML1_MODEL_PARSER::readMaterial() ...@@ -182,7 +182,7 @@ int VRML1_MODEL_PARSER::readMaterial()
m_model->m_Materials = material; m_model->m_Materials = material;
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( *text == ']' ) if( *text == ']' )
{ {
...@@ -230,7 +230,7 @@ int VRML1_MODEL_PARSER::readCoordinate3() ...@@ -230,7 +230,7 @@ int VRML1_MODEL_PARSER::readCoordinate3()
// DBG( printf( " readCoordinate3\n" ) ); // DBG( printf( " readCoordinate3\n" ) );
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( *text == ']' ) if( *text == ']' )
{ {
...@@ -258,7 +258,7 @@ int VRML1_MODEL_PARSER::readIndexedFaceSet() ...@@ -258,7 +258,7 @@ int VRML1_MODEL_PARSER::readIndexedFaceSet()
// DBG( printf( " readIndexedFaceSet\n" ) ); // DBG( printf( " readIndexedFaceSet\n" ) );
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( *text == ']' ) if( *text == ']' )
{ {
......
...@@ -99,7 +99,7 @@ void VRML2_MODEL_PARSER::Load( const wxString& aFilename, double aVrmlunits_to_3 ...@@ -99,7 +99,7 @@ void VRML2_MODEL_PARSER::Load( const wxString& aFilename, double aVrmlunits_to_3
childs.clear(); childs.clear();
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( ( *text == '}' ) || ( *text == ']' ) ) if( ( *text == '}' ) || ( *text == ']' ) )
{ {
...@@ -138,7 +138,7 @@ int VRML2_MODEL_PARSER::read_Transform() ...@@ -138,7 +138,7 @@ int VRML2_MODEL_PARSER::read_Transform()
{ {
char text[128]; char text[128];
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( *text == ']' ) if( *text == ']' )
{ {
...@@ -257,15 +257,15 @@ int VRML2_MODEL_PARSER::read_DEF_Coordinate() ...@@ -257,15 +257,15 @@ int VRML2_MODEL_PARSER::read_DEF_Coordinate()
char text[128]; char text[128];
// Get the name of the definition. // Get the name of the definition.
GetNextTag( m_file, text ); GetNextTag( m_file, text, sizeof(text) );
std::string coordinateName = text; std::string coordinateName = text;
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( ( text == NULL ) || ( *text == ']' ) ) if( *text == ']' )
continue; continue;
if( ( *text == '}' ) ) if( *text == '}' )
return 0; return 0;
if( strcmp( text, "Coordinate" ) == 0 ) if( strcmp( text, "Coordinate" ) == 0 )
...@@ -287,9 +287,9 @@ int VRML2_MODEL_PARSER::read_DEF() ...@@ -287,9 +287,9 @@ int VRML2_MODEL_PARSER::read_DEF()
{ {
char text[128]; char text[128];
GetNextTag( m_file, text ); GetNextTag( m_file, text, sizeof(text) );
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( *text == ']' ) if( *text == ']' )
{ {
...@@ -344,7 +344,7 @@ int VRML2_MODEL_PARSER::read_USE() ...@@ -344,7 +344,7 @@ int VRML2_MODEL_PARSER::read_USE()
char text[128]; char text[128];
// Get the name of the definition. // Get the name of the definition.
GetNextTag( m_file, text ); GetNextTag( m_file, text, sizeof(text) );
std::string coordinateName = text; std::string coordinateName = text;
// Look for it in our coordinate map. // Look for it in our coordinate map.
...@@ -368,7 +368,7 @@ int VRML2_MODEL_PARSER::read_Shape() ...@@ -368,7 +368,7 @@ int VRML2_MODEL_PARSER::read_Shape()
{ {
char text[128]; char text[128];
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( *text == ']' ) if( *text == ']' )
{ {
...@@ -418,7 +418,7 @@ int VRML2_MODEL_PARSER::read_Appearance() ...@@ -418,7 +418,7 @@ int VRML2_MODEL_PARSER::read_Appearance()
{ {
char text[128]; char text[128];
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( *text == ']' ) if( *text == ']' )
{ {
...@@ -446,7 +446,7 @@ int VRML2_MODEL_PARSER::read_material() ...@@ -446,7 +446,7 @@ int VRML2_MODEL_PARSER::read_material()
S3D_MATERIAL* material = NULL; S3D_MATERIAL* material = NULL;
char text[128]; char text[128];
if( GetNextTag( m_file, text ) ) if( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( strcmp( text, "Material" ) == 0 ) if( strcmp( text, "Material" ) == 0 )
{ {
...@@ -462,7 +462,7 @@ int VRML2_MODEL_PARSER::read_material() ...@@ -462,7 +462,7 @@ int VRML2_MODEL_PARSER::read_material()
} }
else if( strcmp( text, "DEF" ) == 0 ) else if( strcmp( text, "DEF" ) == 0 )
{ {
if( GetNextTag( m_file, text ) ) if( GetNextTag( m_file, text, sizeof(text) ) )
{ {
wxString mat_name; wxString mat_name;
mat_name = FROM_UTF8( text ); mat_name = FROM_UTF8( text );
...@@ -471,7 +471,7 @@ int VRML2_MODEL_PARSER::read_material() ...@@ -471,7 +471,7 @@ int VRML2_MODEL_PARSER::read_material()
GetMaster()->Insert( material ); GetMaster()->Insert( material );
m_model->m_Materials = material; m_model->m_Materials = material;
if( GetNextTag( m_file, text ) ) if( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( strcmp( text, "Material" ) == 0 ) if( strcmp( text, "Material" ) == 0 )
{ {
...@@ -482,7 +482,7 @@ int VRML2_MODEL_PARSER::read_material() ...@@ -482,7 +482,7 @@ int VRML2_MODEL_PARSER::read_material()
} }
else if( strcmp( text, "USE" ) == 0 ) else if( strcmp( text, "USE" ) == 0 )
{ {
if( GetNextTag( m_file, text ) ) if( GetNextTag( m_file, text, sizeof(text) ) )
{ {
wxString mat_name; wxString mat_name;
mat_name = FROM_UTF8( text ); mat_name = FROM_UTF8( text );
...@@ -511,7 +511,7 @@ int VRML2_MODEL_PARSER::read_Material() ...@@ -511,7 +511,7 @@ int VRML2_MODEL_PARSER::read_Material()
char text[128]; char text[128];
glm::vec3 vertex; glm::vec3 vertex;
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( *text == ']' ) if( *text == ']' )
{ {
...@@ -593,7 +593,7 @@ int VRML2_MODEL_PARSER::read_IndexedFaceSet() ...@@ -593,7 +593,7 @@ int VRML2_MODEL_PARSER::read_IndexedFaceSet()
m_normalPerVertex = false; m_normalPerVertex = false;
colorPerVertex = false; colorPerVertex = false;
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( *text == ']' ) if( *text == ']' )
{ {
...@@ -607,7 +607,7 @@ int VRML2_MODEL_PARSER::read_IndexedFaceSet() ...@@ -607,7 +607,7 @@ int VRML2_MODEL_PARSER::read_IndexedFaceSet()
if( strcmp( text, "normalPerVertex" ) == 0 ) if( strcmp( text, "normalPerVertex" ) == 0 )
{ {
if( GetNextTag( m_file, text ) ) if( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( strcmp( text, "TRUE" ) == 0 ) if( strcmp( text, "TRUE" ) == 0 )
{ {
...@@ -617,7 +617,7 @@ int VRML2_MODEL_PARSER::read_IndexedFaceSet() ...@@ -617,7 +617,7 @@ int VRML2_MODEL_PARSER::read_IndexedFaceSet()
} }
else if( strcmp( text, "colorPerVertex" ) == 0 ) else if( strcmp( text, "colorPerVertex" ) == 0 )
{ {
GetNextTag( m_file, text ); GetNextTag( m_file, text, sizeof(text) );
if( strcmp( text, "TRUE" ) ) if( strcmp( text, "TRUE" ) )
{ {
...@@ -667,12 +667,12 @@ int VRML2_MODEL_PARSER::read_IndexedLineSet() ...@@ -667,12 +667,12 @@ int VRML2_MODEL_PARSER::read_IndexedLineSet()
{ {
char text[128]; char text[128];
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( ( text == NULL ) || ( *text == ']' ) ) if( *text == ']' )
continue; continue;
if( ( *text == '}' ) ) if( *text == '}' )
return 0; return 0;
if( strcmp( text, "Coordinate" ) == 0 ) if( strcmp( text, "Coordinate" ) == 0 )
...@@ -783,7 +783,7 @@ int VRML2_MODEL_PARSER::read_Color() ...@@ -783,7 +783,7 @@ int VRML2_MODEL_PARSER::read_Color()
{ {
char text[128]; char text[128];
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( *text == ']' ) if( *text == ']' )
{ {
...@@ -810,7 +810,7 @@ int VRML2_MODEL_PARSER::read_Normal() ...@@ -810,7 +810,7 @@ int VRML2_MODEL_PARSER::read_Normal()
{ {
char text[128]; char text[128];
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( *text == ']' ) if( *text == ']' )
{ {
...@@ -843,7 +843,7 @@ int VRML2_MODEL_PARSER::read_Coordinate() ...@@ -843,7 +843,7 @@ int VRML2_MODEL_PARSER::read_Coordinate()
{ {
char text[128]; char text[128];
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( *text == ']' ) if( *text == ']' )
{ {
...@@ -872,12 +872,12 @@ int VRML2_MODEL_PARSER::read_CoordinateDef() ...@@ -872,12 +872,12 @@ int VRML2_MODEL_PARSER::read_CoordinateDef()
{ {
char text[128]; char text[128];
while( GetNextTag( m_file, text ) ) while( GetNextTag( m_file, text, sizeof(text) ) )
{ {
if( ( text == NULL ) || ( *text == ']' ) ) if( *text == ']' )
continue; continue;
if( ( *text == '}' ) ) if( *text == '}' )
return 0; return 0;
if( strcmp( text, "point" ) == 0 ) if( strcmp( text, "point" ) == 0 )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment