Skip to content

Commit 12a2f37

Browse files
util: Avoid potential uninitialized read in FormatISO8601DateTime(int64_t nTime) by checking gmtime_s/gmtime_r return value
1 parent eddcbfb commit 12a2f37

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

ci/test/00_setup_env_native_fuzz_with_valgrind.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export NO_DEPENDS=1
1212
export RUN_UNIT_TESTS=false
1313
export RUN_FUNCTIONAL_TESTS=false
1414
export RUN_FUZZ_TESTS=true
15-
export FUZZ_TESTS_CONFIG="--exclude integer,parse_iso8601 --valgrind"
15+
export FUZZ_TESTS_CONFIG="--valgrind"
1616
export GOAL="install"
1717
export BITCOIN_CONFIG="--enable-fuzz --with-sanitizers=fuzzer CC=clang-8 CXX=clang++-8"
1818
# Use clang-8, instead of default clang on bionic, which is clang-6 and does not come with libfuzzer on aarch64

src/util/time.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,25 @@ std::string FormatISO8601DateTime(int64_t nTime) {
9494
struct tm ts;
9595
time_t time_val = nTime;
9696
#ifdef _MSC_VER
97-
gmtime_s(&ts, &time_val);
97+
if (gmtime_s(&ts, &time_val) != 0) {
9898
#else
99-
gmtime_r(&time_val, &ts);
99+
if (gmtime_r(&time_val, &ts) == nullptr) {
100100
#endif
101+
return {};
102+
}
101103
return strprintf("%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec);
102104
}
103105

104106
std::string FormatISO8601Date(int64_t nTime) {
105107
struct tm ts;
106108
time_t time_val = nTime;
107109
#ifdef _MSC_VER
108-
gmtime_s(&ts, &time_val);
110+
if (gmtime_s(&ts, &time_val) != 0) {
109111
#else
110-
gmtime_r(&time_val, &ts);
112+
if (gmtime_r(&time_val, &ts) == nullptr) {
111113
#endif
114+
return {};
115+
}
112116
return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
113117
}
114118

@@ -124,4 +128,4 @@ int64_t ParseISO8601DateTime(const std::string& str)
124128
if (ptime.is_not_a_date_time() || epoch > ptime)
125129
return 0;
126130
return (ptime - epoch).total_seconds();
127-
}
131+
}

0 commit comments

Comments
 (0)