mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-14 10:37:41 -04:00
Get rid of most _alloca use and remove ALLOCA().
This commit is contained in:
@ -174,8 +174,6 @@ inline void CDECL operator delete (void *, void *) {}
|
||||
#define MEMDUP(s, b) MemDup(s, b, 0, __FILE__, __LINE__)
|
||||
#define ZERO(s) MemSet(&s, 0, sizeof(s))
|
||||
#define ZEROPTR(p) MemSet(p, 0, sizeof(*p))
|
||||
// Client must #include <malloc.h>
|
||||
#define ALLOCA(t, n) (t *)_alloca((n) * sizeof(t))
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -415,7 +415,7 @@ void pfGameMgr::CreateGame (
|
||||
- sizeof(msg->createData)
|
||||
+ initBytes;
|
||||
|
||||
msg = (Cli2Srv_GameMgr_CreateGame *)_alloca(msgBytes);
|
||||
msg = (Cli2Srv_GameMgr_CreateGame *)malloc(msgBytes);
|
||||
|
||||
msg->messageId = kCli2Srv_GameMgr_CreateGame;
|
||||
msg->recvGameId = 0; // send to GameMgr on server
|
||||
@ -426,6 +426,8 @@ void pfGameMgr::CreateGame (
|
||||
MemCopy(msg->createData, initData, initBytes);
|
||||
|
||||
GameMgrSend(msg, NEWZERO(JoinTransState)(receiver));
|
||||
|
||||
free(msg);
|
||||
}
|
||||
|
||||
//============================================================================
|
||||
@ -443,7 +445,7 @@ void pfGameMgr::JoinCommonGame (
|
||||
- sizeof(msg->createData)
|
||||
+ initBytes;
|
||||
|
||||
msg = (Cli2Srv_GameMgr_JoinGame *)_alloca(msgBytes);
|
||||
msg = (Cli2Srv_GameMgr_JoinGame *)malloc(msgBytes);
|
||||
|
||||
msg->messageId = kCli2Srv_GameMgr_JoinGame;
|
||||
msg->recvGameId = 0; // send to GameMgr on server
|
||||
@ -455,6 +457,8 @@ void pfGameMgr::JoinCommonGame (
|
||||
MemCopy(msg->createData, initData, initBytes);
|
||||
|
||||
GameMgrSend(msg, NEWZERO(JoinTransState)(receiver));
|
||||
|
||||
free(msg);
|
||||
}
|
||||
|
||||
|
||||
|
@ -90,41 +90,40 @@ static unsigned s_nextLookupCancelId = 1;
|
||||
static void LookupProcess (Lookup * lookup, unsigned error) {
|
||||
unsigned count = 0;
|
||||
NetAddress * addrs = nil;
|
||||
for (ONCE) {
|
||||
if (error)
|
||||
break;
|
||||
|
||||
const HOSTENT & host = * (HOSTENT *) lookup->buffer;
|
||||
if (host.h_addrtype != AF_INET)
|
||||
break;
|
||||
if (host.h_length != sizeof(in_addr))
|
||||
break;
|
||||
if (!host.h_addr_list)
|
||||
break;
|
||||
if (error)
|
||||
return;
|
||||
|
||||
in_addr const * const * const inAddr = (in_addr **) host.h_addr_list;
|
||||
const HOSTENT & host = * (HOSTENT *) lookup->buffer;
|
||||
if (host.h_addrtype != AF_INET)
|
||||
return;
|
||||
if (host.h_length != sizeof(in_addr))
|
||||
return;
|
||||
if (!host.h_addr_list)
|
||||
return;
|
||||
|
||||
// count the number of addresses
|
||||
while (inAddr[count])
|
||||
++count;
|
||||
in_addr const * const * const inAddr = (in_addr **) host.h_addr_list;
|
||||
|
||||
// allocate a buffer large enough to hold all the addresses
|
||||
addrs = (NetAddress *) _alloca(sizeof(*addrs) * count);
|
||||
MemZero(addrs, sizeof(*addrs) * count);
|
||||
// count the number of addresses
|
||||
while (inAddr[count])
|
||||
++count;
|
||||
|
||||
// fill in address data
|
||||
const uint16_t port = htons((uint16_t) lookup->port);
|
||||
for (unsigned i = 0; i < count; ++i) {
|
||||
sockaddr_in * inetaddr = (sockaddr_in *) &addrs[i];
|
||||
inetaddr->sin_family = AF_INET;
|
||||
inetaddr->sin_addr = *inAddr[i];
|
||||
inetaddr->sin_port = port;
|
||||
}
|
||||
// allocate a buffer large enough to hold all the addresses
|
||||
addrs = (NetAddress *)malloc(sizeof(*addrs) * count);
|
||||
MemZero(addrs, sizeof(*addrs) * count);
|
||||
|
||||
if (host.h_name && host.h_name[0])
|
||||
StrToUnicode(lookup->name, host.h_name, arrsize(lookup->name));
|
||||
// fill in address data
|
||||
const uint16_t port = htons((uint16_t) lookup->port);
|
||||
for (unsigned i = 0; i < count; ++i) {
|
||||
sockaddr_in * inetaddr = (sockaddr_in *) &addrs[i];
|
||||
inetaddr->sin_family = AF_INET;
|
||||
inetaddr->sin_addr = *inAddr[i];
|
||||
inetaddr->sin_port = port;
|
||||
}
|
||||
|
||||
if (host.h_name && host.h_name[0])
|
||||
StrToUnicode(lookup->name, host.h_name, arrsize(lookup->name));
|
||||
|
||||
if (lookup->lookupProc)
|
||||
lookup->lookupProc(lookup->param, lookup->name, count, addrs);
|
||||
|
||||
@ -134,6 +133,8 @@ static void LookupProcess (Lookup * lookup, unsigned error) {
|
||||
ASSERT(!lookup->link.IsLinked());
|
||||
delete lookup;
|
||||
PerfSubCounter(kAsyncPerfNameLookupAttemptsCurr, 1);
|
||||
|
||||
free(addrs);
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
|
@ -99,9 +99,11 @@ static void AddValueString (
|
||||
const wchar_t src[]
|
||||
) {
|
||||
unsigned chars = StrLen(src) + 1;
|
||||
wchar_t * dst = ALLOCA(wchar_t, chars);
|
||||
wchar_t * dst = (wchar_t*)malloc(sizeof(wchar_t) * chars);
|
||||
StrTokenize(&src, dst, chars, L" \t\r\n\"");
|
||||
value->fArgs.Add(StrDup(dst));
|
||||
|
||||
free(dst);
|
||||
}
|
||||
|
||||
|
||||
|
@ -212,10 +212,12 @@ static bool AdvanceStep (
|
||||
++start;
|
||||
const char * term = StrChr(start, ';');
|
||||
if (term) {
|
||||
char * buffer = ALLOCA(char, term + 1 - start);
|
||||
char * buffer = (char*)malloc(term + 1 - start);
|
||||
StrCopy(buffer, start, term + 1 - start);
|
||||
Send(sock, "rcpt to:<", buffer, ">\r\n", nil);
|
||||
transaction->subStep = term + 1 - transaction->recipient;
|
||||
|
||||
free(buffer);
|
||||
}
|
||||
else {
|
||||
Send(sock, "rcpt to:<", start, ">\r\n", nil);
|
||||
@ -445,12 +447,7 @@ static void __cdecl Send (
|
||||
}
|
||||
|
||||
// Allocate string buffer
|
||||
char * packed;
|
||||
const unsigned kStackBufSize = 8 * 1024;
|
||||
if (bytes > kStackBufSize)
|
||||
packed = (char *) malloc(bytes);
|
||||
else
|
||||
packed = (char *) _alloca(bytes);
|
||||
char* packed = (char *) malloc(bytes);
|
||||
|
||||
// Pack the string
|
||||
{
|
||||
@ -466,8 +463,7 @@ static void __cdecl Send (
|
||||
AsyncSocketSend(sock, packed, bytes - 1);
|
||||
|
||||
// Free the string
|
||||
if (bytes > kStackBufSize)
|
||||
free(packed);
|
||||
free(packed);
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
|
@ -169,7 +169,7 @@ namespace pnNetCli {
|
||||
//============================================================================
|
||||
static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) {
|
||||
|
||||
uint8_t * temp, * heap = NULL;
|
||||
uint8_t * temp = NULL;
|
||||
|
||||
#ifndef PLASMA_EXTERNAL_RELEASE
|
||||
// Write to the netlog
|
||||
@ -189,13 +189,7 @@ static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) {
|
||||
#endif // PLASMA_EXTERNAL_RELEASE
|
||||
|
||||
if (cli->mode == kNetCliModeEncrypted && cli->cryptOut) {
|
||||
// Encrypt data...
|
||||
if (bytes <= 2048)
|
||||
// uint8_t count is small, use stack-based buffer
|
||||
temp = ALLOCA(uint8_t, bytes);
|
||||
else
|
||||
// uint8_t count is large, use heap-based buffer
|
||||
temp = heap = (uint8_t *)malloc(bytes);
|
||||
temp = (uint8_t *)malloc(bytes);
|
||||
|
||||
MemCopy(temp, data, bytes);
|
||||
CryptEncrypt(cli->cryptOut, bytes, temp);
|
||||
@ -205,7 +199,7 @@ static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) {
|
||||
AsyncSocketSend(cli->sock, data, bytes);
|
||||
|
||||
// free heap buffer (if any)
|
||||
free(heap);
|
||||
free(temp);
|
||||
}
|
||||
|
||||
//============================================================================
|
||||
@ -295,7 +289,7 @@ static void BufferedSendData (
|
||||
case kNetMsgFieldInteger: {
|
||||
const unsigned count = cmd->count ? cmd->count : 1;
|
||||
const unsigned bytes = cmd->size * count;
|
||||
void * temp = ALLOCA(uint8_t, bytes);
|
||||
void * temp = malloc(bytes);
|
||||
|
||||
if (count == 1)
|
||||
{
|
||||
@ -328,6 +322,8 @@ static void BufferedSendData (
|
||||
|
||||
// Write values to send buffer
|
||||
AddToSendBuffer(cli, bytes, temp);
|
||||
|
||||
free(temp);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -1112,15 +1108,10 @@ bool NetCliDispatch (
|
||||
do {
|
||||
if (cli->mode == kNetCliModeEncrypted) {
|
||||
// Decrypt data...
|
||||
uint8_t * temp, * heap = NULL;
|
||||
uint8_t * temp = NULL;
|
||||
|
||||
if (cli->cryptIn) {
|
||||
if (bytes <= 2048)
|
||||
// uint8_t count is small, use stack-based buffer
|
||||
temp = ALLOCA(uint8_t, bytes);
|
||||
else
|
||||
// uint8_t count is large, use heap-based buffer
|
||||
temp = heap = (uint8_t *)malloc(bytes);
|
||||
temp = (uint8_t *)malloc(bytes);
|
||||
|
||||
MemCopy(temp, data, bytes);
|
||||
CryptDecrypt(cli->cryptIn, bytes, temp);
|
||||
@ -1153,7 +1144,7 @@ bool NetCliDispatch (
|
||||
#endif
|
||||
|
||||
// free heap buffer (if any)
|
||||
free(heap);
|
||||
free(temp);
|
||||
|
||||
cli->input.Compact();
|
||||
return cli->recvDispatch;
|
||||
|
@ -991,9 +991,10 @@ void NetVaultNodeFieldArray::GetFieldValueString_LCS (
|
||||
case NetVaultNode::kString64_6:
|
||||
case NetVaultNode::kIString64_1:
|
||||
case NetVaultNode::kIString64_2: {
|
||||
wchar_t * tmp = ALLOCA(wchar_t, dstChars);
|
||||
wchar_t * tmp = (wchar_t*)malloc(sizeof(wchar_t) * dstChars);
|
||||
IStrSqlEscape(*(wchar_t **)fieldAddr, tmp, dstChars);
|
||||
StrPrintf(dst, dstChars, L"'%s'", tmp);
|
||||
free(tmp);
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -458,9 +458,11 @@ void OraGetUuid (
|
||||
occi::Bytes bytes = oraStmt->getBytes(index);
|
||||
if (const unsigned length = bytes.length()) {
|
||||
ASSERT(length == msizeof(Uuid, data));
|
||||
uint8_t * buf = ALLOCA(uint8_t, length);
|
||||
uint8_t * buf = malloc(length);
|
||||
bytes.getBytes(buf, length);
|
||||
GuidFromHex(buf, length, uuid);
|
||||
|
||||
free(buf)
|
||||
}
|
||||
else {
|
||||
GuidClear(uuid);
|
||||
|
@ -355,7 +355,7 @@ unsigned NetAddressGetLocal (
|
||||
// Create a buffer to sort the addresses
|
||||
NetAddressNode * dst;
|
||||
if (found > count)
|
||||
dst = ALLOCA(NetAddressNode, found);
|
||||
dst = (NetAddressNode*)_alloca(found * sizeof(NetAddressNode));
|
||||
else
|
||||
dst = addresses;
|
||||
|
||||
|
@ -96,36 +96,38 @@ void MemoryGetStatus (MemoryStatus * status) {
|
||||
|
||||
//============================================================================
|
||||
void DiskGetStatus (ARRAY(DiskStatus) * disks) {
|
||||
for (;;) {
|
||||
DWORD length = GetLogicalDriveStrings(0, NULL);
|
||||
if (!length || length > 2048)
|
||||
break;
|
||||
DWORD length = GetLogicalDriveStrings(0, NULL);
|
||||
if (!length || length > 2048)
|
||||
return;
|
||||
|
||||
wchar_t * buffer = ALLOCA(wchar_t, length + 1);
|
||||
if (!GetLogicalDriveStringsW(length, buffer))
|
||||
break;
|
||||
|
||||
for (; *buffer; buffer += StrLen(buffer) + 1) {
|
||||
UINT driveType = GetDriveTypeW(buffer);
|
||||
if (driveType != DRIVE_FIXED)
|
||||
continue;
|
||||
|
||||
ULARGE_INTEGER freeBytes;
|
||||
ULARGE_INTEGER totalBytes;
|
||||
if (!GetDiskFreeSpaceExW(buffer, &freeBytes, &totalBytes, NULL))
|
||||
continue;
|
||||
|
||||
DiskStatus status;
|
||||
StrCopy(status.name, buffer, arrsize(status.name));
|
||||
|
||||
const uint64_t BYTES_PER_MB = 1024 * 1024;
|
||||
status.totalSpaceMB = unsigned(totalBytes.QuadPart / BYTES_PER_MB);
|
||||
status.freeSpaceMB = unsigned(freeBytes.QuadPart / BYTES_PER_MB);
|
||||
|
||||
disks->Add(status);
|
||||
}
|
||||
break;
|
||||
wchar_t* buffer = (wchar_t*)malloc((length + 1) * sizeof(wchar_t));
|
||||
wchar_t* origbuf = buffer;
|
||||
if (!GetLogicalDriveStringsW(length, buffer))
|
||||
{
|
||||
free(buffer);
|
||||
return;
|
||||
}
|
||||
|
||||
for (; *buffer; buffer += StrLen(buffer) + 1) {
|
||||
UINT driveType = GetDriveTypeW(buffer);
|
||||
if (driveType != DRIVE_FIXED)
|
||||
continue;
|
||||
|
||||
ULARGE_INTEGER freeBytes;
|
||||
ULARGE_INTEGER totalBytes;
|
||||
if (!GetDiskFreeSpaceExW(buffer, &freeBytes, &totalBytes, NULL))
|
||||
continue;
|
||||
|
||||
DiskStatus status;
|
||||
StrCopy(status.name, buffer, arrsize(status.name));
|
||||
|
||||
const uint64_t BYTES_PER_MB = 1024 * 1024;
|
||||
status.totalSpaceMB = unsigned(totalBytes.QuadPart / BYTES_PER_MB);
|
||||
status.freeSpaceMB = unsigned(freeBytes.QuadPart / BYTES_PER_MB);
|
||||
|
||||
disks->Add(status);
|
||||
}
|
||||
free(origbuf);
|
||||
}
|
||||
|
||||
//============================================================================
|
||||
|
@ -166,9 +166,11 @@ static void Rc4Codec (
|
||||
void * data
|
||||
) {
|
||||
// RC4 uses the same algorithm to both encrypt and decrypt
|
||||
uint8_t * temp = ALLOCA(uint8_t, bytes);
|
||||
uint8_t * temp = (uint8_t *)malloc(bytes);
|
||||
RC4((RC4_KEY *)key->handle, bytes, (const unsigned char *)data, temp);
|
||||
MemCopy(data, temp, bytes);
|
||||
|
||||
free(temp);
|
||||
}
|
||||
|
||||
} using namespace Crypt;
|
||||
@ -337,7 +339,7 @@ void CryptHashPassword (
|
||||
unsigned passlen = StrLen(password);
|
||||
unsigned userlen = StrLen(username);
|
||||
|
||||
wchar_t * buffer = ALLOCA(wchar_t, passlen + userlen);
|
||||
wchar_t * buffer = (wchar_t*)malloc(sizeof(wchar_t) * (passlen + userlen));
|
||||
StrCopy(buffer, password, passlen);
|
||||
StrCopy(buffer + passlen, username, userlen);
|
||||
StrLower(buffer + passlen); // lowercase the username
|
||||
@ -348,6 +350,8 @@ void CryptHashPassword (
|
||||
(userlen + passlen) * sizeof(buffer[0]),
|
||||
buffer
|
||||
);
|
||||
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
//============================================================================
|
||||
|
@ -234,7 +234,7 @@ void PathSplitEmail (
|
||||
return;
|
||||
|
||||
// copy email address so we can tokenize it
|
||||
wchar_t * tmp = ALLOCA(wchar_t, len + 1);
|
||||
wchar_t * tmp = (wchar_t*)malloc(sizeof(wchar_t) * (len + 1));
|
||||
StrCopy(tmp, emailAddr, len + 1);
|
||||
const wchar_t * work = tmp;
|
||||
|
||||
@ -255,9 +255,11 @@ void PathSplitEmail (
|
||||
ARRAY(wchar_t *) arr;
|
||||
while (StrTokenize(&work, token, arrsize(token), L".")) {
|
||||
unsigned toklen = StrLen(token);
|
||||
wchar_t * str = ALLOCA(wchar_t, toklen + 1);
|
||||
wchar_t * str = (wchar_t*)malloc(sizeof(wchar_t) * (toklen + 1));
|
||||
StrCopy(str, token, toklen + 1);
|
||||
arr.Add(str);
|
||||
|
||||
free(str);
|
||||
}
|
||||
|
||||
// copy domains to output parameters
|
||||
@ -285,6 +287,8 @@ void PathSplitEmail (
|
||||
if (subDomains)
|
||||
StrCopy(&SUB_DOMAIN(index), arr[index], subDomainChars);
|
||||
}
|
||||
|
||||
free(tmp);
|
||||
|
||||
#undef SUB_DOMAIN
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ unsigned GuidHash (const Uuid & uuid) {
|
||||
static const wchar_t s_hexChars[] = L"0123456789ABCDEF";
|
||||
const wchar_t * GuidToHex (const Uuid & uuid, wchar_t * dst, unsigned chars) {
|
||||
|
||||
wchar_t * str = ALLOCA(wchar_t, sizeof(uuid.data) * 2 + 1);
|
||||
wchar_t * str = (wchar_t*)malloc((sizeof(uuid.data) * 2 + 1) * sizeof(wchar_t));
|
||||
wchar_t * cur = str;
|
||||
|
||||
for (unsigned i = 0; i < sizeof(uuid.data); ++i) {
|
||||
@ -91,6 +91,8 @@ const wchar_t * GuidToHex (const Uuid & uuid, wchar_t * dst, unsigned chars) {
|
||||
*cur = 0;
|
||||
|
||||
StrCopy(dst, str, chars);
|
||||
|
||||
free(str);
|
||||
return dst;
|
||||
}
|
||||
|
||||
@ -100,4 +102,4 @@ bool GuidFromHex (const uint8_t buf[], unsigned length, Uuid * uuid) {
|
||||
ASSERT(length == msizeof(Uuid, data));
|
||||
MemCopy(uuid->data, buf, msizeof(Uuid, data));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1084,7 +1084,7 @@ void NetCommSendMsg (
|
||||
msg->SetPlayerID(NetCommGetPlayer()->playerInt);
|
||||
|
||||
unsigned msgSize = msg->GetPackSize();
|
||||
uint8_t * buf = ALLOCA(uint8_t, msgSize);
|
||||
uint8_t * buf = (uint8_t *)malloc(msgSize);
|
||||
msg->PokeBuffer((char *)buf, msgSize);
|
||||
|
||||
switch (msg->GetNetProtocol()) {
|
||||
@ -1106,6 +1106,8 @@ void NetCommSendMsg (
|
||||
|
||||
DEFAULT_FATAL(msg->GetNetProtocol());
|
||||
}
|
||||
|
||||
free(buf);
|
||||
}
|
||||
|
||||
//============================================================================
|
||||
|
@ -566,17 +566,14 @@ void VaultSDLNode::SetStateDataRecord (const plStateDataRecord * rec, unsigned w
|
||||
ram.Rewind();
|
||||
|
||||
unsigned bytes = ram.GetEOF();
|
||||
uint8_t * buf, * heap = nil;
|
||||
if (bytes <= 2048)
|
||||
buf = ALLOCA(uint8_t, bytes);
|
||||
else
|
||||
buf = (uint8_t *)ALLOC(bytes);
|
||||
uint8_t * buf = nil;
|
||||
buf = (uint8_t *)malloc(bytes);
|
||||
|
||||
ram.CopyToMem(buf);
|
||||
|
||||
IVaultNodeSetBlob(kSDLData, base, &sdlData, &sdlDataLen, buf, bytes);
|
||||
|
||||
free(heap);
|
||||
free(buf);
|
||||
}
|
||||
#endif // def CLIENT
|
||||
|
||||
|
Reference in New Issue
Block a user