Skip to content

Commit 79859eb

Browse files
committed
Merge pull request opencv#15145 from alalek:fix_15127
2 parents b10ec8e + 5691d99 commit 79859eb

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

modules/core/src/persistence_json.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ class JSONParser : public FileStorageParser
296296

297297
while ( is_eof == false && is_completed == false )
298298
{
299+
if (!ptr)
300+
CV_PARSE_ERROR_CPP("Invalid input");
299301
switch ( *ptr )
300302
{
301303
/* comment */
@@ -381,6 +383,7 @@ class JSONParser : public FileStorageParser
381383
if ( is_eof || !is_completed )
382384
{
383385
ptr = fs->bufferStart();
386+
CV_Assert(ptr);
384387
*ptr = '\0';
385388
fs->setEof();
386389
if( !is_completed )
@@ -392,6 +395,9 @@ class JSONParser : public FileStorageParser
392395

393396
char* parseKey( char* ptr, FileNode& collection, FileNode& value_placeholder )
394397
{
398+
if (!ptr)
399+
CV_PARSE_ERROR_CPP("Invalid input");
400+
395401
if( *ptr != '"' )
396402
CV_PARSE_ERROR_CPP( "Key must start with \'\"\'" );
397403

@@ -430,6 +436,9 @@ class JSONParser : public FileStorageParser
430436

431437
char* parseValue( char* ptr, FileNode& node )
432438
{
439+
if (!ptr)
440+
CV_PARSE_ERROR_CPP("Invalid value input");
441+
433442
ptr = skipSpaces( ptr );
434443
if( !ptr || !*ptr )
435444
CV_PARSE_ERROR_CPP( "Unexpected End-Of-File" );
@@ -817,6 +826,9 @@ class JSONParser : public FileStorageParser
817826

818827
bool parse( char* ptr )
819828
{
829+
if (!ptr)
830+
CV_PARSE_ERROR_CPP("Invalid input");
831+
820832
ptr = skipSpaces( ptr );
821833
if ( !ptr || !*ptr )
822834
return false;

modules/core/src/persistence_xml.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ class XMLParser : public FileStorageParser
360360

361361
char* skipSpaces( char* ptr, int mode )
362362
{
363+
if (!ptr)
364+
CV_PARSE_ERROR_CPP("Invalid input");
365+
363366
int level = 0;
364367

365368
for(;;)
@@ -441,6 +444,9 @@ class XMLParser : public FileStorageParser
441444

442445
char* parseValue( char* ptr, FileNode& node )
443446
{
447+
if (!ptr)
448+
CV_PARSE_ERROR_CPP("Invalid input");
449+
444450
FileNode new_elem;
445451
bool have_space = true;
446452
int value_type = node.type();
@@ -456,6 +462,8 @@ class XMLParser : public FileStorageParser
456462
(c == '<' && ptr[1] == '!' && ptr[2] == '-') )
457463
{
458464
ptr = skipSpaces( ptr, 0 );
465+
if (!ptr)
466+
CV_PARSE_ERROR_CPP("Invalid input");
459467
have_space = true;
460468
c = *ptr;
461469
}
@@ -502,6 +510,8 @@ class XMLParser : public FileStorageParser
502510
{
503511
ptr = fs->parseBase64( ptr, 0, new_elem);
504512
ptr = skipSpaces( ptr, 0 );
513+
if (!ptr)
514+
CV_PARSE_ERROR_CPP("Invalid input");
505515
}
506516

507517
ptr = parseTag( ptr, key2, type_name, tag_type );
@@ -645,6 +655,9 @@ class XMLParser : public FileStorageParser
645655
char* parseTag( char* ptr, std::string& tag_name,
646656
std::string& type_name, int& tag_type )
647657
{
658+
if (!ptr)
659+
CV_PARSE_ERROR_CPP("Invalid tag input");
660+
648661
if( *ptr == '\0' )
649662
CV_PARSE_ERROR_CPP( "Unexpected end of the stream" );
650663

@@ -702,6 +715,8 @@ class XMLParser : public FileStorageParser
702715
if( *ptr != '=' )
703716
{
704717
ptr = skipSpaces( ptr, CV_XML_INSIDE_TAG );
718+
if (!ptr)
719+
CV_PARSE_ERROR_CPP("Invalid attribute");
705720
if( *ptr != '=' )
706721
CV_PARSE_ERROR_CPP( "Attribute name should be followed by \'=\'" );
707722
}
@@ -740,6 +755,8 @@ class XMLParser : public FileStorageParser
740755
if( c != '>' )
741756
{
742757
ptr = skipSpaces( ptr, CV_XML_INSIDE_TAG );
758+
if (!ptr)
759+
CV_PARSE_ERROR_CPP("Invalid input");
743760
c = *ptr;
744761
}
745762

@@ -781,6 +798,8 @@ class XMLParser : public FileStorageParser
781798

782799
// CV_XML_INSIDE_TAG is used to prohibit leading comments
783800
ptr = skipSpaces( ptr, CV_XML_INSIDE_TAG );
801+
if (!ptr)
802+
CV_PARSE_ERROR_CPP("Invalid input");
784803

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

795816
if( *ptr != '\0' )
796817
{

modules/core/src/persistence_yml.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ class YAMLParser : public FileStorageParser
330330

331331
char* skipSpaces( char* ptr, int min_indent, int max_comment_indent )
332332
{
333+
if (!ptr)
334+
CV_PARSE_ERROR_CPP("Invalid input");
335+
333336
for(;;)
334337
{
335338
while( *ptr == ' ' )
@@ -374,6 +377,9 @@ class YAMLParser : public FileStorageParser
374377

375378
bool getBase64Row(char* ptr, int indent, char* &beg, char* &end)
376379
{
380+
if (!ptr)
381+
CV_PARSE_ERROR_CPP("Invalid input");
382+
377383
beg = end = ptr = skipSpaces(ptr, 0, INT_MAX);
378384
if (!ptr || !*ptr)
379385
return false; // end of file
@@ -394,6 +400,9 @@ class YAMLParser : public FileStorageParser
394400

395401
char* parseKey( char* ptr, FileNode& map_node, FileNode& value_placeholder )
396402
{
403+
if (!ptr)
404+
CV_PARSE_ERROR_CPP("Invalid input");
405+
397406
char c;
398407
char *endptr = ptr - 1, *saveptr;
399408

@@ -422,6 +431,9 @@ class YAMLParser : public FileStorageParser
422431

423432
char* parseValue( char* ptr, FileNode& node, int min_indent, bool is_parent_flow )
424433
{
434+
if (!ptr)
435+
CV_PARSE_ERROR_CPP("Invalid input");
436+
425437
char* endptr = 0;
426438
char c = ptr[0], d = ptr[1];
427439
int value_type = FileNode::NONE;
@@ -508,6 +520,8 @@ class YAMLParser : public FileStorageParser
508520

509521
*endptr = d;
510522
ptr = skipSpaces( endptr, min_indent, INT_MAX );
523+
if (!ptr)
524+
CV_PARSE_ERROR_CPP("Invalid input");
511525

512526
c = *ptr;
513527

@@ -634,6 +648,8 @@ class YAMLParser : public FileStorageParser
634648
FileNode elem;
635649

636650
ptr = skipSpaces( ptr, new_min_indent, INT_MAX );
651+
if (!ptr)
652+
CV_PARSE_ERROR_CPP("Invalid input");
637653
if( *ptr == '}' || *ptr == ']' )
638654
{
639655
if( *ptr != d )
@@ -647,6 +663,8 @@ class YAMLParser : public FileStorageParser
647663
if( *ptr != ',' )
648664
CV_PARSE_ERROR_CPP( "Missing , between the elements" );
649665
ptr = skipSpaces( ptr + 1, new_min_indent, INT_MAX );
666+
if (!ptr)
667+
CV_PARSE_ERROR_CPP("Invalid input");
650668
}
651669

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

747765
bool parse( char* ptr )
748766
{
767+
if (!ptr)
768+
CV_PARSE_ERROR_CPP("Invalid input");
769+
749770
bool first = true;
750771
bool ok = true;
751772
FileNode root_collection(fs->getFS(), 0, 0);

0 commit comments

Comments
 (0)