[x264-devel] slurp_file: Various minor bug fixes

Henrik Gramner git at videolan.org
Sun Jul 26 22:26:29 CEST 2015


x264 | branch: master | Henrik Gramner <henrik at gramner.com> | Tue Jun 23 22:08:35 2015 +0200| [936e8da1a4f9d0431b181d0877bb1602d4de9441] | committer: Anton Mitrofanov

slurp_file: Various minor bug fixes

 * Fix unsigned <= 0 check.
 * Add additional size sanity check on 32-bit systems.
 * Don't read uninitialized data if fread() fails.

> http://git.videolan.org/gitweb.cgi/x264.git/?a=commit;h=936e8da1a4f9d0431b181d0877bb1602d4de9441
---

 common/common.c |   15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/common/common.c b/common/common.c
index 5559d67..28eec1f 100644
--- a/common/common.c
+++ b/common/common.c
@@ -1274,29 +1274,36 @@ REDUCE_FRACTION( x264_reduce_fraction64, uint64_t )
 char *x264_slurp_file( const char *filename )
 {
     int b_error = 0;
-    size_t i_size;
+    int64_t i_size;
     char *buf;
     FILE *fh = x264_fopen( filename, "rb" );
     if( !fh )
         return NULL;
+
     b_error |= fseek( fh, 0, SEEK_END ) < 0;
     b_error |= ( i_size = ftell( fh ) ) <= 0;
+    if( WORD_SIZE == 4 )
+        b_error |= i_size > INT32_MAX;
     b_error |= fseek( fh, 0, SEEK_SET ) < 0;
     if( b_error )
         goto error;
+
     buf = x264_malloc( i_size+2 );
     if( !buf )
         goto error;
+
     b_error |= fread( buf, 1, i_size, fh ) != i_size;
-    if( buf[i_size-1] != '\n' )
-        buf[i_size++] = '\n';
-    buf[i_size] = 0;
     fclose( fh );
     if( b_error )
     {
         x264_free( buf );
         return NULL;
     }
+
+    if( buf[i_size-1] != '\n' )
+        buf[i_size++] = '\n';
+    buf[i_size] = '\0';
+
     return buf;
 error:
     fclose( fh );



More information about the x264-devel mailing list