aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2016-11-16 00:52:20 +0100
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2016-11-16 16:34:35 +0100
commitc0fe49457d37e4c51cd4fd829895a60ae24bc8af (patch)
treefe319e605a89d91453581dca9f77b51d1d27b6a1
parent1f9cf5cb9419d958beb6e6d3cb4fe520e1ef3a1c (diff)
downloadsciteco-c0fe49457d37e4c51cd4fd829895a60ae24bc8af.tar.gz
fixed segfault when munging empty scripts
* empty scripts are SciTECO scripts with an Hash-Bang line but no EOL characters. They are simply ignored now. * A test case cannot be added since 1) it's hard to create the test script with AT_DATA - we'd have to add it to the repo instead. 2) If the bug is not occurring, SciTECO starts into interactive mode which cannot be inhibited unless the script is __not__ empty. * Skipping the Hash-Bang line is optimized now, saving one iteration of the macro just to find out it contains no CR (which is the most common case).
-rw-r--r--src/parser.cpp13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/parser.cpp b/src/parser.cpp
index 1936837..97822e7 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -297,11 +297,17 @@ Execute::file(const gchar *filename, bool locals)
if (!g_file_get_contents(filename, &macro_str, NULL, &gerror))
throw GlibError(gerror);
+
/* only when executing files, ignore Hash-Bang line */
- if (*macro_str == '#')
- p = MAX(strchr(macro_str, '\r'), strchr(macro_str, '\n'))+1;
- else
+ if (*macro_str == '#') {
+ p = strpbrk(macro_str, "\r\n");
+ if (G_UNLIKELY(!p))
+ /* empty script */
+ goto cleanup;
+ p++;
+ } else {
p = macro_str;
+ }
try {
macro(p, locals);
@@ -318,6 +324,7 @@ Execute::file(const gchar *filename, bool locals)
throw; /* forward */
}
+cleanup:
g_free(macro_str);
}