diff --git a/src/stratagus/util.cpp b/src/stratagus/util.cpp
index 6659c144e..26c632d5d 100644
--- a/src/stratagus/util.cpp
+++ b/src/stratagus/util.cpp
@@ -433,7 +433,7 @@ int UTF8GetPrev(const std::string &text, int curpos)
 		return curpos;
 	}
 	while (curpos >= 0) {
-		if ((text[curpos] & 0xC0) != 0x80) {
+		if (curpos < text.size() && (text[curpos] & 0xC0) != 0x80) {
 			return curpos;
 		}
 		--curpos;