Fix crash with CBot string functions out of range (closes #704)

dev-time-step
krzys-h 2016-01-23 21:07:19 +01:00
parent 048534e89d
commit 9ff978155c
2 changed files with 22 additions and 0 deletions

View File

@ -70,6 +70,9 @@ bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
// retrieves this number
int n = pVar->GetValInt();
if (n > static_cast<int>(s.length())) n = s.length();
if (n < 0) n = 0;
// no third parameter
if ( pVar->GetNext() != nullptr ) { ex = CBotErrOverParam ; return true; }
@ -103,6 +106,9 @@ bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
// retrieves this number
int n = pVar->GetValInt();
if (n > static_cast<int>(s.length())) n = s.length();
if (n < 0) n = 0;
// no third parameter
if ( pVar->GetNext() != nullptr ) { ex = CBotErrOverParam ; return true; }
@ -136,6 +142,9 @@ bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
// retrieves this number
int n = pVar->GetValInt();
if (n > static_cast<int>(s.length())) n = s.length();
if (n < 0) n = 0;
// third parameter optional
if ( pVar->GetNext() != nullptr )
{
@ -147,6 +156,9 @@ bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
// retrieves this number
int l = pVar->GetValInt();
if (l > static_cast<int>(s.length())) l = s.length();
if (l < 0) l = 0;
// but no fourth parameter
if ( pVar->GetNext() != nullptr ){ ex = CBotErrOverParam ; return true; }

View File

@ -969,6 +969,16 @@ TEST_F(CBotUT, StringFunctions)
" ASSERT(strfind(s, \"o\") == 1);\n"
" ASSERT(strval(\"2.5\") == 2.5);\n"
"}\n"
"extern void StringFunctionsOutOfRange()\n"
"{\n"
" ASSERT(strmid(\"asdf\", 5, 1) == \"\");\n"
" ASSERT(strmid(\"asdf\", 0, 100) == \"asdf\");\n"
" ASSERT(strmid(\"asdf\", -500, 100) == \"asdf\");\n"
" ASSERT(strleft(\"asdf\", 15) == \"asdf\");\n"
" ASSERT(strleft(\"asdf\", -15) == \"\");\n"
" ASSERT(strright(\"asdf\", 15) == \"asdf\");\n"
" ASSERT(strright(\"asdf\", -15) == \"\");\n"
"}\n"
);
}