0

即使我正在使用 /dev/urandom,在调用该函数 4 次后,我仍然获得相同的地址位置。这是为什么 ?为什么 /dev/urandom 提供的数字非常低或非常高?

104
0x7fffb128de78
0x7f9e3c565000
4294967192
0x7fffb128de78
0x7f9e3c564000
97
0x7fffb128de78
0x7f9e3c563000
4294967139
0x7fffb128de78
0x7f9e3c562000
void test(void){
    char random[4];
    int filedescriptor = open("/dev/urandom", O_RDONLY);
    read(filedescriptor, random, 4);
    close(filedescriptor);

    unsigned int hintmemoryaddress = (unsigned int)random;
printf("%u\n", hintmemoryaddress);

    char *p = (char *)hintmemoryaddress;
    p += hintmemoryaddress;
printf("%p\n", &p);

    char *x = (char *)mmap((void *)&p, 1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
printf("%p\n", x);
}

int main(void){
    test();
    test();
    test();
    test();

    return 0;
}
4

1 回答 1

2

因为您将指针的值与其地址混淆了。你想用p,没有&p。此外,临时字符数组是无用的,你将它复制到周围都是不正确的。更正后的代码应该是:

void test(void){
    void *p;
    int filedescriptor = open("/dev/urandom", O_RDONLY);
    read(filedescriptor, &p, sizeof p);
    close(filedescriptor);

    char *x = (char *)mmap(p, 1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    printf("%p\n", x);
}
于 2013-06-22T18:06:06.197 回答