From fee3ba1424f5776c6a6a5199b4a60ccb4f938367 Mon Sep 17 00:00:00 2001 From: Kirides Date: Wed, 22 Dec 2021 12:32:37 +0100 Subject: [PATCH] dev1-fix1: exception free float parsing, with error logging and properly passing nRead from GetPrivateProfileStringA to std::string constructor --- D3D11Engine/GothicAPI.cpp | 26 ++++++++++++++++++++++++-- D3D11Engine/pch.h | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/D3D11Engine/GothicAPI.cpp b/D3D11Engine/GothicAPI.cpp index 2c60b90e..f78fa650 100644 --- a/D3D11Engine/GothicAPI.cpp +++ b/D3D11Engine/GothicAPI.cpp @@ -136,6 +136,24 @@ GothicAPI::~GothicAPI() { SAFE_DELETE( WrappedWorldMesh ); } +int stof_safe( const std::string& str, float& p_value, std::size_t* pos = 0 ) { + // wrapping std::stof because it may throw an exception + + try { + p_value = std::stof( str, pos ); + return 0; + } catch ( const std::invalid_argument& ia ) { + LogWarn() << "parsing float failed with invalid argument: " << ia.what(); + return -1; + } catch ( const std::out_of_range& oor ) { + LogWarn() << "parsing float failed with out of range: " << oor.what(); + return -2; + } catch ( const std::exception& e ) { + LogWarn() << "parsing float failed with unknown error: " << e.what(); + return -3; + } +} + float GetPrivateProfileFloatA( const LPCSTR lpAppName, const LPCSTR lpKeyName, @@ -144,8 +162,12 @@ float GetPrivateProfileFloatA( ) { const int float_str_max = 30; TCHAR nFloat[float_str_max]; - if ( GetPrivateProfileStringA( lpAppName, lpKeyName, nullptr, nFloat, float_str_max, lpFileName.c_str() ) ) { - return std::stof( std::string( nFloat ) ); + auto nRead = GetPrivateProfileStringA( lpAppName, lpKeyName, nullptr, nFloat, float_str_max, lpFileName.c_str() ); + if ( nRead ) { + float value; + if ( 0 == stof_safe( std::string( nFloat, nRead ), value )) { + return value; + } } return nDefault; } diff --git a/D3D11Engine/pch.h b/D3D11Engine/pch.h index 51a1d5cf..bacf9805 100644 --- a/D3D11Engine/pch.h +++ b/D3D11Engine/pch.h @@ -27,7 +27,7 @@ #define stdext std #endif -#define VERSION_NUMBER "17.8-dev1" +#define VERSION_NUMBER "17.8-dev1-fix1" __declspec(selectany) const char* VERSION_NUMBER_STR = VERSION_NUMBER; extern bool FeatureLevel10Compatibility;