From c0fe49457d37e4c51cd4fd829895a60ae24bc8af Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Wed, 16 Nov 2016 00:52:20 +0100 Subject: 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). --- src/parser.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src/parser.cpp') 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, ¯o_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); } -- cgit v1.2.3