lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Nov 08, 2011 at 12:48:10PM -0200, Roberto Ierusalimschy wrote:
> I am trying to test errors in read operations (io.read - io.line). Does
> anyone know a reliable/automatic way to generate a read error on
> Linux? (That is, a situation where fread returns EOF and ferror returns
> not zero.)

Does this suffices?

tasi@birba:~$ cat /tmp/fail.c
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream){
	return 0;
}
int ferror(FILE *stream){
	return 1;
}
tassi@birba:~$ cat /tmp/read.c 
#include <stdio.h>
int main(){
	FILE* f = fopen("/tmp/read.c","r");
	char buf[10];
	printf("%d\n", fread(buf,1,10,f));
	printf("%d\n", ferror(f));
	return 0;
}
tassi@birba:~$ gcc -shared /tmp/fail.c -o /tmp/fail.so
tassi@birba:~$ gcc /tmp/read.c -o /tmp/read
tassi@birba:~$ /tmp/read
10
0
tassi@birba:~$ LD_PRELOAD=/tmp/fail.so /tmp/read
0
1
-- 
Enrico Tassi