Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions core/libs/prx/libc/src/Strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "prx/libc/include/General.hpp"

namespace {

int CompareUnits(char16_t left, char16_t right) {
return left < right ? -1 : 1;
}
Expand Down Expand Up @@ -44,6 +43,15 @@ auto ParseAsciiPrefix(const char16_t* text, char16_t** end, TParse parse) {
return value;
}

bool RangesOverlap(const void* first, std::size_t firstSize, const void* second, std::size_t secondSize) {
if (firstSize == 0 || secondSize == 0) return false;
const auto firstAddress = reinterpret_cast<std::uintptr_t>(first);
const auto secondAddress = reinterpret_cast<std::uintptr_t>(second);
return firstAddress <= secondAddress
? secondAddress - firstAddress < firstSize
: firstAddress - secondAddress < secondSize;
}

}

extern "C" {
Expand Down Expand Up @@ -230,6 +238,11 @@ int APS5_VABI strncpy_s_nid_postfix(char* dest, size_t destsz, const char* src,
dest[0] = '\0';
return GuestErange;
}
const size_t sourceSize = length + (length < count ? 1u : 0u);
if (RangesOverlap(dest, length + 1u, src, sourceSize)) {
dest[0] = '\0';
return GuestEinval;
}
std::memcpy(dest, src, length);
dest[length] = '\0';
return 0;
Expand All @@ -255,6 +268,11 @@ int APS5_VABI strncat_s_nid_postfix(char* dest, size_t destsz, const char* src,
dest[0] = '\0';
return GuestErange;
}
const size_t sourceSize = length + (length < count ? 1u : 0u);
if (RangesOverlap(dest + used, length + 1u, src, sourceSize)) {
dest[0] = '\0';
return GuestEinval;
}
std::memcpy(dest + used, src, length);
dest[used + length] = '\0';
return 0;
Expand All @@ -272,10 +290,7 @@ int APS5_VABI memcpy_s_nid_postfix(void* dest, size_t destsz, const void* src, s
std::memset(dest, 0, destsz);
return src ? GuestErange : GuestEinval;
}
const auto destination = reinterpret_cast<std::uintptr_t>(dest);
const auto source = reinterpret_cast<std::uintptr_t>(src);
const auto distance = destination < source ? source - destination : destination - source;
if (count != 0 && distance < count) {
if (RangesOverlap(dest, count, src, count)) {
std::memset(dest, 0, destsz);
return GuestEinval;
}
Expand Down
29 changes: 29 additions & 0 deletions core/libs/tests/GuestStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ char* APS5_VABI strtok_r_nid_postfix(char*, const char*, char**);
char* APS5_VABI strtok_nid_postfix(char*, const char*);
char* APS5_VABI strcasestr_nid_postfix(const char*, const char*);
int APS5_VABI strcpy_s_nid_postfix(char*, std::size_t, const char*);
int APS5_VABI strncpy_s_nid_postfix(char*, std::size_t, const char*, std::size_t);
int APS5_VABI strcat_s_nid_postfix(char*, std::size_t, const char*);
int APS5_VABI strncat_s_nid_postfix(char*, std::size_t, const char*, std::size_t);
int APS5_VABI memcpy_s_nid_postfix(void*, std::size_t, const void*, std::size_t);
Expand Down Expand Up @@ -66,10 +67,38 @@ static void CheckBoundsCheckedFunctions() {
Require(strcpy_s_nid_postfix(small, sizeof(small), "abc") == 0 && std::strcmp(small, "abc") == 0);
Require(strcpy_s_nid_postfix(small, sizeof(small), "abcd") == 34 && small[0] == '\0');
Require(strcpy_s_nid_postfix(nullptr, 4, "a") == 22);
char overlappingCopy[8] = "abcdef";
Require(strcpy_s_nid_postfix(overlappingCopy, sizeof(overlappingCopy), overlappingCopy + 1) == 22 && overlappingCopy[0] == '\0');
char overlappingBoundedCopy[8] = "abcdef";
Require(strncpy_s_nid_postfix(overlappingBoundedCopy, sizeof(overlappingBoundedCopy), overlappingBoundedCopy + 1, 3) == 22 && overlappingBoundedCopy[0] == '\0');
char disjointCopy[16] = {};
std::memcpy(disjointCopy + 8, "xy", 3);
Require(strcpy_s_nid_postfix(disjointCopy, sizeof(disjointCopy), disjointCopy + 8) == 0 && std::strcmp(disjointCopy, "xy") == 0);
char disjointBoundedCopy[16] = {};
std::memcpy(disjointBoundedCopy + 8, "xy", 3);
Require(strncpy_s_nid_postfix(disjointBoundedCopy, sizeof(disjointBoundedCopy), disjointBoundedCopy + 8, 2) == 0 && std::strcmp(disjointBoundedCopy, "xy") == 0);
char overlappingCopyTerminator[8] = {};
std::memcpy(overlappingCopyTerminator + 3, "xyz", 3);
Require(strncpy_s_nid_postfix(overlappingCopyTerminator, sizeof(overlappingCopyTerminator), overlappingCopyTerminator + 3, 3) == 22 && overlappingCopyTerminator[0] == '\0');
char joined[8] = "ab";
Require(strcat_s_nid_postfix(joined, sizeof(joined), "cd") == 0 && std::strcmp(joined, "abcd") == 0);
Require(strncat_s_nid_postfix(joined, sizeof(joined), "efgh", 2) == 0 && std::strcmp(joined, "abcdef") == 0);
Require(strcat_s_nid_postfix(joined, sizeof(joined), "gh") == 34 && joined[0] == '\0');
char disjointConcat[16] = "abc";
std::memcpy(disjointConcat + 8, "xy", 3);
Require(strcat_s_nid_postfix(disjointConcat, sizeof(disjointConcat), disjointConcat + 8) == 0 && std::strcmp(disjointConcat, "abcxy") == 0);
char disjointBoundedConcat[16] = "abc";
std::memcpy(disjointBoundedConcat + 8, "xy", 3);
Require(strncat_s_nid_postfix(disjointBoundedConcat, sizeof(disjointBoundedConcat), disjointBoundedConcat + 8, 2) == 0 && std::strcmp(disjointBoundedConcat, "abcxy") == 0);
char overlappingConcat[16] = "abc";
std::memcpy(overlappingConcat + 4, "xy", 3);
Require(strcat_s_nid_postfix(overlappingConcat, sizeof(overlappingConcat), overlappingConcat + 4) == 22 && overlappingConcat[0] == '\0');
char overlappingBoundedConcat[16] = "abc";
std::memcpy(overlappingBoundedConcat + 4, "xy", 3);
Require(strncat_s_nid_postfix(overlappingBoundedConcat, sizeof(overlappingBoundedConcat), overlappingBoundedConcat + 4, 2) == 22 && overlappingBoundedConcat[0] == '\0');
char overlappingConcatTerminator[12] = "abc";
std::memcpy(overlappingConcatTerminator + 5, "xy", 3);
Require(strncat_s_nid_postfix(overlappingConcatTerminator, sizeof(overlappingConcatTerminator), overlappingConcatTerminator + 5, 2) == 22 && overlappingConcatTerminator[0] == '\0');
char bytes[4] = {1, 2, 3, 4};
const char source[4] = {5, 6, 7, 8};
Require(memcpy_s_nid_postfix(bytes, sizeof(bytes), source, 2) == 0 && bytes[0] == 5 && bytes[2] == 3);
Expand Down