Kernel: Fix sysclib strchr/strrchr, TLS pool leak, FPL counter

- sysclib strchr/strrchr searched for the whole string, not the
  character, so they returned 0 for any nonzero c.
- A thread ending with blocks from more than one TLS pool leaked every
  other block, since the restarted loop's ++iter skipped an entry.
- FPL's nextBlock grew by up to numBlocks per allocation and eventually
  overflowed into a negative block index. Old states with a negative one
  now reset it rather than failing to load.
- sysclib strncpy bounded its copy by the source's valid size only, not
  the destination's.
- sceKernelAllocHeapMemory wrapped for sizes near 4GB, and
  sceKernelGetModuleIdList could write a partial entry past the buffer
  size and dereferenced a missing module.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-10-26 21:42:06 +01:00
1 parent 210be3b311
commit 22b6fcbf22
4 files changed
+34 -24

No files matched your search

+10 -4
View File
@@ -978,7 +978,8 @@ static u32 sysclib_strncpy(u32 dest, u32 src, u32 size) {
// This is just regular strncpy, but being explicit to avoid warnings/safety fixes on missing null.
u32 i = 0;
u32 srcSize = Memory::ClampValidSizeAt(src, size);
const u32 destSize = Memory::ClampValidSizeAt(dest, size);
const u32 srcSize = std::min(Memory::ClampValidSizeAt(src, size), destSize);
const u8 *srcp = Memory::GetPointerUnchecked(src);
u8 *destp = Memory::GetPointerWriteUnchecked(dest);
for (i = 0; i < srcSize; ++i) {
@@ -988,7 +989,6 @@ static u32 sysclib_strncpy(u32 dest, u32 src, u32 size) {
*destp++ = c;
}
u32 destSize = Memory::ClampValidSizeAt(dest, size);
for (; i < destSize; ++i) {
*destp++ = 0;
}
@@ -1013,7 +1013,10 @@ static u32 sysclib_strchr(u32 src, int c) {
return hleLogError(Log::sceKernel, 0, "invalid address");
}
const std::string str = Memory::GetCharPointer(src);
size_t cpos = str.find(str, c);
if ((char)c == 0) {
return hleLogVerbose(Log::sceKernel, src + (u32)str.size());
}
size_t cpos = str.find((char)c);
if (cpos == std::string::npos) {
return hleLogVerbose(Log::sceKernel, 0);
}
@@ -1025,7 +1028,10 @@ static u32 sysclib_strrchr(u32 src, int c) {
return hleLogError(Log::sceKernel, 0, "invalid address");
}
const std::string str = Memory::GetCharPointer(src);
size_t cpos = str.rfind(str, c);
if ((char)c == 0) {
return hleLogVerbose(Log::sceKernel, src + (u32)str.size());
}
size_t cpos = str.rfind((char)c);
if (cpos == std::string::npos) {
return hleLogVerbose(Log::sceKernel, 0);
}