From 5f20c89215e13a6d2cfc2b5ddd2a15469f7c9621 Mon Sep 17 00:00:00 2001 From: suzuki toshiya Date: Sun, 13 Oct 2024 10:20:42 +0900 Subject: [PATCH] apinames: Fix a buffer overrun for VMS platform. Some output formats may rewrite symbol names during the output, like the concatenation of "64__" suffix on VMS. To estimate sufficient size to store symbol name, pass the output format info to `names_add`. For VMS, `names_add` allocates longer buffer to append "64__". * apinames.c (SUFFIX_VMS_64ADDR): New macro of "64__". (main): Pass the format info to `read_header_file`. (read_header_file): Pass the format info to `names_add`. (names_add): Receive the format info, and reserve the symbol name buffer 4 byte longer in the case of VMS, to append the suffix in `names_dump`. --- src/tools/apinames.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/tools/apinames.c b/src/tools/apinames.c index 343428bac..f64541ebb 100644 --- a/src/tools/apinames.c +++ b/src/tools/apinames.c @@ -42,6 +42,7 @@ typedef enum OutputFormat_ } OutputFormat; +#define SUFFIX_VMS_64ADDR "64__" static void panic( const char* fmt, @@ -76,11 +77,12 @@ static int max_names; static void -names_add( const char* name, - const char* end ) +names_add( const char* name, + const char* end, + OutputFormat format ) { unsigned int h; - int nn, len; + int nn, len, len_suffix; Name nm; @@ -116,8 +118,18 @@ names_add( const char* name, } nm = &the_names[num_names++]; + switch ( format ) + { + case OUTPUT_VMS_OPT: + /* VMS mode would join the symbol name with a suffix */ + len_suffix = sizeof ( SUFFIX_VMS_64ADDR ); + break; + default: + len_suffix = 0; + } + nm->hash = h; - nm->name = (char*)malloc( len + 1 ); + nm->name = (char*)malloc( len + len_suffix + 1 ); if ( !nm->name ) panic( "not enough memory" ); @@ -229,7 +241,7 @@ names_dump( FILE* out, /* Also emit a 64-bit symbol, as created by the `vms_auto64` tool. */ /* It has the string '64__' appended to its name. */ - strcat( the_names[nn].name , "64__" ); + strcat( the_names[nn].name , SUFFIX_VMS_64ADDR ); if ( vms_shorten_symbol( the_names[nn].name, short_symbol, 1 ) == -1 ) panic( "could not shorten name '%s'", the_names[nn].name ); fprintf( out, "symbol_vector = ( %s = PROCEDURE)\n", short_symbol ); @@ -277,8 +289,9 @@ typedef enum State_ static int -read_header_file( FILE* file, - int verbose ) +read_header_file( FILE* file, + int verbose, + OutputFormat format ) { static char buff[LINEBUFF_SIZE + 1]; State state = STATE_START; @@ -350,7 +363,7 @@ read_header_file( FILE* file, if ( verbose ) fprintf( stderr, ">>> %.*s\n", (int)( p - name ), name ); - names_add( name, p ); + names_add( name, p, format ); } state = STATE_START; @@ -519,7 +532,7 @@ main( int argc, } /* end of while loop */ if ( from_stdin ) - read_header_file( stdin, verbose ); + read_header_file( stdin, verbose, format ); else { for ( --argc, argv++; argc > 0; argc--, argv++ ) @@ -534,7 +547,7 @@ main( int argc, if ( verbose ) fprintf( stderr, "opening '%s'\n", argv[0] ); - read_header_file( file, verbose ); + read_header_file( file, verbose, format ); fclose( file ); } }