From ca0bfcfa806ff91883d2e8978b122ee34716d62e Mon Sep 17 00:00:00 2001 From: AllyTally Date: Sat, 8 Feb 2020 00:10:45 -0400 Subject: [PATCH] Fix undefined behavior while loading scripts In `editor.cpp`, there's a few sections of code that try and index stuff using `string.length()-1`. This causes issues where if the string is empty, the result is -1, causing undefined behavior. Flibit fixed a few of these cases, like on line `375` of editor.cpp: `if((int) tstring.length() - 1 >= 0) // FIXME: This is sketchy. -flibit` It turns out that one of these weren't caught, over at line `471`. `tstring=tstring[tstring.length()-1];` This causes builds compiled on Windows to segfault if you load more than one level in the editor. I added a quick `if` around it, setting `tstring` to an empty string, which seems to fix the problem. --- desktop_version/src/editor.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/desktop_version/src/editor.cpp b/desktop_version/src/editor.cpp index 20d587df..455011ab 100644 --- a/desktop_version/src/editor.cpp +++ b/desktop_version/src/editor.cpp @@ -468,7 +468,11 @@ void editorclass::removehookfromscript(std::string t) { //If this line is not the start of a new hook, remove it! tstring=script.customscript[i]; - tstring=tstring[tstring.length()-1]; + if (tstring.length() > 0) { + tstring=tstring[tstring.length()-1]; + } else { + tstring=""; + } if(tstring==":") { //this is a hook