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.
This commit is contained in:
Gautham 2025-01-12 13:15:08 -06:00
parent 4f49a4f1d0
commit 3243d89c78

View file

@ -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;
}