-------------------------------------------------------------------------------------
int rtctimer_next_tick( rtctimer_t *rtctimer )
{
unsigned long rtc_data;
struct pollfd pfd;
pfd.fd = rtctimer->rtc_fd;
pfd.events = POLLIN | POLLERR;
again:
if( poll( &pfd, 1, 100000 ) < 0 ) {
if( errno == EINTR ) {
goto again;
}
if( rtctimer->verbose ) {
fprintf( stderr, "rtctimer: poll call failed: %s\n",
strerror( errno ) );
}
return 0;
}
read( rtctimer->rtc_fd, &rtc_data, sizeof( rtc_data ) );
return 1;
}
-------------------------------------------------------------------------------------
I compiled tvtime and the following warning popped up. One of them is:
-------------------------------------------------------------------------------------
rtctimer.c: In function 'rtctimer_next_tick':
rtctimer.c: warning: ignoring return value of 'read', declared with attribute warn_unused_result [-Wunused-result]
-------------------------------------------------------------------------------------
If you don't check what read returns, chances are very high that your program will fail unexpectedly if a read error does occur, and it will be impossible to debug.
Always check the return value of library calls, especially I/O ones since they do fail in normal circumstances.
Thancks.
You're not checking the return value of read, which is a bug just waiting to happen.
ssize_t r = read(rtctimer->rtc_fd, &rtc_data, sizeof( rtc_data ) );
if (r == -1) {
// deal with failed read
} else if (r != sizeof(rtc_data)) {
// you didn't read as much as you wanted to
...
If you don't check what read returns, chances are very high that your program will fail unexpectedly if a read error does occur, and it will be impossible to debug. (The second case might not apply in this specific example, or for some pipe reads.)
Always check the return value of library calls, especially I/O ones since they do fail in normal circumstances.