[futils] Change signature of getEnv to take an int

While taking an EnVar as the parameter is more clear it has the
disadvantage, that passing anything outside of the range of the
enumeration is undefined behavior. The compiler could then optimize
the range check in getEnv away (perfectly legal due to UB), leading
to buffer overreads.
This commit is contained in:
Dan Čermák
2018-08-24 09:41:04 +02:00
parent e705f1ef5b
commit 9f1a5a1ebb
2 changed files with 7 additions and 6 deletions
+4 -3
View File
@@ -60,12 +60,13 @@ namespace Exiv2 {
const char* ENVARKEY[] = {"EXIV2_HTTP_POST", "EXIV2_TIMEOUT"}; //!< @brief request keys for http exiv2 handler and time-out
// *****************************************************************************
// free functions
std::string getEnv(EnVar var)
std::string getEnv(int env_var)
{
if (var < envHTTPPOST || var > envTIMEOUT) {
// this check is relying on undefined behavior and might not be effective
if (env_var < envHTTPPOST || env_var > envTIMEOUT) {
throw std::out_of_range("Unexpected env variable");
}
return getenv(ENVARKEY[var]) ? getenv(ENVARKEY[var]) : ENVARDEF[var];
return getenv(ENVARKEY[env_var]) ? getenv(ENVARKEY[env_var]) : ENVARDEF[env_var];
}
/// @brief Convert an integer value to its hex character.