setupapi: Fix parsing of inf files containing garbage at the beginning of the file.

Sebastian Lackner sebastian at fds-team.de
Tue Mar 15 23:34:14 CDT 2016


Signed-off-by: Sebastian Lackner <sebastian at fds-team.de>
---

I guess its a bug in the Windows implementation, but unfortunately some applications
ship inf files affected by this issue: As soon as a [Strings] section is present (yes,
other sections do not matter), garbage at the beginning of the file is silently ignored.
It does not seem to matter where the [Strings] section is.

 dlls/setupapi/parser.c       |   14 +++++++++++++-
 dlls/setupapi/tests/parser.c |   12 ++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/dlls/setupapi/parser.c b/dlls/setupapi/parser.c
index e8c8fd3..5cc3ef5 100644
--- a/dlls/setupapi/parser.c
+++ b/dlls/setupapi/parser.c
@@ -114,6 +114,7 @@ struct parser
     int               cur_section;  /* index of section being parsed*/
     struct line      *line;         /* current line */
     unsigned int      line_pos;     /* current line position in file */
+    unsigned int      broken_line;  /* first line containing invalid data (if any) */
     unsigned int      error;        /* error code */
     unsigned int      token_len;    /* current token len */
     WCHAR token[MAX_FIELD_LEN+1];   /* current token */
@@ -600,12 +601,15 @@ static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos )
             set_state( parser, SECTION_NAME );
             return p + 1;
         default:
-            if (!isspaceW(*p))
+            if (isspaceW(*p)) break;
+            if (parser->cur_section != -1)
             {
                 parser->start = p;
                 set_state( parser, KEY_NAME );
                 return p;
             }
+            if (!parser->broken_line)
+                parser->broken_line = parser->line_pos;
             break;
         }
     }
@@ -886,6 +890,7 @@ static DWORD parse_buffer( struct inf_file *file, const WCHAR *buffer, const WCH
     parser.stack_pos   = 0;
     parser.cur_section = -1;
     parser.line_pos    = 1;
+    parser.broken_line = 0;
     parser.error       = 0;
     parser.token_len   = 0;
 
@@ -916,6 +921,13 @@ static DWORD parse_buffer( struct inf_file *file, const WCHAR *buffer, const WCH
 
     /* find the [strings] section */
     file->strings_section = find_section( file, Strings );
+
+    if (file->strings_section == -1 && parser.broken_line)
+    {
+        if (error_line) *error_line = parser.broken_line;
+        return ERROR_EXPECTED_SECTION_NAME;
+    }
+
     return 0;
 }
 
diff --git a/dlls/setupapi/tests/parser.c b/dlls/setupapi/tests/parser.c
index f929f01..67c9887 100644
--- a/dlls/setupapi/tests/parser.c
+++ b/dlls/setupapi/tests/parser.c
@@ -133,6 +133,18 @@ static const struct
     { STD_HEADER " [Test\x00Section]\n",                     ERROR_BAD_SECTION_NAME_LINE, 3,    FALSE },
     { STD_HEADER " [TestSection\x00]\n",                     ERROR_BAD_SECTION_NAME_LINE, 3,    FALSE },
     { STD_HEADER " [Test\x00Section]\n",                     ERROR_BAD_SECTION_NAME_LINE, 3,    FALSE },
+    { "garbage1\ngarbage2\n[abc]\n" STD_HEADER,              ERROR_EXPECTED_SECTION_NAME, 1,    FALSE },
+    { "garbage1\ngarbage2\n[Strings]\n" STD_HEADER,          0,                           0,    FALSE },
+    { ";comment\ngarbage1\ngarbage2\n[abc]\n" STD_HEADER,    ERROR_EXPECTED_SECTION_NAME, 2,    FALSE },
+    { ";comment\ngarbage1\ngarbage2\n[Strings]\n" STD_HEADER, 0,                          0,    FALSE },
+    { " \t\ngarbage1\ngarbage2\n[abc]\n" STD_HEADER,         ERROR_EXPECTED_SECTION_NAME, 2,    FALSE },
+    { " \t\ngarbage1\ngarbage2\n[Strings]\n" STD_HEADER,     0,                           0,    FALSE },
+    { "garbage1\ngarbage2\n" STD_HEADER "[abc]\n",           ERROR_EXPECTED_SECTION_NAME, 1,    FALSE },
+    { "garbage1\ngarbage2\n" STD_HEADER "[Strings]\n",       0,                           0,    FALSE },
+    { ";comment\ngarbage1\ngarbage2\n" STD_HEADER "[abc]\n", ERROR_EXPECTED_SECTION_NAME, 2,    FALSE },
+    { ";comment\ngarbage1\ngarbage2\n" STD_HEADER "[Strings]\n", 0,                       0,    FALSE },
+    { " \t\ngarbage1\ngarbage2\n" STD_HEADER "[abc]\n",      ERROR_EXPECTED_SECTION_NAME, 2,    FALSE },
+    { " \t\ngarbage1\ngarbage2\n" STD_HEADER "[Strings]\n",  0,                           0,    FALSE },
 };
 
 static void test_invalid_files(void)
-- 
2.7.1



More information about the wine-patches mailing list