The usual way to do this on POSIX systems is to use the write()
system call. It will return EFAULT
in errno
rather than raising a signal if the memory cannot be read:
int nullfd = open("/dev/random", O_WRONLY);
if (write(nullfd, pointer, size) < 0)
{
/* Not OK */
}
close(nullfd);
(/dev/random
is a good device to use for this on Linux, because it can be written by any user and will actually try to read the memory given. On OSes without /dev/random
or where it isn't writeable, try /dev/null
). Another alternative would be an anonymous pipe, but if you want to test a large region you'll need to regularly clear the reading end of the pipe.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…