Convert to use snprintf or strncat

main
teknomunk 5 months ago
parent d28e14f352
commit 3b09172f01

@ -142,24 +142,24 @@ cstr_t SPrintFuncTypeSignature (IM3FuncType i_funcType)
{
static char string [256];
sprintf (string, "(");
snprintf (string, 256, "(");
for (u32 i = 0; i < i_funcType->numArgs; ++i)
{
if (i != 0)
strcat (string, ", ");
strncat (string, ", ", 256);
strcat (string, GetTypeName (d_FuncArgType(i_funcType, i)));
strncat (string, GetTypeName (d_FuncArgType(i_funcType, i)), 256);
}
strcat (string, ") -> ");
strncat (string, ") -> ", 256);
for (u32 i = 0; i < i_funcType->numRets; ++i)
{
if (i != 0)
strcat (string, ", ");
strncat (string, ", ", 256);
strcat (string, GetTypeName (d_FuncRetType(i_funcType, i)));
strncat (string, GetTypeName (d_FuncRetType(i_funcType, i)), 256);
}
return string;
@ -207,14 +207,14 @@ OpInfo find_operation_info (IM3Operation i_operation)
#undef fetch
#define fetch(TYPE) (* (TYPE *) ((*o_pc)++))
#define d_m3Decoder(FUNC) void Decode_##FUNC (char * o_string, u8 i_opcode, IM3Operation i_operation, IM3OpInfo i_opInfo, pc_t * o_pc)
#define d_m3Decoder(FUNC) void Decode_##FUNC (char * o_string, size_t n, u8 i_opcode, IM3Operation i_operation, IM3OpInfo i_opInfo, pc_t * o_pc)
d_m3Decoder (Call)
{
void * function = fetch (void *);
i32 stackOffset = fetch (i32);
sprintf (o_string, "%p; stack-offset: %d", function, stackOffset);
snprintf (o_string, n, "%p; stack-offset: %d", function, stackOffset);
}
@ -223,7 +223,7 @@ d_m3Decoder (Entry)
IM3Function function = fetch (IM3Function);
// only prints out the first registered name for the function
sprintf (o_string, "%s", m3_GetFunctionName(function));
snprintf (o_string, n, "%s", m3_GetFunctionName(function));
}
@ -234,7 +234,7 @@ d_m3Decoder (f64_Store)
u32 operand = fetch (u32);
u32 offset = fetch (u32);
sprintf (o_string, "offset= slot:%d + immediate:%d", operand, offset);
snprintf (o_string, n, "offset= slot:%d + immediate:%d", operand, offset);
}
// sprintf (o_string, "%s", function->name);
@ -244,14 +244,15 @@ d_m3Decoder (f64_Store)
d_m3Decoder (Branch)
{
void * target = fetch (void *);
sprintf (o_string, "%p", target);
snprintf (o_string, n, "%p", target);
}
d_m3Decoder (BranchTable)
{
u32 slot = fetch (u32);
o_string += sprintf (o_string, "slot: %" PRIu32 "; targets: ", slot);
int size = snprintf (o_string, n, "slot: %" PRIu32 "; targets: ", slot);
o_string += size; n -= size;
// IM3Function function = fetch2 (IM3Function);
@ -260,26 +261,27 @@ d_m3Decoder (BranchTable)
for (i32 i = 0; i < targets; ++i)
{
pc_t addr = fetch (pc_t);
o_string += sprintf (o_string, "%" PRIi32 "=%p, ", i, addr);
size = snprintf (o_string, n, "%" PRIi32 "=%p, ", i, addr);
o_string += size; n -= size;
}
pc_t addr = fetch (pc_t);
sprintf (o_string, "def=%p ", addr);
snprintf (o_string, n, "def=%p ", addr);
}
d_m3Decoder (Const)
{
u64 value = fetch (u64); i32 offset = fetch (i32);
sprintf (o_string, " slot [%d] = %" PRIu64, offset, value);
snprintf (o_string, n, " slot [%d] = %" PRIu64, offset, value);
}
#undef fetch
void DecodeOperation (char * o_string, u8 i_opcode, IM3Operation i_operation, IM3OpInfo i_opInfo, pc_t * o_pc)
void DecodeOperation (char * o_string, size_t n, u8 i_opcode, IM3Operation i_operation, IM3OpInfo i_opInfo, pc_t * o_pc)
{
#define d_m3Decode(OPCODE, FUNC) case OPCODE: Decode_##FUNC (o_string, i_opcode, i_operation, i_opInfo, o_pc); break;
#define d_m3Decode(OPCODE, FUNC) case OPCODE: Decode_##FUNC (o_string, n, i_opcode, i_operation, i_opInfo, o_pc); break;
switch (i_opcode)
{
@ -305,7 +307,7 @@ void dump_code_page (IM3CodePage i_codePage, pc_t i_startPC)
while (pc < end)
{
pc_t operationPC = pc;
//pc_t operationPC = pc;
IM3Operation op = (IM3Operation) (* pc++);
OpInfo i = find_operation_info (op);
@ -314,7 +316,7 @@ void dump_code_page (IM3CodePage i_codePage, pc_t i_startPC)
{
char infoString [8*1024] = { 0 };
DecodeOperation (infoString, i.opcode, op, i.info, & pc);
DecodeOperation (infoString, sizeof(infoString), i.opcode, op, i.info, & pc);
m3log (code, "%p | %20s %s", operationPC, i.info->name, infoString);
}
@ -397,18 +399,18 @@ void dump_type_stack (IM3Compilation o)
char item [100];
if (slot >= 0)
sprintf (item, "%s%s%d", type, location, slot);
snprintf (item, sizeof(item), "%s%s%d", type, location, slot);
else
sprintf (item, "%s%s", type, location);
snprintf (item, sizeof(item), "%s%s", type, location);
if (p == 1)
{
size_t s = strlen (item);
sprintf (item, "%d", i);
snprintf (item, sizeof(item), "%d", i);
while (strlen (item) < s)
strcat (item, " ");
strncat (item, " ", 100);
}
printf ("|%s ", item);

@ -8,7 +8,6 @@
#include "m3_env.h"
#include "m3_exception.h"
void Module_FreeFunctions (IM3Module i_module)
{
for (u32 i = 0; i < i_module->numFunctions; ++i)

@ -356,7 +356,7 @@ _ (ReadLEB_u32 (& size, & i_bytes, i_end));
if (size)
{
const u8 * ptr = i_bytes;
//const u8 * ptr = i_bytes;
i_bytes += size;
if (i_bytes <= i_end)

Loading…
Cancel
Save