The current checked_fread() macro compares fread() return value with the number of bytes to read :
#define checked_fread(item, size) \
if (fread(item, size, 1, Dataf) != size) \
{ \ fprintf(stderr, "unstr: data file corrupted\n"); \
exit(1); \
}
It should instead compare fread() return value with the number of items to read, which is 1 :
#define checked_fread(item, size) \
if (fread(item, size, 1, Dataf) != 1) \
{ \ fprintf(stderr, "unstr: data file corrupted\n"); \
exit(1); \
}
After changing the code, running the same commands give :
$ strfile fort
"fort.dat" created
There were 36 strings
Longest string: 201 bytes
Shortest string: 25 bytes
$ ls -l fort*
-rw-rw-r-- 1 br br 3383 Feb 23 10:31 fort
-rw-rw-r-- 1 br br 172 Feb 23 10:38 fort.dat
$ ./unstr fort
Input file: fort
nothing to do -- table in file order
Hot to reproduce :
$ strfile fort
"fort.dat" created
There were 36 strings
Longest string: 201 bytes
Shortest string: 25 bytes
$ ls -l fort*
-rw-rw-r-- 1 br br 3383 Feb 23 10:31 fort
-rw-rw-r-- 1 br br 172 Feb 23 10:31 fort.dat
$ unstr fort
Input file: fort
unstr: data file corrupted
The bug is line 234 of unstr.c (source file: https:/ /sources. debian. org/src/ fortune- mod/1%3A1. 99.1-7/ util/unstr. c/).
The current checked_fread() macro compares fread() return value with the number of bytes to read :
fprintf( stderr, "unstr: data file corrupted\n"); \
#define checked_fread(item, size) \
if (fread(item, size, 1, Dataf) != size) \
{ \
exit(1); \
}
It should instead compare fread() return value with the number of items to read, which is 1 :
fprintf( stderr, "unstr: data file corrupted\n"); \
#define checked_fread(item, size) \
if (fread(item, size, 1, Dataf) != 1) \
{ \
exit(1); \
}
After changing the code, running the same commands give :
$ strfile fort
"fort.dat" created
There were 36 strings
Longest string: 201 bytes
Shortest string: 25 bytes
$ ls -l fort*
-rw-rw-r-- 1 br br 3383 Feb 23 10:31 fort
-rw-rw-r-- 1 br br 172 Feb 23 10:38 fort.dat
$ ./unstr fort
Input file: fort
nothing to do -- table in file order