mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
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:
parent
4f49a4f1d0
commit
3243d89c78
1 changed files with 12 additions and 3 deletions
|
@ -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);
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue