Skip to content

Commit

Permalink
dev1-fix1: exception free float parsing, with error logging and prope…
Browse files Browse the repository at this point in the history
…rly passing nRead from GetPrivateProfileStringA to std::string constructor
  • Loading branch information
kirides committed Dec 22, 2021
1 parent 1c9ceb4 commit fee3ba1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions D3D11Engine/GothicAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion D3D11Engine/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fee3ba1

Please sign in to comment.