Add replacement helpers for strncpy[_s]

Portable without vulnerabilities.
This commit is contained in:
PJB3005
2026-03-27 16:54:51 +01:00
parent f6f2f14a60
commit abfe917008
3 changed files with 57 additions and 4 deletions
+52
View File
@@ -0,0 +1,52 @@
#ifndef DUSK_STRING_HPP
#define DUSK_STRING_HPP
#include "global.h"
#include <cstring>
#include <dolphin/os.h>
namespace dusk {
/**
* Copy a string to a fixed-size array.
* Truncates if the destination is not large enough, always inserts a null terminator (padding the remainder of the buffer with zeroes.)
*/
template <size_t BufSize>
void SafeStringCopyTruncate(char (&buffer)[BufSize], const char* src) {
static_assert(BufSize > 0, "Target buffer cannot be size zero");
// Can't just use strncpy as I can't figure out how to selectively mute the warnings on MSVC.
// And can't use strncpy_s on MSVC as it doesn't fill the entire buffer.
constexpr size_t copyBufSize = BufSize - 1;
const auto srcLen = strlen(src);
const auto copyLen = srcLen > copyBufSize ? copyBufSize : srcLen;
memcpy(buffer, src, copyLen);
memset(buffer + copyLen, 0, BufSize - copyLen);
}
/**
* Copy a string to a fixed-size array.
* Aborts if the destination is not large enough, always inserts a null terminator (padding the remainder of the buffer with zeroes.)
*/
template <size_t BufSize>
void SafeStringCopy(char (&buffer)[BufSize], const char* src) {
static_assert(BufSize > 0, "Target buffer cannot be size zero");
if (buffer == src) {
CRASH("Cannot copy string to same buffer");
}
// Can't just use strncpy as I can't figure out how to selectively mute the warnings on MSVC.
// And can't use strncpy_s on MSVC as it doesn't fill the entire buffer.
constexpr size_t copyBufSize = BufSize - 1;
const auto srcLen = strlen(src);
if (srcLen > copyBufSize) {
CRASH("Destination buffer too small!");
}
memcpy(buffer, src, srcLen);
memset(buffer + srcLen, 0, BufSize - srcLen);
}
}
#endif // DUSK_STRING_HPP
+2 -3
View File
@@ -15,6 +15,7 @@
#include "JSystem/JUtility/JUTAssert.h"
#include "JSystem/JUtility/JUTException.h"
#include "dusk/string.hpp"
#ifdef __MWERKS__
#include <stdint.h>
#else
@@ -701,9 +702,7 @@ JKRHeap* JKRHeap::getCurrentHeap() {
}
void JKRHeap::setName(const char* name) {
size_t len = strlen(name);
strncpy(mName, name, sizeof(mName) - 1);
mName[sizeof(mName) - 1] = '\0';
dusk::SafeStringCopyTruncate(mName, name);
}
void JKRHeap::setNamef(const char* fmt, ...) {
+3 -1
View File
@@ -21,6 +21,8 @@
#include "m_Do/m_Do_Reset.h"
#include <cstdio>
#include <cstring>
#include "dusk/string.hpp"
#if TARGET_PC
#include <format>
#include <fmt/ranges.h>
@@ -154,7 +156,7 @@ void dStage_startStage_c::set(const char* i_Name, s8 i_RoomNo, s16 i_Point, s8 i
#if TARGET_PC
// UB fix.
if (mName != i_Name) {
strncpy_s(mName, sizeof(mName), i_Name, sizeof(mName));
dusk::SafeStringCopy(mName, i_Name);
}
#else
strcpy(mName, i_Name);