From 6c266075de0bcf53d3bb7fb3c7582acf9c684da0 Mon Sep 17 00:00:00 2001 From: lethosor Date: Tue, 15 Jan 2019 21:06:49 -0500 Subject: [PATCH] Console-posix: fix crash with prompts longer than screen width Also add an extra fallback check around substr Fixes #1425 --- library/Console-posix.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/library/Console-posix.cpp b/library/Console-posix.cpp index da786aaac..696672861 100644 --- a/library/Console-posix.cpp +++ b/library/Console-posix.cpp @@ -480,7 +480,7 @@ namespace DFHack { char seq[64]; int cols = get_columns(); - int plen = prompt.size(); + int plen = prompt.size() % cols; int len = raw_buffer.size(); int begin = 0; int cooked_cursor = raw_cursor; @@ -493,7 +493,15 @@ namespace DFHack } if (plen+len > cols) len -= plen+len - cols; - std::string mbstr = toLocaleMB(raw_buffer.substr(begin,len)); + std::string mbstr; + try { + mbstr = toLocaleMB(raw_buffer.substr(begin,len)); + } + catch (std::out_of_range&) { + // fallback check in case begin is still out of range + // (this behaves badly but at least doesn't crash) + mbstr = toLocaleMB(raw_buffer); + } /* Cursor to left edge */ snprintf(seq,64,"\x1b[1G"); if (::write(STDIN_FILENO,seq,strlen(seq)) == -1) return;