From 9f1a5a1ebb4e0c2b745e488182b27eae7d7efc47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20=C4=8Cerm=C3=A1k?= Date: Fri, 24 Aug 2018 09:41:04 +0200 Subject: [PATCH] [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. --- include/exiv2/futils.hpp | 6 +++--- src/futils.cpp | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/exiv2/futils.hpp b/include/exiv2/futils.hpp index 4d7963ef..3117fa81 100644 --- a/include/exiv2/futils.hpp +++ b/include/exiv2/futils.hpp @@ -59,11 +59,11 @@ namespace Exiv2 // free functions /*! @brief Return the value of environmental variable. - @param var The name of environmental variable. + @param[in] var The name of environmental variable. Must be a member of the enumeration @ref EnVar. @return the value of environmental variable. If it's empty, the default value is returned. @throws std::out_of_range when an unexpected EnVar is given as input. */ - EXIV2API std::string getEnv(EnVar var); + EXIV2API std::string getEnv(int env_var); /*! @brief Encode the input url. @@ -204,4 +204,4 @@ namespace Exiv2 } // namespace Exiv2 -#endif // #ifndef FUTILS_HPP_ \ No newline at end of file +#endif // #ifndef FUTILS_HPP_ diff --git a/src/futils.cpp b/src/futils.cpp index f2695132..d0d2c067 100644 --- a/src/futils.cpp +++ b/src/futils.cpp @@ -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.