- Make the parser more strict, rejecting broken sources
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Mon, 6 Nov 2006 11:21:15 +0000 (11:21 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Mon, 6 Nov 2006 11:21:15 +0000 (11:21 +0000)
  but showing the error line.
  Last fix making the parser relax would break multitag
  descriptions with empty lines, now we check dependencies
  at a higher level. (reference #160607)

zypp/parser/tagfile/TagFileParser.cc
zypp/parser/tagfile/TagFileParser.h
zypp/source/susetags/PackagesParser.cc
zypp/source/susetags/PatternTagFileParser.cc
zypp/source/susetags/PatternTagFileParser.h

index 4cb767d..125e081 100644 (file)
@@ -89,7 +89,7 @@ namespace zypp
         int new_progress = 0;
         _file_r = file_r;
         _file_size = 0;
-        _no_lines = 0;
+        _line_number = 0;
         _file_size = PathInfo(file_r).size();
         std::ifstream file(file_r.asString().c_str());
         int readed = 0;
@@ -111,6 +111,7 @@ namespace zypp
         while(file && !file.eof())
         {
           getline(file, buffer);
+          _line_number++;
           readed +=  buffer.size();
           
           boost::smatch what;
@@ -135,6 +136,7 @@ namespace zypp
             std::string element;
             boost::smatch element_what;
             getline(file, element);
+            _line_number++;
             readed +=  element.size();
             // while we dont find the list terminator
             while(!file.eof())
@@ -166,14 +168,11 @@ namespace zypp
                 }
               }
               
-              // skip empty lines and comments inside lists
-              if ( ! ( element.empty() || ( element[0] == '#' ) ) )
-              {
-                tag.values.push_back(element);
-              }
+              tag.values.push_back(element);
               
               XXX << element << std::endl;
               getline(file, element);
+              _line_number++;
               readed +=  element.size();
               //dumpRegexpResults(element_what);
             }
@@ -203,9 +202,9 @@ namespace zypp
             // before we used to throw a parse error exception if we dont find
             // a key value line. But package descriptions usually are broken
             // and contain multiple lines for single line tags, etc.
-            // so now we just skip those lines.
-            //ZYPP_THROW(ParseException("parse error: " + buffer));
-            ERR << "Parse error, unrecognized format [" << buffer << "]. Be sure " << _file_r << "does not contains a single tag with new lines." << std::endl;
+            stringstream ss;
+            ss << "Parse error, unrecognized line [" << buffer << "]. Be sure " << _file_r << " line " << _line_number << " misses a tag or comment.";
+            ZYPP_THROW( ParseException( ss.str() ) );
           }
           
           new_progress = (int)((((float)readed)/((float)_file_size))*100);
index 6b62544..be41219 100644 (file)
@@ -87,7 +87,7 @@ namespace zypp
           ParserProgress::Ptr _progress;
           Pathname _file_r;
           int _file_size;
-          int _no_lines;
+          int _line_number;
       };
       /////////////////////////////////////////////////////////////////
     } // namespace parser
index a552fc1..a116a86 100644 (file)
@@ -9,6 +9,7 @@
 /** \file      zypp/source/susetags/PackagesParser.cc
  *
 */
+#include <sstream>
 #include <iostream>
 
 #include <boost/regex.hpp>
@@ -25,6 +26,7 @@
 #include "zypp/ZYppFactory.h"
 
 using std::endl;
+using namespace std;
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
@@ -177,13 +179,22 @@ struct PackagesParser : public parser::tagfile::TagFileParser
     for ( std::list<std::string>::const_iterator it = depstr_r.begin();
           it != depstr_r.end(); ++it )
     {
+      if ( (*it).empty() )
+      {
+        stringstream ss;
+        ss << "Bad source [" << _source.alias() << "] at URL:[" << _source.url().asString() << "]. Emtpy capability on " << _file_r << " line " << _line_number;
+        ZYPP_THROW( ParseException( ss.str() ) );
+      }
+      
       try
       {
         capset.insert( CapFactory().parse( ResTraits<Package>::kind, *it ) );
       }
       catch (Exception & excpt_r)
       {
-        ZYPP_THROW( ParseException("Bad source ["+ _source.alias() +"] at URL:[" + _source.url().asString() + "]. Can't parse capability: [" + *it + "]" ) );
+        stringstream ss;
+        ss << "Bad source [" << _source.alias() << "] at URL:[" << _source.url().asString() << "]. Can't parse capability: [" << *it << "] on " << _file_r << " line " << _line_number;
+        ZYPP_THROW( ParseException( ss.str() ) );
       }
     }
   }
index f404dc5..c25de29 100644 (file)
@@ -22,6 +22,7 @@
 #include "zypp/CapFactory.h"
 
 #include "zypp/source/susetags/PatternTagFileParser.h"
+#include "zypp/parser/tagfile/ParseException.h"
 #include <boost/regex.hpp>
 
 #undef ZYPP_BASE_LOGGER_LOGGROUP
@@ -29,6 +30,7 @@
 
 using namespace std;
 using namespace boost;
+using namespace zypp::parser::tagfile;
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
@@ -194,13 +196,29 @@ void PatternTagFileParser::consume( const MultiTag &tag )
   }
 }
 
-static void parseDeps( const std::list<std::string> & strdeps, CapSet & capset, const Resolvable::Kind & kind = ResTraits<Pattern>::kind )
+void PatternTagFileParser::parseDeps( const std::list<std::string> & strdeps, CapSet & capset, const Resolvable::Kind & kind )
 {
   CapFactory f;
   for (std::list<std::string>::const_iterator it = strdeps.begin(); it != strdeps.end(); it++)
-  {
-    Capability cap = f.parse( kind, *it );
-    capset.insert( cap );
+  {  
+    if ( (*it).empty() )
+    {
+      stringstream ss;
+      ss << "Emtpy capability on " << _file_r << " line " << _line_number;
+      ZYPP_THROW( ParseException( ss.str() ) );
+    }
+    
+    try
+    {
+      Capability cap = f.parse( kind, *it );
+      capset.insert( cap );
+    }
+    catch ( const Exception &e )
+    {
+      stringstream ss;
+      ss << "Broken capability [" << *it << "]" << _file_r << " line " << _line_number;
+      ZYPP_THROW( ParseException( ss.str() ) );
+    }
   }
   return;
 }
index aedaba3..770efe7 100644 (file)
@@ -48,6 +48,8 @@ namespace zypp
         virtual ~PatternTagFileParser()
         {}
 
+        void parseDeps( const std::list<std::string> & strdeps, CapSet & capset, const Resolvable::Kind & kind = ResTraits<Pattern>::kind );
+        
         void consume( const SingleTag &tag );
         void consume( const MultiTag &tag );
         void endParse();