Skip to content

Commit cfc15cc

Browse files
authored
Fix string escape with %q in format() (#1159)
* fix escape string %q in format * fix clang format and optimizations * whitespace fix clang format
1 parent 79efc17 commit cfc15cc

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

Server/Components/Pawn/format.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -783,20 +783,23 @@ size_t atcprintf(D* buffer, size_t maxlen, const S* format, AMX* amx, const cell
783783
}
784784
if (argLen > 0)
785785
{
786-
++argLen;
787-
std::string strArg;
788-
strArg.resize(argLen);
786+
// using only cell
787+
DynamicArray<cell> escaped(cptr, cptr + argLen);
788+
cell quote = '\'';
789789

790-
amx_GetString((char*)strArg.data(), cptr, false, argLen);
791-
792-
size_t pos = 0;
793-
while ((pos = strArg.find('\'', pos)) != std::string::npos)
790+
for (size_t i = 0; i < escaped.size(); ++i)
794791
{
795-
strArg.insert(strArg.begin() + pos, '\'');
796-
pos += 2;
792+
if (escaped[i] == quote)
793+
{
794+
escaped.insert(escaped.begin() + i, quote);
795+
i++;
796+
}
797797
}
798798

799-
AddString(&buf_p, llen, strArg.c_str(), width, prec, flags);
799+
// null terminator
800+
escaped.push_back(0);
801+
802+
AddString(&buf_p, llen, escaped.data(), width, prec, flags);
800803
}
801804

802805
arg++;

0 commit comments

Comments
 (0)