问题描述
我正在尝试演示一个示例,其中我使用mprotect来更改字符串文字的值。
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
int main() {
char* ptr = "Test\n";
unsigned long sz = getpagesize() - 1;
// And off any page offset bits
void *pg = (void *) ((long) ptr & ~sz);
// This tells the OS that a page is readable/write
mprotect(pg,strlen(ptr),PROT_WRITE | PROT_READ);
strcpy(ptr,"Darn\n");
const char* ptr2 = "Test\n";
assert(ptr == "Test\n");//
assert(ptr == ptr2);
printf("%s",ptr2);
printf("Test\n");
assert(ptr == "Test\n");
assert(ptr == ptr2);
}
由于此演示在运行时不会失败断言,因此ptr == ptr2 ==“ Test \ n”,并且它们都应指向.rodata中的相同内存。那么,为什么第二个打印语句打印“ Test \ n”呢?
printf("%s","Test\n");
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)