From 04dad55e49308529ec81b2bb61a73d37648beec5 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 16 Feb 2016 09:56:14 +0000 Subject: [PATCH] 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 ); }