I am very confused with read()
calls to get inotify events.
Here is the code:
#define EVENT_SIZE sizeof(inotify_event)
int fd = inotify_init();
int wd = inotify_add_watch(fd, dir, IN_MODIFY);
void* p = malloc(sizeof(EVENT_SIZE));
read(wd, p, (EVENT_SIZE + 10));
My test file is a.txt
.
The output after debug in gdb is:
{wd = 0, mask = 0, cookie = 0, len = 0, name = 0x558f05d002d0 ""}
Now, when I change the last line to read(fd, p, (EVENT_SIZE + 16));
, the output that I get in gdb is:
{wd = 1, mask = 2, cookie = 0, len = 16, name = 0x5625cdd422d0 ".a.txt.swp"}
Q1. Why don't I get an overflow error because in both cases, I am writing more than the allocated buffer p
?
Q2. If there is no error, then my first program should also work because my filename is less than 10, but it doesn't work and it only works with 16. What am I missing here?
compiler - g++ 9.3.0
os - ubuntu 20.04
Thank you.
question from:
https://stackoverflow.com/questions/65860234/confused-with-read-system-call 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…