Index: src/OFXMLParser.h ================================================================== --- src/OFXMLParser.h +++ src/OFXMLParser.h @@ -165,10 +165,11 @@ of_xml_parser_string_block_t CDATAHandler; of_xml_parser_string_block_t commentHandler; of_xml_parser_unknown_entity_block_t unknownEntityHandler; #endif size_t level; + BOOL finishedParsing; } #ifdef OF_HAVE_PROPERTIES @property (retain) id delegate; # ifdef OF_HAVE_BLOCKS @@ -314,9 +315,14 @@ * Parses the specified file. * * \param path The path to the file to parse */ - (void)parseFile: (OFString*)path; + +/** + * \return Whether the XML parser has finished parsing + */ +- (BOOL)finishedParsing; @end @interface OFObject (OFXMLParserDelegate) @end Index: src/OFXMLParser.m ================================================================== --- src/OFXMLParser.m +++ src/OFXMLParser.m @@ -192,10 +192,20 @@ - (void)parseBuffer: (const char*)buf withSize: (size_t)size { size_t i, last = 0; + + if (finishedParsing) { + for (i = 0; i < size; i++) + if (buf[i] != ' ' && buf[i] != '\t' && + buf[i] != '\n' && buf[i] != '\r') + @throw [OFMalformedXMLException + newWithClass: isa]; + + return; + } for (i = 0; i < size; i++) lookup_table[state](self, selectors[state], buf, &i, &last); /* In OF_XMLPARSER_IN_TAG, there can be only spaces */ @@ -497,10 +507,13 @@ *last = *i + 1; state = (buf[*i] == '>' ? OF_XMLPARSER_OUTSIDE_TAG : OF_XMLPARSER_EXPECT_SPACE_OR_CLOSE); + + if ([previous count] == 0) + finishedParsing = YES; } /* Inside a tag, name found */ - (void)_parseInTagWithBuffer: (const char*)buf i: (size_t*)i @@ -877,10 +890,15 @@ level--; } *last = *i + 1; } + +- (BOOL)finishedParsing +{ + return finishedParsing; +} - (OFString*)string: (OFString*)string containsUnknownEntityNamed: (OFString*)entity { #if defined(OF_HAVE_PROPERTIES) && defined(OF_HAVE_BLOCKS) Index: tests/OFXMLParserTests.m ================================================================== --- tests/OFXMLParserTests.m +++ tests/OFXMLParserTests.m @@ -9,10 +9,11 @@ * the packaging of this file. */ #include "config.h" +#include #include #import "OFXMLParser.h" #import "OFString.h" #import "OFArray.h" @@ -339,10 +340,13 @@ /* Simulate a stream where we only get chunks */ len = strlen(str); for (j = 0; j < len; j+= 2) { + if ([parser finishedParsing]) + abort(); + if (j + 2 > len) [parser parseBuffer: str + j withSize: 1]; else [parser parseBuffer: str + j @@ -349,8 +353,16 @@ withSize: 2]; } TEST(@"Checking if everything was parsed", i == 32) + TEST(@"-[finishedParsing]", [parser finishedParsing]) + + TEST(@"Parsing whitespaces after the document", + R([parser parseString: @" \t\r\n "])) + + EXPECT_EXCEPTION(@"Detection of junk after the document", + OFMalformedXMLException, [parser parseString: @"a"]) + [pool drain]; } @end