changeset 261:39a03560163d

Improve STIL parsing reliability.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 05 Jan 2020 23:01:29 +0200
parents 4a3e8960b3a9
children ef1d6d16718e
files sidlib.c
diffstat 1 files changed, 10 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/sidlib.c	Sun Jan 05 22:11:48 2020 +0200
+++ b/sidlib.c	Sun Jan 05 23:01:29 2020 +0200
@@ -802,18 +802,6 @@
             // Get next character
             switch (ctx.ch = thfgetc(fh))
             {
-            case EOF:
-                if (ctx.parseMode != PM_IDLE)
-                {
-                    ret = th_io_error(fh, THERR_OUT_OF_DATA,
-                        "Unexpected end of file on line #%d",
-                        fh->line);
-                    ctx.parseMode = PM_ERROR;
-                }
-                else
-                    ctx.parseMode = PM_EOF;
-                break;
-
             case '\n':
                 fh->line++;
                 ctx.linePos = -1;
@@ -845,6 +833,11 @@
         case PM_NEXT:
         case PM_IDLE:
             // Normal parsing mode
+            if (ctx.ch == EOF)
+            {
+                sidlib_stildb_set_parsemode(&ctx, PM_EOF);
+            }
+            else
             if (ctx.ch == '#')
             {
                 sidlib_stildb_set_parsemode(&ctx, PM_COMMENT);
@@ -893,7 +886,7 @@
             break;
 
         case PM_ENTRY:
-            if (th_iscrlf(ctx.ch) || th_isspace(ctx.ch))
+            if (ctx.ch == EOF || th_iscrlf(ctx.ch) || th_isspace(ctx.ch))
             {
                 // A new file / entry, allocate and append
                 tmpStr[strPos] = 0;
@@ -923,7 +916,7 @@
             break;
 
         case PM_SUBTUNE:
-            if (ctx.ch == ')')
+            if (ctx.ch == EOF || ctx.ch == ')')
             {
                 BOOL neg = FALSE;
 
@@ -979,7 +972,7 @@
             break;
 
         case PM_FIELD_NAME:
-            if (ctx.ch == ':' || th_iscrlf(ctx.ch) || th_isspace(ctx.ch))
+            if (ctx.ch == EOF || ctx.ch == ':' || th_iscrlf(ctx.ch) || th_isspace(ctx.ch))
             {
                 tmpStr[strPos] = 0;
                 th_pstr_cpy(&fieldName, tmpStr);
@@ -1025,7 +1018,7 @@
                 ctx.ch = -1;
             }
             else
-            if (ctx.lineStart && ctx.linePos < 8)
+            if (ctx.ch == EOF || (ctx.lineStart && ctx.linePos < 8))
             {
                 // Field done
                 tmpStr[strPos] = 0;
@@ -1069,6 +1062,7 @@
     }
 
 out:
+
     th_free(tmpStr);
     th_free(fieldName);