better handling of short files in vips7 compat

the sniffer could read beyond the end of the file sometimes
This commit is contained in:
John Cupitt 2020-03-04 17:38:41 +00:00
parent 7d3a7e9c29
commit 9ef8b55218
5 changed files with 16 additions and 20 deletions

View File

@ -176,7 +176,7 @@ im_isvips( const char *filename )
{
unsigned char buf[4];
if( im__get_bytes( filename, buf, 4 ) ) {
if( im__get_bytes( filename, buf, 4 ) == 4 ) {
if( buf[0] == 0x08 && buf[1] == 0xf2 &&
buf[2] == 0xa6 && buf[3] == 0xb6 )
/* SPARC-order VIPS image.
@ -212,7 +212,7 @@ vips_flags( const char *filename )
flags = VIPS_FORMAT_PARTIAL;
if( im__get_bytes( filename, buf, 4 ) &&
if( im__get_bytes( filename, buf, 4 ) == 4 &&
buf[0] == 0x08 &&
buf[1] == 0xf2 &&
buf[2] == 0xa6 &&

View File

@ -751,15 +751,13 @@ G_DEFINE_TYPE( VipsForeignLoadMagick7File, vips_foreign_load_magick7_file,
static gboolean
ismagick7( const char *filename )
{
/* Fetch the first 100 bytes. Hopefully that'll be enough.
/* Fetch up to the first 100 bytes. Hopefully that'll be enough.
*/
unsigned char buf[100];
int len;
/* Files shorter than 100 bytes will leave nonsense at the end of buf,
* but it shouldn't matter.
*/
return( vips__get_bytes( filename, buf, 100 ) &&
magick_ismagick( buf, 100 ) );
return( (len = vips__get_bytes( filename, buf, 100 )) > 10 &&
magick_ismagick( buf, len ) );
}
static int

View File

@ -173,15 +173,13 @@ G_DEFINE_TYPE( VipsForeignLoadMagickFile, vips_foreign_load_magick_file,
static gboolean
ismagick( const char *filename )
{
/* Fetch the first 100 bytes. Hopefully that'll be enough.
/* Fetch up to the first 100 bytes. Hopefully that'll be enough.
*/
unsigned char buf[100];
int len;
/* Files shorter than 100 bytes will leave nonsense at the end of buf,
* but it shouldn't matter.
*/
return( vips__get_bytes( filename, buf, 100 ) &&
magick_ismagick( buf, 100 ) );
return( (len = vips__get_bytes( filename, buf, 100 )) > 10 &&
magick_ismagick( buf, len ) );
}
/* Unfortunately, libMagick does not support header-only reads very well. See

View File

@ -266,8 +266,8 @@ char *vips__file_read( FILE *fp, const char *name, size_t *length_out );
char *vips__file_read_name( const char *name, const char *fallback_dir,
size_t *length_out );
int vips__file_write( void *data, size_t size, size_t nmemb, FILE *stream );
guint64 vips__get_bytes( const char *filename,
unsigned char buf[], guint64 len );
gint64 vips__get_bytes( const char *filename,
unsigned char buf[], gint64 len );
int vips__fgetc( FILE *fp );
GValue *vips__gvalue_ref_string_new( const char *text );

View File

@ -874,13 +874,13 @@ vips__file_write( void *data, size_t size, size_t nmemb, FILE *stream )
* types, so we must read binary.
*
* Return the number of bytes actually read (the file might be shorter than
* len), or 0 for error.
* len), or -1 for error.
*/
guint64
vips__get_bytes( const char *filename, unsigned char buf[], guint64 len )
gint64
vips__get_bytes( const char *filename, unsigned char buf[], gint64 len )
{
int fd;
guint64 bytes_read;
gint64 bytes_read;
/* File may not even exist (for tmp images for example!)
* so no hasty messages. And the file might be truncated, so no error