Repository navigation
Fix potential buffer underflow when stripping trailing newlines - #1951
Merged
Merged
Conversation
Use std::string::back() and std::string::pop_back() (with a non-empty check) instead of indexing buf[strlen(buf) - 1] in app/src/locale.cc and testing/run_all_tests.cc, and close /etc/timezone in GetTimezone(). Resolves GitHub Code Scanning security alerts (Rule WS-CPP-00049): - https://github.com/firebase/firebase-cpp-sdk/security/code-scanning/2788 - https://github.com/firebase/firebase-cpp-sdk/security/code-scanning/2789 - https://github.com/firebase/firebase-cpp-sdk/security/code-scanning/2791
Contributor
There was a problem hiding this comment.
Code Review
This pull request fixes a potential 1-byte buffer underflow and a file descriptor leak when reading /etc/timezone in GetTimezone(), and refactors the test log printing loop to safely handle newlines. The review feedback suggests handling CRLF line endings when parsing /etc/timezone by stripping both \r and \n characters, and optimizing the test logging loop by manipulating the raw C-string buffer directly to avoid the overhead of creating a std::string on each iteration.
Replaces the C-style fgets + manual newline stripping in GetTimezone() and the Android test runner with std::getline, which has no fixed-size buffer, no index arithmetic, and closes the file via RAII. GetTimezone() now also trims a trailing '\r' from CRLF line endings.
a-maurice
approved these changes
Oct 29, 2026
AustinBenoit
enabled auto-merge (squash)
October 29, 2026 17:49
❌ Integration test FAILEDRequested by @AustinBenoit on commit 2d11ea6
Add flaky tests to go/fpl-cpp-flake-tracker |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Resolves GitHub Code Scanning security alerts (
WS-CPP-00049):app/src/locale.cc:235)app/src/locale.cc:236)testing/run_all_tests.cc:97)Both
firebase::internal::GetTimezone()(app/src/locale.cc) and the Android native test runner (testing/run_all_tests.cc) read lines usingfgetsinto a fixed buffer and stripped trailing newlines by indexingbuf[strlen(buf) - 1]. Iffgetsreads a line starting with a NUL byte (\0),strlenreturns0andlen - 1wraps around toSIZE_MAX(buf[-1]), resulting in a 1-byte buffer underflow. Intesting/run_all_tests.cc,log[strlen(log) - 1] = '\0'also truncated the 255th character of any line longer than the buffer that did not end with\n.This change:
buf[strlen(buf) - 1] = '\0') inapp/src/locale.ccandtesting/run_all_tests.ccwithstd::string(if (!str.empty() && str.back() == '\n') { str.pop_back(); }).char buf[kBufSize] = {0};andchar log[256] = {0};).tz_file(fclose(tz_file)) inGetTimezone()to fix aFILE*leak on Linux, and falls back togetenv("TZ")/tznameif/etc/timezoneis blank after stripping the newline.while (!feof(fp)) { if (fgets(...)) }intesting/run_all_tests.ccwithwhile (fgets(log, sizeof(log), fp) != nullptr)to avoid an infinite loop ifferror(fp)is set.### Upcominginrelease_build_files/readme.md.Testing
app/src/locale.ccon Linux desktop withg++ -std=c++17 -Wall -Wextra -Werror.python3 scripts/format_code.py -git_diff -noformat_file -verbose.bash scripts/gha/check_copyright.sh -g.Type of Change
Place an
xthe applicable box:https://github.com/firebase/firebase-cpp-sdk/security/code-scanning/2788
https://github.com/firebase/firebase-cpp-sdk/security/code-scanning/2789
https://github.com/firebase/firebase-cpp-sdk/security/code-scanning/2791
Notes
Release Notessection ofrelease_build_files/readme.md.