aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/StringConversionHandler.cc
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-15 08:20:37 +0800
committer_ov4 <ov4@ov4.org>2026-10-15 08:20:37 +0800
commitab86ae0284469a9a941f5b196f9cc99a90305e3a (patch)
tree2e22e00e6b2c9465be964a58e10dd71f9aa457b4 /lib/CodeGen/StringConversionHandler.cc
parentbb64298ef26c9b42ae1bb50fb92b15e509b48c07 (diff)
fix(codegen): prevent NUM_TO_STR buffer overflow
Size REAL buffers for the full fixed-point double range and bound writes with snprintf. Register MinGW CRT aliases for embedded execution. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Diffstat (limited to 'lib/CodeGen/StringConversionHandler.cc')
-rw-r--r--lib/CodeGen/StringConversionHandler.cc11
1 files changed, 7 insertions, 4 deletions
diff --git a/lib/CodeGen/StringConversionHandler.cc b/lib/CodeGen/StringConversionHandler.cc
index 6f7461c..2263ef1 100644
--- a/lib/CodeGen/StringConversionHandler.cc
+++ b/lib/CodeGen/StringConversionHandler.cc
@@ -1,4 +1,5 @@
#include "cps/StringConversionHandler.h"
+#include <limits>
using namespace llvm;
using namespace cps;
@@ -9,8 +10,8 @@ StringConversionHandler::StringConversionHandler(LLVMContext &Ctx, IRBuilder<> &
}
void StringConversionHandler::setupExternalFunctions() {
- FunctionType *SprintfType = FunctionType::get(Type::getInt32Ty(Context), {PointerType::getUnqual(Context), PointerType::getUnqual(Context)}, true);
- SprintfFunc = Module.getOrInsertFunction("sprintf", SprintfType);
+ FunctionType *SnprintfType = FunctionType::get(Type::getInt32Ty(Context), {PointerType::getUnqual(Context), Type::getInt64Ty(Context), PointerType::getUnqual(Context)}, true);
+ SnprintfFunc = Module.getOrInsertFunction("snprintf", SnprintfType);
FunctionType *StrtollType = FunctionType::get(Type::getInt64Ty(Context), {PointerType::getUnqual(Context), PointerType::getUnqual(Context), Type::getInt32Ty(Context)}, false);
StrtollFunc = Module.getOrInsertFunction("strtoll", StrtollType);
@@ -23,7 +24,9 @@ void StringConversionHandler::setupExternalFunctions() {
}
Value *StringConversionHandler::emitNumToStr(Value *Num, bool IsReal) {
- Value *AllocSize = ConstantInt::get(Type::getInt64Ty(Context), 64);
+ // Allow an extra integer digit, sign, decimal point, six fractional digits and NUL.
+ Value *AllocSize = ConstantInt::get(Type::getInt64Ty(Context),
+ IsReal ? std::numeric_limits<double>::max_exponent10 + 10 : 64);
Value *Buffer = Builder.CreateCall(MallocFunc, AllocSize, "num_str_buf");
Value *FormatStr;
@@ -33,7 +36,7 @@ Value *StringConversionHandler::emitNumToStr(Value *Num, bool IsReal) {
FormatStr = Builder.CreateGlobalString("%lld");
}
- Builder.CreateCall(SprintfFunc, {Buffer, FormatStr, Num});
+ Builder.CreateCall(SnprintfFunc, {Buffer, AllocSize, FormatStr, Num});
return Buffer;
}