From 5ed8ac8a816feaa3df4b6938dff2ff9f3ccd76e6 Mon Sep 17 00:00:00 2001 From: rubidium Date: Sat, 11 Apr 2015 19:33:36 +0000 Subject: (svn r27233) -Fix [FS#6272]: crash when no AIs were installed due to improper handling of non-ASCII characters by the string pointer lexer --- src/3rdparty/squirrel/squirrel/sqapi.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/3rdparty/squirrel/squirrel/sqapi.cpp b/src/3rdparty/squirrel/squirrel/sqapi.cpp index 8374f7f31..f02fee29f 100644 --- a/src/3rdparty/squirrel/squirrel/sqapi.cpp +++ b/src/3rdparty/squirrel/squirrel/sqapi.cpp @@ -1261,10 +1261,28 @@ struct BufState{ WChar buf_lexfeed(SQUserPointer file) { - BufState *buf=(BufState*)file; - if(buf->size<(buf->ptr+1)) - return 0; - return buf->buf[buf->ptr++]; + /* Convert an UTF-8 character into a WChar */ + BufState *buf = (BufState *)file; + const char *p = &buf->buf[buf->ptr]; + + if (buf->size < buf->ptr + 1) return 0; + + /* Read the first character, and get the length based on UTF-8 specs. If invalid, bail out. */ + uint len = Utf8EncodedCharLen(*p); + if (len == 0) { + buf->ptr++; + return -1; + } + + /* Read the remaining bits. */ + if (buf->size < buf->ptr + len) return 0; + buf->ptr += len; + + /* Convert the character, and when definitely invalid, bail out as well. */ + WChar c; + if (Utf8Decode(&c, p) != len) return -1; + + return c; } SQRESULT sq_compilebuffer(HSQUIRRELVM v,const SQChar *s,SQInteger size,const SQChar *sourcename,SQBool raiseerror) { -- cgit v1.2.3-54-g00ecf