From ef6d8f597985ca48c20f307887e02da9395b2720 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 16 Feb 2016 09:53:04 +0000 Subject: [PATCH 1/2] make test for matlab files more specific Previously vips just called Mat_Open() to test if a file was a Matlab save file, but this is rather unreliable. For example, some JPEG files can crash libmatio, and it can incorrectly think that at least some JP2 files are Matlab save files. Instead, look for "MATLAB 5.0" at the start of the file. This is really too specific, the first 116 bytes of a Matlab save file are freeform text, but in practice all Matlab writers use the first few bytes to record the file type. See https://github.com/jcupitt/libvips/issues/385 --- ChangeLog | 1 + libvips/foreign/matlab.c | 13 +++++++------ libvips/foreign/matload.c | 5 ----- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1cc12a86..4a5ccd76 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,7 @@ - bicubic is better on 32-bit int images - add pdfload, svgload, gifload for PDF, SVG and GIF rendering - vipsthumbnail knows about pdfload and svgload +- matload is more specific (thanks bithive) 27/1/16 started 8.2.3 - fix a crash with SPARC byte-order labq vips images diff --git a/libvips/foreign/matlab.c b/libvips/foreign/matlab.c index a7663f55..a4b3d576 100644 --- a/libvips/foreign/matlab.c +++ b/libvips/foreign/matlab.c @@ -7,6 +7,8 @@ * 21/8/14 * - swap width/height * - set interpretation to rgb16 etc. + * 16/2/16 + * - more specific is_a test */ /* @@ -319,14 +321,13 @@ vips__mat_load( const char *filename, VipsImage *out ) int vips__mat_ismat( const char *filename ) { - mat_t *mat; + unsigned char buf[15]; - vips_error_freeze(); - mat = Mat_Open( filename, MAT_ACC_RDONLY ); - Mat_Close( mat ); - vips_error_thaw(); + if( vips__get_bytes( filename, buf, 10 ) && + vips_isprefix( "MATLAB 5.0", (char *) buf ) ) + return( 1 ); - return( mat != NULL ); + return( 0 ); } const char *vips__mat_suffs[] = { ".mat", NULL }; diff --git a/libvips/foreign/matload.c b/libvips/foreign/matload.c index 2101ccde..4423894a 100644 --- a/libvips/foreign/matload.c +++ b/libvips/foreign/matload.c @@ -122,11 +122,6 @@ vips_foreign_load_mat_class_init( VipsForeignLoadMatClass *class ) foreign_class->suffs = vips__mat_suffs; - /* We need to be lower priority than the jpeg loader, since some jpegs - * can make libmatio segv on Mat_Open(). - */ - foreign_class->priority = -50; - load_class->is_a = vips__mat_ismat; load_class->get_flags_filename = vips_foreign_load_mat_get_flags_filename; From 04dad55e49308529ec81b2bb61a73d37648beec5 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 16 Feb 2016 09:56:14 +0000 Subject: [PATCH 2/2] faster and safer isprefix --- libvips/iofuncs/util.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/libvips/iofuncs/util.c b/libvips/iofuncs/util.c index 80c874a8..dd531983 100644 --- a/libvips/iofuncs/util.c +++ b/libvips/iofuncs/util.c @@ -319,21 +319,24 @@ vips_ispostfix( const char *a, const char *b ) return( strcmp( a + m - n, b ) == 0 ); } -/* Test for string a starts string b. +/* Test for string a starts string b. a is a known-good string, b may be + * random data. */ gboolean vips_isprefix( const char *a, const char *b ) { - int n = strlen( a ); - int m = strlen( b ); int i; - if( m < n ) - return( FALSE ); - for( i = 0; i < n; i++ ) + for( i = 0; a[i] && b[i]; i++ ) if( a[i] != b[i] ) return( FALSE ); - + + /* If there's stuff left in a but b has finished, we must have a + * mismatch. + */ + if( a[i] && !b[i] ) + return( FALSE ); + return( TRUE ); }