Make GetStringTableData native binary-safe (#1232)

Replace StringToLocalUTF8 with LocalToString and memcpy to make this binary compatible and update the documentation.
This commit is contained in:
thewavelength 2020-04-14 18:51:39 +02:00 committed by GitHub
parent 1d98c3a782
commit 25462071df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 11 deletions

View File

@ -184,8 +184,7 @@ static cell_t GetStringTableData(IPluginContext *pContext, const cell_t *params)
INetworkStringTable *pTable = netstringtables->GetTable(idx);
int stringidx;
const char *userdata;
int datalen;
size_t numBytes;
int datalen = 0;
if (!pTable)
{
@ -199,12 +198,22 @@ static cell_t GetStringTableData(IPluginContext *pContext, const cell_t *params)
}
userdata = (const char *)pTable->GetStringUserData(stringidx, &datalen);
if (!userdata)
{
userdata = "";
}
pContext->StringToLocalUTF8(params[3], params[4], userdata, &numBytes);
char *addr;
pContext->LocalToString(params[3], &addr);
int maxBytes = params[4];
size_t numBytes = MIN(maxBytes, datalen);
if (userdata)
{
memcpy(addr, userdata, numBytes);
}
else if (maxBytes > 0)
{
addr[0] = '\0';
numBytes = 0;
}
return numBytes;
}

View File

@ -119,9 +119,9 @@ native int GetStringTableDataLength(int tableidx, int stringidx);
*
* @param tableidx A string table index.
* @param stringidx A string index.
* @param userdata Buffer to store the user data. This will be set to "" if there is no user data.
* @param userdata Buffer to store the user data. This will be set to "" if there is no user data
* @param maxlength Maximum length of string buffer.
* @return Number of bytes written to the buffer (UTF-8 safe).
* @return Number of bytes written to the buffer (binary safe, includes the null terminator).
* @error Invalid string table index or string index.
*/
native int GetStringTableData(int tableidx, int stringidx, char[] userdata, int maxlength);
@ -133,10 +133,9 @@ native int GetStringTableData(int tableidx, int stringidx, char[] userdata, int
* @param stringidx A string index.
* @param userdata User data string that will be set.
* @param length Length of user data string. This should include the null terminator.
* @return Number of bytes written to the buffer (UTF-8 safe).
* @error Invalid string table index or string index.
*/
native int SetStringTableData(int tableidx, int stringidx, const char[] userdata, int length);
native void SetStringTableData(int tableidx, int stringidx, const char[] userdata, int length);
/**
* Adds a string to a given string table.