Permalink
Browse files

handle some fread returns. handle no sauce in test program.

  • Loading branch information...
1 parent a9999b2 commit 519e785676f3da959a1426b4e2500144e6b3bdd3 @bricas committed Mar 29, 2010
Showing with 16 additions and 4 deletions.
  1. +1 −0 etc/nosauce.txt
  2. +10 −4 src/libsauce.c
  3. +5 −0 t/test.c
View
@@ -0,0 +1 @@
+No SAUCE record here.
View
@@ -28,10 +28,10 @@ void _read_record( FILE *file, sauce *record ) {
return;
}
- fread( record->id, sizeof( record->id ) - 1, 1, file );
+ int read_status = fread( record->id, sizeof( record->id ) - 1, 1, file );
record->id[ sizeof( record->id ) - 1 ] = '\0';
- if( strcmp( record->id, SAUCE_ID ) != 0 ) {
+ if( read_status != 1 || strcmp( record->id, SAUCE_ID ) != 0 ) {
free( record );
return;
}
@@ -81,7 +81,10 @@ void _read_comments( FILE *file, char **comment_lines, int comments ) {
if( fseek( file, 0 - ( RECORD_SIZE + 5 + COMMENT_SIZE * comments ), SEEK_END ) == 0 ) {
char id[ 6 ];
- fread( id, sizeof( id ) - 1, 1, file );
+ if( fread( id, sizeof( id ) - 1, 1, file ) != 1 ) {
+ printf( "COMNT record truncated: ID failed\n" );
+ exit( 1 );
+ }
id[ sizeof( id ) - 1 ] = '\0';
if( strcmp( id, COMMENT_ID ) != 0 ) {
@@ -92,7 +95,10 @@ void _read_comments( FILE *file, char **comment_lines, int comments ) {
for( i = 0; i < comments; i++ ) {
char buf[ COMMENT_SIZE + 1 ] = "";
- fread( buf, COMMENT_SIZE, 1, file );
+ if( fread( buf, COMMENT_SIZE, 1, file ) != 1 ) {
+ printf( "COMNT record truncated: comment line %d failed\n", i + 1 );
+ exit( 1 );
+ }
buf[ COMMENT_SIZE ] = '\0';
if( ferror( file ) == 0 ) {
View
@@ -8,6 +8,11 @@ int main( int argc, char *argv[] ) {
exit( EXIT_FAILURE );
}
+ if( strcmp( record->id, "SAUCE" ) != 0 ) {
+ printf( "File does not have a SAUCE record\n" );
+ return 0;
+ }
+
printf( "%9s: [%s]\n", "id", record->id );
printf( "%9s: [%s]\n", "version", record->version );
printf( "%9s: [%s]\n", "title", record->title );

0 comments on commit 519e785

Please sign in to comment.