From 3243d89c781258d06b6aedc89b1b604aaf295366 Mon Sep 17 00:00:00 2001 From: Gautham Date: Sun, 12 Jan 2025 13:15:08 -0600 Subject: [PATCH] add check for replacement at end of section if the string to replace is at the absolute end of the .rodata section, it may not have a null terminating byte. In that case alone, we replace one less byte than earlier, so as to avoid overwriting some other data. --- tool/build/renamestr.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tool/build/renamestr.c b/tool/build/renamestr.c index cce0c4b67..1364bc6c3 100644 --- a/tool/build/renamestr.c +++ b/tool/build/renamestr.c @@ -23,6 +23,7 @@ #include "libc/elf/struct/ehdr.h" #include "libc/elf/struct/phdr.h" #include "libc/intrin/kprintf.h" +#include "libc/intrin/likely.h" #include "libc/macros.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" @@ -205,10 +206,18 @@ static void OpenInput(const char *path) { } static void ReplaceString(struct Param *param) { - Elf64_Xword len = strnlen(param->roloc, roend - param->roloc); + size_t len; + char *x = (char *)memchr(param->roloc, 0, roend - param->roloc); memmove(param->roloc, param->to.str, param->to.len); - memmove(param->roloc + param->to.len, param->roloc + param->from.len, - len + 1 - param->from.len); + if (UNLIKELY(x == NULL)) { + len = roend - param->roloc; + memmove(param->roloc + param->to.len, param->roloc + param->from.len, + len - param->from.len); + } else { + len = x - param->roloc; + memmove(param->roloc + param->to.len, param->roloc + param->from.len, + len + 1 - param->from.len); + } param->roloc += param->to.len; }