Fix crash with CBot string functions out of range (closes #704)
parent
048534e89d
commit
9ff978155c
|
@ -70,6 +70,9 @@ bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
|
||||||
// retrieves this number
|
// retrieves this number
|
||||||
int n = pVar->GetValInt();
|
int n = pVar->GetValInt();
|
||||||
|
|
||||||
|
if (n > static_cast<int>(s.length())) n = s.length();
|
||||||
|
if (n < 0) n = 0;
|
||||||
|
|
||||||
// no third parameter
|
// no third parameter
|
||||||
if ( pVar->GetNext() != nullptr ) { ex = CBotErrOverParam ; return true; }
|
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
|
// retrieves this number
|
||||||
int n = pVar->GetValInt();
|
int n = pVar->GetValInt();
|
||||||
|
|
||||||
|
if (n > static_cast<int>(s.length())) n = s.length();
|
||||||
|
if (n < 0) n = 0;
|
||||||
|
|
||||||
// no third parameter
|
// no third parameter
|
||||||
if ( pVar->GetNext() != nullptr ) { ex = CBotErrOverParam ; return true; }
|
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
|
// retrieves this number
|
||||||
int n = pVar->GetValInt();
|
int n = pVar->GetValInt();
|
||||||
|
|
||||||
|
if (n > static_cast<int>(s.length())) n = s.length();
|
||||||
|
if (n < 0) n = 0;
|
||||||
|
|
||||||
// third parameter optional
|
// third parameter optional
|
||||||
if ( pVar->GetNext() != nullptr )
|
if ( pVar->GetNext() != nullptr )
|
||||||
{
|
{
|
||||||
|
@ -147,6 +156,9 @@ bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser )
|
||||||
// retrieves this number
|
// retrieves this number
|
||||||
int l = pVar->GetValInt();
|
int l = pVar->GetValInt();
|
||||||
|
|
||||||
|
if (l > static_cast<int>(s.length())) l = s.length();
|
||||||
|
if (l < 0) l = 0;
|
||||||
|
|
||||||
// but no fourth parameter
|
// but no fourth parameter
|
||||||
if ( pVar->GetNext() != nullptr ){ ex = CBotErrOverParam ; return true; }
|
if ( pVar->GetNext() != nullptr ){ ex = CBotErrOverParam ; return true; }
|
||||||
|
|
||||||
|
|
|
@ -969,6 +969,16 @@ TEST_F(CBotUT, StringFunctions)
|
||||||
" ASSERT(strfind(s, \"o\") == 1);\n"
|
" ASSERT(strfind(s, \"o\") == 1);\n"
|
||||||
" ASSERT(strval(\"2.5\") == 2.5);\n"
|
" ASSERT(strval(\"2.5\") == 2.5);\n"
|
||||||
"}\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"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue