Console-posix: fix crash with prompts longer than screen width

Also add an extra fallback check around substr

Fixes #1425
develop
lethosor 2019-01-15 21:06:49 -05:00
parent 17d60c5a1f
commit 6c266075de
1 changed files with 10 additions and 2 deletions

@ -480,7 +480,7 @@ namespace DFHack
{ {
char seq[64]; char seq[64];
int cols = get_columns(); int cols = get_columns();
int plen = prompt.size(); int plen = prompt.size() % cols;
int len = raw_buffer.size(); int len = raw_buffer.size();
int begin = 0; int begin = 0;
int cooked_cursor = raw_cursor; int cooked_cursor = raw_cursor;
@ -493,7 +493,15 @@ namespace DFHack
} }
if (plen+len > cols) if (plen+len > cols)
len -= 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 */ /* Cursor to left edge */
snprintf(seq,64,"\x1b[1G"); snprintf(seq,64,"\x1b[1G");
if (::write(STDIN_FILENO,seq,strlen(seq)) == -1) return; if (::write(STDIN_FILENO,seq,strlen(seq)) == -1) return;