diff --git a/app/src/locale.cc b/app/src/locale.cc index d19d598db2..bdb85f5dcb 100644 --- a/app/src/locale.cc +++ b/app/src/locale.cc @@ -33,10 +33,9 @@ #define UCHAR_TYPE wchar_t #include #elif FIREBASE_PLATFORM_LINUX -#include - #include #include +#include #else #error "Unknown platform." #endif // platform selector @@ -225,17 +224,13 @@ std::string GetTimezone() { #elif FIREBASE_PLATFORM_LINUX // Ubuntu: Check /etc/timezone for the full time zone name. - FILE* tz_file = fopen("/etc/timezone", "r"); - if (tz_file) { - const size_t kBufSize = 128; - char buf[kBufSize]; - if (fgets(buf, kBufSize, tz_file)) { - // Remove a trailing '\n', if any. - size_t len = strlen(buf); - if (buf[len - 1] == '\n') { - buf[len - 1] = '\0'; - } - return std::string(buf); + std::ifstream tz_file("/etc/timezone"); + std::string tz; + if (std::getline(tz_file, tz)) { + // Trim trailing whitespace, including the '\r' of a CRLF line ending. + tz.erase(tz.find_last_not_of(" \t\r\n") + 1); + if (!tz.empty()) { + return tz; } } // If TZ environment variable is defined and not empty, use it, else use diff --git a/release_build_files/readme.md b/release_build_files/readme.md index 3c31886ec7..dceff0c6aa 100644 --- a/release_build_files/readme.md +++ b/release_build_files/readme.md @@ -615,6 +615,8 @@ code. ## Release Notes ### Upcoming - Changes + - General (Linux Desktop): Fixed a potential 1-byte buffer underflow and file + descriptor leak when reading `/etc/timezone` in `GetTimezone()`. - Database, Functions, Storage (Android): Fixed a crash in `GetInstance()` when initialization fails. It now returns `nullptr` and reports `kInitResultFailedMissingDependency`. diff --git a/testing/run_all_tests.cc b/testing/run_all_tests.cc index 284f83ea47..dd935b3312 100644 --- a/testing/run_all_tests.cc +++ b/testing/run_all_tests.cc @@ -21,6 +21,9 @@ #include #include +#include +#include + #include "base/logging.h" #include "gtest/gtest.h" @@ -90,13 +93,11 @@ extern "C" JNIEXPORT jint JNICALL JNI_FUNCTION_NAME #ifdef OUTPUT_TO_INFO if (fp != nullptr) { LOG(INFO) << "Native test logs:"; - fseek(fp, 0, SEEK_SET); - char log[256]; - while (!feof(fp)) { - if (fgets(log, 256, fp)) { - log[strlen(log) - 1] = '\0'; // Remove newline. - LOG(INFO) << log; - } + fflush(fp); + std::ifstream log_file(log_path); + std::string line; + while (std::getline(log_file, line)) { + LOG(INFO) << line; } fclose(fp); } else {