From d7037618fccacf99e97d5d088aa219b5adc9935c Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Sun, 1 Dec 2013 12:18:16 +0000 Subject: [PATCH] use a define for PATH_MAX add VIPS_PATH_MAX, a long-enough-for-almost-any-path buffer size we ought to switch to dynamic paths really ... do this when we fix filename encoding --- .gitignore | 1 + libvips/deprecated/dispatch_types.c | 4 --- libvips/deprecated/package.c | 8 ++--- libvips/foreign/dzsave.c | 55 +++++++++++++++-------------- libvips/include/vips/util.h | 5 +++ libvips/iofuncs/header.c | 4 +-- libvips/iofuncs/init.c | 35 +++++++++--------- libvips/iofuncs/object.c | 24 +++++++------ libvips/iofuncs/system.c | 8 ++--- libvips/iofuncs/util.c | 9 ++--- 10 files changed, 79 insertions(+), 74 deletions(-) diff --git a/.gitignore b/.gitignore index 03ef54ad..955e04bd 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ Vips-8.0.typelib .*.swp *.lo *.bak +*.vect *.la Vips-8.0.gir Vips-8.0.typelib diff --git a/libvips/deprecated/dispatch_types.c b/libvips/deprecated/dispatch_types.c index a05fbef2..66dc73e2 100644 --- a/libvips/deprecated/dispatch_types.c +++ b/libvips/deprecated/dispatch_types.c @@ -49,10 +49,6 @@ #include #include -/* Max str we parse. - */ -#define IM_MAX_STR (4096) - /* String containing each of the characters which can be used within a * single command line argument to separate the elements of a vector. */ diff --git a/libvips/deprecated/package.c b/libvips/deprecated/package.c index 8ecfd61b..df2194bc 100644 --- a/libvips/deprecated/package.c +++ b/libvips/deprecated/package.c @@ -705,7 +705,7 @@ int im_load_plugins( const char *fmt, ... ) { va_list ap; - char dir_name[4096]; + char dir_name[VIPS_PATH_MAX]; GDir *dir; const char *name; int result; @@ -716,7 +716,7 @@ im_load_plugins( const char *fmt, ... ) return( 0 ); va_start( ap, fmt ); - (void) im_vsnprintf( dir_name, 4096 - 1, fmt, ap ); + (void) im_vsnprintf( dir_name, VIPS_PATH_MAX - 1, fmt, ap ); va_end( ap ); #ifdef DEBUG @@ -731,9 +731,9 @@ im_load_plugins( const char *fmt, ... ) result = 0; while( (name = g_dir_read_name( dir )) ) if( im_ispostfix( name, ".plg" ) ) { - char path[4096]; + char path[VIPS_PATH_MAX]; - im_snprintf( path, 4096 - 1, + im_snprintf( path, VIPS_PATH_MAX - 1, "%s" G_DIR_SEPARATOR_S "%s", dir_name, name ); if( !im_load_plugin( path ) ) result = -1; diff --git a/libvips/foreign/dzsave.c b/libvips/foreign/dzsave.c index 0f3e9039..1d6ef188 100644 --- a/libvips/foreign/dzsave.c +++ b/libvips/foreign/dzsave.c @@ -363,12 +363,12 @@ pyramid_build( VipsForeignSaveDz *dz, Layer *above, static int pyramid_mkdir( VipsForeignSaveDz *dz ) { - char buf[4096]; + char buf[VIPS_PATH_MAX]; if( dz->layout == VIPS_FOREIGN_DZ_LAYOUT_DZ ) - vips_snprintf( buf, 4096, "%s_files", dz->basename ); + vips_snprintf( buf, VIPS_PATH_MAX, "%s_files", dz->basename ); else - vips_snprintf( buf, 4096, "%s", dz->basename ); + vips_snprintf( buf, VIPS_PATH_MAX, "%s", dz->basename ); if( vips_existsf( "%s", buf ) ) { vips_error( "dzsave", @@ -385,14 +385,14 @@ static int write_dzi( VipsForeignSaveDz *dz ) { FILE *fp; - char buf[4096]; + char buf[VIPS_PATH_MAX]; char *p; - vips_snprintf( buf, 4096, "%s.dzi", dz->basename ); + vips_snprintf( buf, VIPS_PATH_MAX, "%s.dzi", dz->basename ); if( !(fp = vips__file_open_write( buf, TRUE )) ) return( -1 ); - vips_snprintf( buf, 4096, "%s", dz->suffix + 1 ); + vips_snprintf( buf, VIPS_PATH_MAX, "%s", dz->suffix + 1 ); if( (p = (char *) vips__find_rightmost_brackets( buf )) ) *p = '\0'; @@ -418,9 +418,10 @@ static int write_properties( VipsForeignSaveDz *dz ) { FILE *fp; - char buf[4096]; + char buf[VIPS_PATH_MAX]; - vips_snprintf( buf, 4096, "%s/ImageProperties.xml", dz->basename ); + vips_snprintf( buf, VIPS_PATH_MAX, + "%s/ImageProperties.xml", dz->basename ); if( !(fp = vips__file_open_write( buf, TRUE )) ) return( -1 ); @@ -440,14 +441,14 @@ write_properties( VipsForeignSaveDz *dz ) static int write_blank( VipsForeignSaveDz *dz ) { - char buf[4096]; + char buf[VIPS_PATH_MAX]; VipsImage *x, *t; int n; VipsArea *ones; double *d; int i; - vips_snprintf( buf, 4096, "%s/blank.png", dz->basename ); + vips_snprintf( buf, VIPS_PATH_MAX, "%s/blank.png", dz->basename ); if( vips_black( &x, dz->tile_size, dz->tile_size, NULL ) ) return( -1 ); @@ -710,8 +711,8 @@ tile_name( Layer *layer, char *buf, int x, int y ) VipsForeignSaveDz *dz = layer->dz; - char dirname[4096]; - char dirname2[4096]; + char dirname[VIPS_PATH_MAX]; + char dirname2[VIPS_PATH_MAX]; Layer *p; int n; @@ -721,10 +722,10 @@ tile_name( Layer *layer, char *buf, int x, int y ) switch( dz->layout ) { case VIPS_FOREIGN_DZ_LAYOUT_DZ: - vips_snprintf( dirname, 4096, "%s_files/%d", - dz->basename, layer->n ); - vips_snprintf( buf, 4096, "%s/%d_%d%s", - dirname, x, y, dz->suffix ); + vips_snprintf( dirname, VIPS_PATH_MAX, + "%s_files/%d", dz->basename, layer->n ); + vips_snprintf( buf, VIPS_PATH_MAX, + "%s/%d_%d%s", dirname, x, y, dz->suffix ); if( !vips_existsf( "%s", dirname ) && vips_mkdirf( "%s", dirname ) ) { @@ -751,10 +752,10 @@ tile_name( Layer *layer, char *buf, int x, int y ) */ n += y * layer->tiles_across + x; - vips_snprintf( dirname, 4096, "%s/TileGroup%d", - dz->basename, n / 256 ); - vips_snprintf( buf, 4096, "%s/%d-%d-%d%s", - dirname, layer->n, x, y, dz->suffix ); + vips_snprintf( dirname, VIPS_PATH_MAX, + "%s/TileGroup%d", dz->basename, n / 256 ); + vips_snprintf( buf, VIPS_PATH_MAX, + "%s/%d-%d-%d%s", dirname, layer->n, x, y, dz->suffix ); /* Used at the end in ImageProperties.xml */ @@ -769,12 +770,12 @@ tile_name( Layer *layer, char *buf, int x, int y ) break; case VIPS_FOREIGN_DZ_LAYOUT_GOOGLE: - vips_snprintf( dirname, 4096, "%s/%d", - dz->basename, layer->n ); - vips_snprintf( dirname2, 4096, "%s/%d", - dirname, y ); - vips_snprintf( buf, 4096, "%s/%d%s", - dirname2, x, dz->suffix ); + vips_snprintf( dirname, VIPS_PATH_MAX, + "%s/%d", dz->basename, layer->n ); + vips_snprintf( dirname2, VIPS_PATH_MAX, + "%s/%d", dirname, y ); + vips_snprintf( buf, VIPS_PATH_MAX, + "%s/%d%s", dirname2, x, dz->suffix ); if( !vips_existsf( "%s", dirname ) && vips_mkdirf( "%s", dirname ) ) { @@ -808,7 +809,7 @@ strip_work( VipsThreadState *state, void *a ) Layer *layer = strip->layer; VipsForeignSaveDz *dz = layer->dz; - char buf[4096]; + char buf[VIPS_PATH_MAX]; VipsImage *x; VipsImage *t; diff --git a/libvips/include/vips/util.h b/libvips/include/vips/util.h index a1d55c20..152bcb2c 100644 --- a/libvips/include/vips/util.h +++ b/libvips/include/vips/util.h @@ -157,6 +157,11 @@ G_STMT_START { \ #define VIPS_CLIP_NONE( V, SEQ ) {} +/* Not all platforms have PATH_MAX (eg. Hurd) and we don't need a platform one + * anyway, just a static buffer big enough for almost any path. + */ +#define VIPS_PATH_MAX (4096) + const char *vips_enum_string( GType enm, int value ); const char *vips_enum_nick( GType enm, int value ); int vips_enum_from_nick( const char *domain, GType type, const char *str ); diff --git a/libvips/iofuncs/header.c b/libvips/iofuncs/header.c index f430bd53..ceeb4f93 100644 --- a/libvips/iofuncs/header.c +++ b/libvips/iofuncs/header.c @@ -1485,14 +1485,14 @@ int vips_image_history_printf( VipsImage *image, const char *fmt, ... ) { va_list args; - char line[4096]; + char line[VIPS_PATH_MAX]; time_t timebuf; /* Format command. -40, to leave 26 for the ctime, three for the # and * a bit. */ va_start( args, fmt ); - (void) vips_vsnprintf( line, 4096 - 40, fmt, args ); + (void) vips_vsnprintf( line, VIPS_PATH_MAX - 40, fmt, args ); va_end( args ); strcat( line, " # " ); diff --git a/libvips/iofuncs/init.c b/libvips/iofuncs/init.c index d241127e..7dbd9ded 100644 --- a/libvips/iofuncs/init.c +++ b/libvips/iofuncs/init.c @@ -507,8 +507,8 @@ vips_get_option_group( void ) static char * extract_prefix( const char *dir, const char *name ) { - char edir[4096]; - char vname[4096]; + char edir[VIPS_PATH_MAX]; + char vname[VIPS_PATH_MAX]; int i; #ifdef DEBUG @@ -522,12 +522,12 @@ extract_prefix( const char *dir, const char *name ) char *cwd; cwd = g_get_current_dir(); - vips_snprintf( edir, 4096, "%s" G_DIR_SEPARATOR_S "%s", - cwd, dir ); + vips_snprintf( edir, VIPS_PATH_MAX, + "%s" G_DIR_SEPARATOR_S "%s", cwd, dir ); g_free( cwd ); } else { - vips_strncpy( edir, dir, 4096 ); + vips_strncpy( edir, dir, VIPS_PATH_MAX ); } /* Chop off the trailing prog name, plus the trailing @@ -535,7 +535,7 @@ extract_prefix( const char *dir, const char *name ) */ if( !vips_ispostfix( edir, name ) ) return( NULL ); - vips_strncpy( vname, edir, 4096 ); + vips_strncpy( vname, edir, VIPS_PATH_MAX ); vname[strlen( edir ) - strlen( name ) - 1] = '\0'; /* Remove any "/./", any trailing "/.", any trailing "/". @@ -577,11 +577,11 @@ scan_path( char *path, const char *name ) for( p = path; (q = vips_break_token( p, G_SEARCHPATH_SEPARATOR_S )); p = q ) { - char str[4096]; + char str[VIPS_PATH_MAX]; /* Form complete path. */ - vips_snprintf( str, 4096, + vips_snprintf( str, VIPS_PATH_MAX, "%s" G_DIR_SEPARATOR_S "%s", p, name ); #ifdef DEBUG @@ -605,7 +605,7 @@ find_file( const char *name ) { const char *path = g_getenv( "PATH" ); char *prefix; - char full_path[4096]; + char full_path[VIPS_PATH_MAX]; if( !path ) return( NULL ); @@ -621,12 +621,12 @@ find_file( const char *name ) /* Windows always searches '.' first, so prepend cwd to path. */ dir = g_get_current_dir(); - vips_snprintf( full_path, 4096, "%s" G_SEARCHPATH_SEPARATOR_S "%s", - dir, path ); + vips_snprintf( full_path, VIPS_PATH_MAX, + "%s" G_SEARCHPATH_SEPARATOR_S "%s", dir, path ); g_free( dir ); } #else /*!OS_WIN32*/ - vips_strncpy( full_path, path, 4096 ); + vips_strncpy( full_path, path, VIPS_PATH_MAX ); #endif /*OS_WIN32*/ if( (prefix = scan_path( full_path, name )) ) @@ -674,12 +674,12 @@ guess_prefix( const char *argv0, const char *name ) * a full path in argv[0]. */ if( !g_path_is_absolute( argv0 ) ) { - char full_path[4096]; + char full_path[VIPS_PATH_MAX]; char *resolved; char *dir; dir = g_get_current_dir(); - vips_snprintf( full_path, 4096, + vips_snprintf( full_path, VIPS_PATH_MAX, "%s" G_DIR_SEPARATOR_S "%s", dir, argv0 ); g_free( dir ); @@ -726,7 +726,7 @@ vips_guess_prefix( const char *argv0, const char *env_name ) { const char *prefix; const char *p; - char name[4096]; + char name[VIPS_PATH_MAX]; /* Already set? */ @@ -747,10 +747,11 @@ vips_guess_prefix( const char *argv0, const char *env_name ) if( strlen( VIPS_EXEEXT ) > 0 ) { const char *olds[] = { VIPS_EXEEXT }; - vips__change_suffix( p, name, 4096, VIPS_EXEEXT, olds, 1 ); + vips__change_suffix( p, name, + VIPS_PATH_MAX, VIPS_EXEEXT, olds, 1 ); } else - vips_strncpy( name, p, 4096 ); + vips_strncpy( name, p, VIPS_PATH_MAX ); #ifdef DEBUG printf( "vips_guess_prefix: argv0 = %s\n", argv0 ); diff --git a/libvips/iofuncs/object.c b/libvips/iofuncs/object.c index 252080cc..bb93e76f 100644 --- a/libvips/iofuncs/object.c +++ b/libvips/iofuncs/object.c @@ -1959,34 +1959,36 @@ vips_object_set_args( VipsObject *object, const char *p ) VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object ); VipsToken token; - char string[4096]; - char string2[4096]; + char string[VIPS_PATH_MAX]; + char string2[VIPS_PATH_MAX]; GParamSpec *pspec; VipsArgumentClass *argument_class; VipsArgumentInstance *argument_instance; - if( !(p = vips__token_need( p, VIPS_TOKEN_LEFT, string, 4096 )) ) + if( !(p = vips__token_need( p, VIPS_TOKEN_LEFT, + string, VIPS_PATH_MAX )) ) return( -1 ); do { if( !(p = vips__token_need( p, VIPS_TOKEN_STRING, - string, 4096 )) ) + string, VIPS_PATH_MAX )) ) return( -1 ); /* We have to look for a '=', ')' or a ',' to see if string is * a param name or a value. */ - if( !(p = vips__token_must( p, &token, string2, 4096 )) ) + if( !(p = vips__token_must( p, &token, + string2, VIPS_PATH_MAX )) ) return( -1 ); if( token == VIPS_TOKEN_EQUALS ) { if( !(p = vips__token_need( p, VIPS_TOKEN_STRING, - string2, 4096 )) ) + string2, VIPS_PATH_MAX )) ) return( -1 ); if( vips_object_set_argument_from_string( object, string, string2 ) ) return( -1 ); if( !(p = vips__token_must( p, &token, - string2, 4096 )) ) + string2, VIPS_PATH_MAX )) ) return( -1 ); } else if( g_object_class_find_property( @@ -2022,7 +2024,7 @@ vips_object_set_args( VipsObject *object, const char *p ) } } while( token != VIPS_TOKEN_RIGHT ); - if( (p = vips__token_get( p, &token, string, 4096 )) ) { + if( (p = vips__token_get( p, &token, string, VIPS_PATH_MAX )) ) { vips_error( class->nickname, "%s", _( "extra tokens after ')'" ) ); return( -1 ); @@ -2035,7 +2037,7 @@ VipsObject * vips_object_new_from_string( VipsObjectClass *object_class, const char *p ) { const char *q; - char str[4096]; + char str[VIPS_PATH_MAX]; VipsObject *object; g_assert( object_class ); @@ -2045,9 +2047,9 @@ vips_object_new_from_string( VipsObjectClass *object_class, const char *p ) * everything before that as the principal arg for the constructor. */ if( (q = vips__find_rightmost_brackets( p )) ) - vips_strncpy( str, p, VIPS_MIN( 4096, q - p + 1 ) ); + vips_strncpy( str, p, VIPS_MIN( VIPS_PATH_MAX, q - p + 1 ) ); else - vips_strncpy( str, p, 4096 ); + vips_strncpy( str, p, VIPS_PATH_MAX ); if( !(object = object_class->new_from_string( str )) ) return( NULL ); diff --git a/libvips/iofuncs/system.c b/libvips/iofuncs/system.c index 2cb9da06..908de337 100644 --- a/libvips/iofuncs/system.c +++ b/libvips/iofuncs/system.c @@ -63,8 +63,6 @@ #include -#define MAX_STRSIZE (4096) - typedef struct _VipsSystem { VipsOperation parent_instance; @@ -111,8 +109,8 @@ vips_system_build( VipsObject *object ) VipsSystem *system = (VipsSystem *) object; FILE *fp; - char line[MAX_STRSIZE]; - char txt[MAX_STRSIZE]; + char line[VIPS_PATH_MAX]; + char txt[VIPS_PATH_MAX]; VipsBuf buf = VIPS_BUF_STATIC( txt ); int result; @@ -151,7 +149,7 @@ vips_system_build( VipsObject *object ) return( -1 ); } - while( fgets( line, MAX_STRSIZE, fp ) ) + while( fgets( line, VIPS_PATH_MAX, fp ) ) if( !vips_buf_appends( &buf, line ) ) break; diff --git a/libvips/iofuncs/util.c b/libvips/iofuncs/util.c index 733faf2a..2b608e54 100644 --- a/libvips/iofuncs/util.c +++ b/libvips/iofuncs/util.c @@ -1166,11 +1166,11 @@ FILE * vips_popenf( const char *fmt, const char *mode, ... ) { va_list args; - char buf[4096]; + char buf[VIPS_PATH_MAX]; FILE *fp; va_start( args, mode ); - (void) vips_vsnprintf( buf, 4096, fmt, args ); + (void) vips_vsnprintf( buf, VIPS_PATH_MAX, fmt, args ); va_end( args ); #ifdef DEBUG @@ -1387,14 +1387,15 @@ vips__find_rightmost_brackets( const char *p ) { const char *start[MAX_TOKENS]; VipsToken tokens[MAX_TOKENS]; - char str[4096]; + char str[VIPS_PATH_MAX]; int n, i; int nest; start[0] = p; for( n = 0; n < MAX_TOKENS && - (p = vips__token_get( start[n], &tokens[n], str, 4096 )); + (p = vips__token_get( start[n], &tokens[n], + str, VIPS_PATH_MAX )); n++, start[n] = p ) ;