Bug #1 - Bad usage of <int-size>
While troubleshooting a problem I took a look at source for the read-buffer function and found something that I wasn't expecting, namely that when you pass it the <int-size> parameter to specify the maximum number of bytes to read, it will actually allocate a temporary buffer of that size.
From the documentation this is not evident. I assumed that 'read-buffer' would read from <int-file> in smaller chunks, copying what it read into a smaller temporary byte array and then copying that, chunk-by-chunk, into <sym-buffer>. In my opinion this is what the function *should* do because it handles efficiently all scenarios. I.e. what if you're expecting to get data anywhere from 1KB to 100MB? I don't think it makes sense in that situation to always allocate 100MB of memory, but it would be nice to be able to tell 'read-buffer' that such an amount of data is possible.
Bug #2 (severe) - function 'read' used incorrectly
Related to Bug #1, the C read function is used incorrectly, and actually this turned out to be the cause of the problem I was having (referenced in passing above), which is that read-buffer cannot currently be used to read large amounts of data (over ~64KB, this value is system dependent). From the man page on read:
Code: Select all
RETURN VALUES
If successful, the number of bytes actually read is returned. Upon read-
ing end-of-file, zero is returned. Otherwise, a -1 is returned and the
global variable errno is set to indicate the error.
Code: Select all
int read-buffer(int fd, unsigned char *buff, int possibleSize) {
int chunk_size = possibleSize > 8192 ? 8192 : possibleSize;
unsigned char *p = buff;
int amnt;
while ( (amnt = read(fd, p, chunk_size)) != 0 && possibleSize > 0 ) {
if ( amnt == -1 ) return -1; // error!
p += amnt;
possibleSize -= amnt;
if ( possibleSize < chunk_size )
chunk_size = possibleSize;
}
return (int)(p - buff);
}