From bbdd3da43d37ea4fdbcc2dc3ad8b4800020e71e8 Mon Sep 17 00:00:00 2001 From: Norbert Thiebaud Date: Sat, 1 Nov 2014 09:52:30 -0500 Subject: crashrep: get rid of tmpnam() Change-Id: If84d623719058e4b14f224a433253fac4fd47f85 Reviewed-on: https://gerrit.libreoffice.org/12066 Reviewed-by: Michael Stahl Tested-by: Michael Stahl --- crashrep/source/unx/main.cxx | 258 ++++++++++++++++++++++++++----------------- 1 file changed, 154 insertions(+), 104 deletions(-) (limited to 'crashrep') diff --git a/crashrep/source/unx/main.cxx b/crashrep/source/unx/main.cxx index 65d70e2cdf22..d70bb0e293e0 100644 --- a/crashrep/source/unx/main.cxx +++ b/crashrep/source/unx/main.cxx @@ -71,9 +71,7 @@ static string g_strPStackFileName; static string g_strChecksumFileName; static string g_strProgramDir; -static char g_szStackFile[L_tmpnam] = ""; -static char g_szDescriptionFile[2048] = ""; -static char g_szReportFile[2048] = ""; +//static char g_szStackFile[L_tmpnam] = ""; #define REPORT_SERVER (g_strReportServer.c_str()) #define REPORT_PORT g_uReportPort @@ -163,87 +161,124 @@ static size_t fcopy( FILE *fpout, FILE *fpin ) from which it can be reviewed and sent */ -bool write_report( const boost::unordered_map< string, string >& rSettings ) +char* write_report( const boost::unordered_map< string, string >& rSettings ) { - FILE *fp = fopen( tmpnam( g_szReportFile ), "w" ); - const char *pszUserType = getenv( "STAROFFICE_USERTYPE" ); + char * report_filename; - fprintf( fp, - "\n" - "\n" - "\n" - "\n" - "%s\n" - "\n" - "\n" - "\n" - "\n" - , - pszUserType ? xml_encode( pszUserType ).c_str() : "", - xml_encode(rSettings.find( "CONTACT" )->second).c_str(), - xml_encode(rSettings.find( "EMAIL" )->second).c_str(), - xml_encode(rSettings.find( "TITLE" )->second).c_str(), - g_buildid.length() ? xml_encode( g_buildid ).c_str() : "unknown", - _INPATH, - g_strDefaultLanguage.c_str(), - g_signal, - g_strProductKey.length() ? xml_encode(g_strProductKey).c_str() : "unknown", - xml_encode(getprogramdir()).c_str() - ); - - struct utsname info; - - memset( &info, 0, sizeof(info) ); - uname( &info ); + report_filename = (char*)calloc(1, 2048); - fprintf( fp, - "\n" - "\n" - , - xml_encode( info.sysname ).c_str(), - xml_encode( info.version ).c_str(), - xml_encode( info.release ).c_str(), - xml_encode( getlocale() ).c_str() - ); - fprintf( fp, "\n", xml_encode( info.machine ).c_str() ); - fprintf( fp, "\n" ); - - FILE *fpxml = fopen( g_strXMLFileName.c_str(), "r" ); - if ( fpxml ) + if(!report_filename) { - fcopy( fp, fpxml ); - fclose( fpxml ); + return NULL; } + strncpy( report_filename, P_tmpdir, 2047 ); + strncat( report_filename, "/crashreport.XXXXXX", 2047 - strlen(report_filename)); - FILE *fpchk = fopen( g_strChecksumFileName.c_str(), "r" ); - if ( fpchk ) + int fd = mkstemp(report_filename); + if(fd == -1) { - fcopy( fp, fpchk ); - fclose( fpchk ); + free(report_filename); + return NULL; } + else + { + FILE* fp = fdopen(fd, "w"); + + const char* pszUserType = getenv( "STAROFFICE_USERTYPE" ); + + fprintf( fp, + "\n" + "\n" + "\n" + "\n" + "%s\n" + "\n" + "\n" + "\n" + "\n" + , + pszUserType ? xml_encode( pszUserType ).c_str() : "", + xml_encode(rSettings.find( "CONTACT" )->second).c_str(), + xml_encode(rSettings.find( "EMAIL" )->second).c_str(), + xml_encode(rSettings.find( "TITLE" )->second).c_str(), + g_buildid.length() ? xml_encode( g_buildid ).c_str() : "unknown", + _INPATH, + g_strDefaultLanguage.c_str(), + g_signal, + g_strProductKey.length() ? xml_encode(g_strProductKey).c_str() : "unknown", + xml_encode(getprogramdir()).c_str() + ); + + struct utsname info; - fprintf( fp, "\n" ); + memset( &info, 0, sizeof(info) ); + uname( &info ); - fclose( fp ); + fprintf( fp, + "\n" + "\n" + , + xml_encode( info.sysname ).c_str(), + xml_encode( info.version ).c_str(), + xml_encode( info.release ).c_str(), + xml_encode( getlocale() ).c_str() + ); + fprintf( fp, "\n", xml_encode( info.machine ).c_str() ); + fprintf( fp, "\n" ); + + FILE *fpxml = fopen( g_strXMLFileName.c_str(), "r" ); + if ( fpxml ) + { + fcopy( fp, fpxml ); + fclose( fpxml ); + } - return true; + FILE *fpchk = fopen( g_strChecksumFileName.c_str(), "r" ); + if ( fpchk ) + { + fcopy( fp, fpchk ); + fclose( fpchk ); + } + + fprintf( fp, "\n" ); + + fclose( fp ); + } + return report_filename; } -bool write_description( const boost::unordered_map< string, string >& rSettings ) +char* write_description( const boost::unordered_map< string, string >& rSettings ) { - bool bSuccess = false; - FILE *fp = fopen( tmpnam( g_szDescriptionFile ), "w" ); + char * description_filename; - if ( fp ) + description_filename = (char*)calloc(1, 2048); + + if(!description_filename) { - bSuccess = true; - fprintf( fp, "\xEF\xBB\xBF" ); - fprintf( fp, "%s\n", rSettings.find( "DESCRIPTION" )->second.c_str() ); - fclose( fp ); + return NULL; } + strncpy( description_filename, P_tmpdir, 2047 ); + strncat( description_filename, "/crashreport.XXXXXX", 2047 - strlen(description_filename)); - return bSuccess; + int fd = mkstemp(description_filename); + if(fd == -1) + { + free(description_filename); + return NULL; + } + else + { + FILE* fp = fdopen(fd, "w"); + + if ( fp ) + { + fprintf( fp, "\xEF\xBB\xBF" ); + fprintf( fp, "%s\n", rSettings.find( "DESCRIPTION" )->second.c_str() ); + fclose( fp ); + } + } + return description_filename; } #if 0 @@ -372,7 +407,7 @@ bool SendHTTPRequest( return success; } -static void WriteSOAPRequest( FILE *fp ) +static void WriteSOAPRequest( FILE *fp, char* report_filename, char* description_filename ) { fprintf( fp, "\n" @@ -390,7 +425,7 @@ static void WriteSOAPRequest( FILE *fp ) fprintf( fp, "This is an autogenerated crash report mail.\n" ); fprintf( fp, "\n" ); - FILE *fpin = fopen( g_szReportFile, "r" ); + FILE *fpin = fopen( report_filename, "r" ); if ( fpin ) { fprintf( fp, @@ -402,7 +437,7 @@ static void WriteSOAPRequest( FILE *fp ) fclose( fpin ); } - fpin = fopen( g_szDescriptionFile, "r" ); + fpin = fopen( description_filename, "r" ); if ( fpin ) { fprintf( fp, @@ -414,6 +449,10 @@ static void WriteSOAPRequest( FILE *fp ) fclose( fpin ); } +// nowhere g_szStackfile actualy populated with something +// so this endup trying to open "" which is ging to fail +// so what's the point ? +#if 0 fpin = fopen( g_szStackFile, "r" ); if ( fpin ) { @@ -425,7 +464,7 @@ static void WriteSOAPRequest( FILE *fp ) fprintf( fp, "]]>\n" ); fclose( fpin ); } - +#endif fprintf( fp, "\n" "\n" @@ -458,30 +497,38 @@ bool send_crash_report( const boost::unordered_map< string, string >& rSettings bool bUseProxy = !strcasecmp( "true", rSettings.find( "USEPROXY" )->second.c_str() ); - write_description( rSettings ); - write_report( rSettings ); - + char* description_filename = write_description( rSettings ); + char* report_filename = write_report( rSettings ); bool bSuccess = false; - FILE *fptemp = tmpfile(); - if ( fptemp ) + if(description_filename && report_filename) { - WriteSOAPRequest( fptemp ); - fseek( fptemp, 0, SEEK_SET ); - - bSuccess = SendHTTPRequest( - fptemp, - REPORT_SERVER, REPORT_PORT, - bUseProxy ? pProxyServer : NULL, - uProxyPort ? uProxyPort : 8080 - ); - - fclose( fptemp ); + FILE* fptemp = tmpfile(); + if ( fptemp ) + { + WriteSOAPRequest( fptemp, report_filename, description_filename ); + fseek( fptemp, 0, SEEK_SET ); + + bSuccess = SendHTTPRequest( + fptemp, + REPORT_SERVER, REPORT_PORT, + bUseProxy ? pProxyServer : NULL, + uProxyPort ? uProxyPort : 8080 + ); + fclose( fptemp ); + } + } + if(description_filename) + { + unlink( description_filename); + free( description_filename); + } + if(report_filename) + { + unlink( report_filename); + free( report_filename); } - - unlink( g_szDescriptionFile ); - unlink( g_szReportFile ); return bSuccess; } @@ -980,25 +1027,28 @@ int main( int argc, char** argv ) read_settings_from_environment( aDialogSettings ); write_crash_data(); - write_report( aDialogSettings ); - - string sPreviewFile = get_home_dir(); - sPreviewFile += "/"; - sPreviewFile += string(PRVFILE); - - FILE *fpout = fopen( sPreviewFile.c_str(), "w+" ); - if ( fpout ) + char* report_filename = write_report( aDialogSettings ); + if(report_filename) { - FILE *fpin = fopen( g_szReportFile, "r" ); - if ( fpin ) + string sPreviewFile = get_home_dir(); + sPreviewFile += "/"; + sPreviewFile += string(PRVFILE); + + FILE *fpout = fopen( sPreviewFile.c_str(), "w+" ); + if ( fpout ) { - fcopy( fpout, fpin ); - fclose( fpin ); + FILE *fpin = fopen( report_filename, "r" ); + if ( fpin ) + { + fcopy( fpout, fpin ); + fclose( fpin ); + } + fclose( fpout ); } - fclose( fpout ); - } - unlink( g_szReportFile ); + unlink( report_filename ); + free(report_filename); + } } if ( g_bLoadReport ) @@ -1007,7 +1057,7 @@ int main( int argc, char** argv ) unlink( g_strChecksumFileName.c_str() ); } - unlink( g_szStackFile ); +// unlink( g_szStackFile ); return 0; } -- cgit