fix(libc): return null when strdup allocation fails
This commit is contained in:
parent
53dc19cf61
commit
e1bfd03f23
|
|
@ -30,6 +30,10 @@ char* APS5_VABI strndup_nid_postfix(const char* source, std::size_t limit) {
|
|||
}
|
||||
}
|
||||
|
||||
char* APS5_VABI strdup_nid_postfix(const char* source) {
|
||||
return strndup_nid_postfix(source, static_cast<std::size_t>(-1));
|
||||
}
|
||||
|
||||
int APS5_VABI asprintf_nid_postfix(char** destination, const char* format, ...) {
|
||||
if (!destination) { errno = 22; return -1; }
|
||||
*destination = nullptr;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
#include "prx/libc/include/ApplicationHeap.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
|
@ -156,13 +155,6 @@ int APS5_VABI strncasecmp_nid_postfix(const char* s1, const char* s2, size_t n)
|
|||
return static_cast<unsigned char>(*s1) - static_cast<unsigned char>(*s2);
|
||||
}
|
||||
|
||||
char* APS5_VABI strdup_nid_postfix(const char* s) {
|
||||
std::size_t len = std::strlen(s) + 1;
|
||||
char* copy = static_cast<char*>(ApplicationHeapAllocate_nid_no_patch(len));
|
||||
std::memcpy(copy, s, len);
|
||||
return copy;
|
||||
}
|
||||
|
||||
int APS5_VABI bcmp_nid_postfix(const void* s1, const void* s2, size_t n) {
|
||||
return std::memcmp(s1, s2, n);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,9 @@ void APS5_VABI _ZdlPv_nid_postfix(void*);
|
|||
void APS5_VABI _ZdaPv_nid_postfix(void*);
|
||||
void APS5_VABI _ZdlPvSt11align_val_t_nid_postfix(void*, std::size_t);
|
||||
void* ApplicationHeapRealign_nid_no_patch(void*, std::size_t, std::size_t);
|
||||
char* APS5_VABI strdup_nid_postfix(const char*);
|
||||
char* APS5_VABI strndup_nid_postfix(const char*, std::size_t);
|
||||
int* APS5_VABI __error_nid_postfix();
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
|
@ -192,4 +195,23 @@ int main(int argc, char** argv) {
|
|||
reject([] { ApplicationHeapAllocate_nid_no_patch(64); });
|
||||
recurse = false;
|
||||
require(ApplicationHeapAllocate_nid_no_patch(64) == storage.data());
|
||||
const char text[] = "guest string";
|
||||
char* copy = strdup_nid_postfix(text);
|
||||
require(copy == reinterpret_cast<char*>(storage.data()) && lastSize == sizeof(text));
|
||||
require(std::strcmp(copy, text) == 0 && copy != text);
|
||||
ApplicationHeapFree_nid_no_patch(copy);
|
||||
copy = strdup_nid_postfix("");
|
||||
require(lastSize == 1 && copy[0] == '\0');
|
||||
ApplicationHeapFree_nid_no_patch(copy);
|
||||
copy = strndup_nid_postfix(text, 5);
|
||||
require(lastSize == 6 && std::strcmp(copy, "guest") == 0);
|
||||
ApplicationHeapFree_nid_no_patch(copy);
|
||||
fail = true;
|
||||
*__error_nid_postfix() = 0;
|
||||
require(strdup_nid_postfix(text) == nullptr && *__error_nid_postfix() == 12);
|
||||
*__error_nid_postfix() = 0;
|
||||
require(strdup_nid_postfix("") == nullptr && *__error_nid_postfix() == 12);
|
||||
*__error_nid_postfix() = 0;
|
||||
require(strndup_nid_postfix(text, 5) == nullptr && *__error_nid_postfix() == 12);
|
||||
fail = false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue