Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 519e785676f3da959a1426b4e2500144e6b3bdd3 1 parent a9999b2
Brian Cassidy authored
1  etc/nosauce.txt
... ...
@@ -0,0 +1 @@
  1
+No SAUCE record here.
14  src/libsauce.c
@@ -28,10 +28,10 @@ void _read_record( FILE *file, sauce *record ) {
28 28
         return;
29 29
     }
30 30
 
31  
-    fread( record->id, sizeof( record->id ) - 1, 1, file );
  31
+    int read_status = fread( record->id, sizeof( record->id ) - 1, 1, file );
32 32
     record->id[ sizeof( record->id ) - 1 ] = '\0';
33 33
 
34  
-    if( strcmp( record->id, SAUCE_ID ) != 0 ) {
  34
+    if( read_status != 1 || strcmp( record->id, SAUCE_ID ) != 0 ) {
35 35
         free( record );
36 36
         return;
37 37
     }
@@ -81,7 +81,10 @@ void _read_comments( FILE *file, char **comment_lines, int comments ) {
81 81
 
82 82
     if( fseek( file, 0 - ( RECORD_SIZE + 5 + COMMENT_SIZE * comments ), SEEK_END ) == 0 ) {
83 83
         char id[ 6 ];
84  
-        fread( id, sizeof( id ) - 1, 1, file );
  84
+        if( fread( id, sizeof( id ) - 1, 1, file ) != 1 ) {
  85
+            printf( "COMNT record truncated: ID failed\n" );
  86
+            exit( 1 );
  87
+        }
85 88
         id[ sizeof( id ) - 1 ] = '\0';
86 89
 
87 90
         if( strcmp( id, COMMENT_ID ) != 0 ) {
@@ -92,7 +95,10 @@ void _read_comments( FILE *file, char **comment_lines, int comments ) {
92 95
         for( i = 0; i < comments; i++ ) {
93 96
             char buf[ COMMENT_SIZE + 1 ] = "";
94 97
 
95  
-            fread( buf, COMMENT_SIZE, 1, file );
  98
+            if( fread( buf, COMMENT_SIZE, 1, file ) != 1 ) {
  99
+                printf( "COMNT record truncated: comment line %d failed\n", i + 1 );
  100
+                exit( 1 );
  101
+            }
96 102
             buf[ COMMENT_SIZE ] = '\0';
97 103
 
98 104
             if( ferror( file ) == 0 ) {
5  t/test.c
@@ -8,6 +8,11 @@ int main( int argc, char *argv[] ) {
8 8
         exit( EXIT_FAILURE );
9 9
     }
10 10
 
  11
+    if( strcmp( record->id, "SAUCE" ) != 0 ) {
  12
+        printf( "File does not have a SAUCE record\n" );
  13
+        return 0;
  14
+    }
  15
+
11 16
     printf( "%9s: [%s]\n", "id", record->id );
12 17
     printf( "%9s: [%s]\n", "version", record->version );
13 18
     printf( "%9s: [%s]\n", "title", record->title );

0 notes on commit 519e785

Please sign in to comment.
Something went wrong with that request. Please try again.