With the following code:
#define MIN(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; })
...
size_t const recvBufLength = 50*1024*1024;
char * const recvBuf = malloc(recvBufLength);
...
while ((r = read(sockfd, recvBuf, MIN(recvLength-received, recvBufLength))) > 0) {
...
}
I'm getting this error:
/usr/include/x86_64-linux-gnu/bits/unistd.h: In function ‘main’:
/usr/include/x86_64-linux-gnu/bits/unistd.h:39:2: error: call to ‘__read_chk_warn’ declared with attribute warning: read called with bigger length than size of the destination buffer [-Werror]
return __read_chk (__fd, __buf, __nbytes, __bos0 (__buf));
^
lto1: all warnings being treated as errors
lto-wrapper: gcc returned 1 exit status
/usr/bin/ld: lto-wrapper failed
collect2: error: ld returned 1 exit status
If I get rid of the MIN
and change the read to read(sockfd, recvBuf, recvBufLength)
then it compiles without complaint.
Is this my mistake, or GCC's, or glibc's?
If not the former, then how can I circumvent the flawed length checking?
Edit:
Even expanding the MIN macro to a simple ternary operation still gives the error.
read(sockfd, recvBuf, (recvLength-received)<recvBufLength?(recvLength-received):recvBufLength)
This is all in GCC 4.8.2, I'll try other versions shortly.