Skip to content

Commit

Permalink
core(persistence): added null ptr checks
Browse files Browse the repository at this point in the history
  • Loading branch information
alalek committed Jul 25, 2019
1 parent b10ec8e commit 5691d99
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
12 changes: 12 additions & 0 deletions modules/core/src/persistence_json.cpp
Expand Up @@ -296,6 +296,8 @@ class JSONParser : public FileStorageParser

while ( is_eof == false && is_completed == false )
{
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");
switch ( *ptr )
{
/* comment */
Expand Down Expand Up @@ -381,6 +383,7 @@ class JSONParser : public FileStorageParser
if ( is_eof || !is_completed )
{
ptr = fs->bufferStart();
CV_Assert(ptr);
*ptr = '\0';
fs->setEof();
if( !is_completed )
Expand All @@ -392,6 +395,9 @@ class JSONParser : public FileStorageParser

char* parseKey( char* ptr, FileNode& collection, FileNode& value_placeholder )
{
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");

if( *ptr != '"' )
CV_PARSE_ERROR_CPP( "Key must start with \'\"\'" );

Expand Down Expand Up @@ -430,6 +436,9 @@ class JSONParser : public FileStorageParser

char* parseValue( char* ptr, FileNode& node )
{
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid value input");

ptr = skipSpaces( ptr );
if( !ptr || !*ptr )
CV_PARSE_ERROR_CPP( "Unexpected End-Of-File" );
Expand Down Expand Up @@ -817,6 +826,9 @@ class JSONParser : public FileStorageParser

bool parse( char* ptr )
{
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");

ptr = skipSpaces( ptr );
if ( !ptr || !*ptr )
return false;
Expand Down
21 changes: 21 additions & 0 deletions modules/core/src/persistence_xml.cpp
Expand Up @@ -360,6 +360,9 @@ class XMLParser : public FileStorageParser

char* skipSpaces( char* ptr, int mode )
{
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");

int level = 0;

for(;;)
Expand Down Expand Up @@ -441,6 +444,9 @@ class XMLParser : public FileStorageParser

char* parseValue( char* ptr, FileNode& node )
{
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");

FileNode new_elem;
bool have_space = true;
int value_type = node.type();
Expand All @@ -456,6 +462,8 @@ class XMLParser : public FileStorageParser
(c == '<' && ptr[1] == '!' && ptr[2] == '-') )
{
ptr = skipSpaces( ptr, 0 );
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");
have_space = true;
c = *ptr;
}
Expand Down Expand Up @@ -502,6 +510,8 @@ class XMLParser : public FileStorageParser
{
ptr = fs->parseBase64( ptr, 0, new_elem);
ptr = skipSpaces( ptr, 0 );
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");
}

ptr = parseTag( ptr, key2, type_name, tag_type );
Expand Down Expand Up @@ -645,6 +655,9 @@ class XMLParser : public FileStorageParser
char* parseTag( char* ptr, std::string& tag_name,
std::string& type_name, int& tag_type )
{
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid tag input");

if( *ptr == '\0' )
CV_PARSE_ERROR_CPP( "Unexpected end of the stream" );

Expand Down Expand Up @@ -702,6 +715,8 @@ class XMLParser : public FileStorageParser
if( *ptr != '=' )
{
ptr = skipSpaces( ptr, CV_XML_INSIDE_TAG );
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid attribute");
if( *ptr != '=' )
CV_PARSE_ERROR_CPP( "Attribute name should be followed by \'=\'" );
}
Expand Down Expand Up @@ -740,6 +755,8 @@ class XMLParser : public FileStorageParser
if( c != '>' )
{
ptr = skipSpaces( ptr, CV_XML_INSIDE_TAG );
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");
c = *ptr;
}

Expand Down Expand Up @@ -781,6 +798,8 @@ class XMLParser : public FileStorageParser

// CV_XML_INSIDE_TAG is used to prohibit leading comments
ptr = skipSpaces( ptr, CV_XML_INSIDE_TAG );
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");

if( memcmp( ptr, "<?xml", 5 ) != 0 ) // FIXIT ptr[1..] - out of bounds read without check
CV_PARSE_ERROR_CPP( "Valid XML should start with \'<?xml ...?>\'" );
Expand All @@ -791,6 +810,8 @@ class XMLParser : public FileStorageParser
while( ptr && *ptr != '\0' )
{
ptr = skipSpaces( ptr, 0 );
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");

if( *ptr != '\0' )
{
Expand Down
21 changes: 21 additions & 0 deletions modules/core/src/persistence_yml.cpp
Expand Up @@ -330,6 +330,9 @@ class YAMLParser : public FileStorageParser

char* skipSpaces( char* ptr, int min_indent, int max_comment_indent )
{
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");

for(;;)
{
while( *ptr == ' ' )
Expand Down Expand Up @@ -374,6 +377,9 @@ class YAMLParser : public FileStorageParser

bool getBase64Row(char* ptr, int indent, char* &beg, char* &end)
{
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");

beg = end = ptr = skipSpaces(ptr, 0, INT_MAX);
if (!ptr || !*ptr)
return false; // end of file
Expand All @@ -394,6 +400,9 @@ class YAMLParser : public FileStorageParser

char* parseKey( char* ptr, FileNode& map_node, FileNode& value_placeholder )
{
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");

char c;
char *endptr = ptr - 1, *saveptr;

Expand Down Expand Up @@ -422,6 +431,9 @@ class YAMLParser : public FileStorageParser

char* parseValue( char* ptr, FileNode& node, int min_indent, bool is_parent_flow )
{
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");

char* endptr = 0;
char c = ptr[0], d = ptr[1];
int value_type = FileNode::NONE;
Expand Down Expand Up @@ -508,6 +520,8 @@ class YAMLParser : public FileStorageParser

*endptr = d;
ptr = skipSpaces( endptr, min_indent, INT_MAX );
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");

c = *ptr;

Expand Down Expand Up @@ -634,6 +648,8 @@ class YAMLParser : public FileStorageParser
FileNode elem;

ptr = skipSpaces( ptr, new_min_indent, INT_MAX );
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");
if( *ptr == '}' || *ptr == ']' )
{
if( *ptr != d )
Expand All @@ -647,6 +663,8 @@ class YAMLParser : public FileStorageParser
if( *ptr != ',' )
CV_PARSE_ERROR_CPP( "Missing , between the elements" );
ptr = skipSpaces( ptr + 1, new_min_indent, INT_MAX );
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");
}

if( struct_type == FileNode::MAP )
Expand Down Expand Up @@ -746,6 +764,9 @@ class YAMLParser : public FileStorageParser

bool parse( char* ptr )
{
if (!ptr)
CV_PARSE_ERROR_CPP("Invalid input");

bool first = true;
bool ok = true;
FileNode root_collection(fs->getFS(), 0, 0);
Expand Down

0 comments on commit 5691d99

Please sign in to comment.