Skip to content

Instantly share code, notes, and snippets.

@hankliu5
Created February 12, 2019 03:17
Show Gist options
  • Save hankliu5/13613855fc2941afefc15605849d07af to your computer and use it in GitHub Desktop.
Save hankliu5/13613855fc2941afefc15605849d07af to your computer and use it in GitHub Desktop.
#include <fcntl.h> /* O_ constants */
#include <unistd.h> /* ftruncate */
#include <sys/mman.h> /* mmap */
#include <string.h>
int main() {
int fd;
char *map;
char *name = "test.txt";
fd = open(name, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
posix_fallocate(fd, 0, 0);
/* THIS is the cause of the problem. */
/*ftruncate(fd, size);*/
map = mmap(NULL, 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
/* This is what generates the SIGBUS. */
*map = 0;
munmap(map, 4);
close(fd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment