Skip to content

Fix potential buffer underflow when stripping trailing newlines - #1951

Merged
AustinBenoit merged 4 commits into
mainfrom
fix/newline-strip-buffer-underflow
Oct 29, 2026
Merged

AustinBenoit merged 4 commits into
mainfrom
fix/newline-strip-buffer-underflow

Conversation

@AustinBenoit

Copy link
Copy Markdown
Contributor

Description

Provide details of the change, and generalize the change in the PR title above.

Resolves GitHub Code Scanning security alerts (WS-CPP-00049):

  • #2788 (app/src/locale.cc:235)
  • #2789 (app/src/locale.cc:236)
  • #2791 (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 using fgets into a fixed buffer and stripped trailing newlines by indexing buf[strlen(buf) - 1]. If fgets reads a line starting with a NUL byte (\0), strlen returns 0 and len - 1 wraps around to SIZE_MAX (buf[-1]), resulting in a 1-byte buffer underflow. In testing/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:

  • Replaces raw C-array newline stripping (buf[strlen(buf) - 1] = '\0') in app/src/locale.cc and testing/run_all_tests.cc with std::string (if (!str.empty() && str.back() == '\n') { str.pop_back(); }).
  • Zero-initializes the stack buffers (char buf[kBufSize] = {0}; and char log[256] = {0};).
  • Closes tz_file (fclose(tz_file)) in GetTimezone() to fix a FILE* leak on Linux, and falls back to getenv("TZ") / tzname if /etc/timezone is blank after stripping the newline.
  • Replaces while (!feof(fp)) { if (fgets(...)) } in testing/run_all_tests.cc with while (fgets(log, sizeof(log), fp) != nullptr) to avoid an infinite loop if ferror(fp) is set.
  • Adds a release note entry under ### Upcoming in release_build_files/readme.md.

Testing

Describe how you've tested these changes. Link any manually triggered Integration tests or CPP binary SDK Packaging Github Action workflows, if applicable.

  • Compiled app/src/locale.cc on Linux desktop with g++ -std=c++17 -Wall -Wextra -Werror.
  • Verified formatting with python3 scripts/format_code.py -git_diff -noformat_file -verbose.
  • Verified copyright headers with bash scripts/gha/check_copyright.sh -g.

Type of Change

Place an x the applicable box:

  • Bug fix. Add the issue # below if applicable.
  • New feature. A non-breaking change which adds functionality.
  • Other, such as a build process or documentation change.

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

  • Bug fixes and feature changes require an update to the Release Notes section of release_build_files/readme.md.
  • Read the contribution guidelines CONTRIBUTING.md.
  • Changes to the public API require an internal API review. If you'd like to help us make Firebase APIs better, please propose your change in a feature request so that we can discuss it together.

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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread app/src/locale.cc Resolved
Comment thread testing/run_all_tests.cc Resolved
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.
@AustinBenoit
AustinBenoit enabled auto-merge (squash) October 29, 2026 17:49
@AustinBenoit
AustinBenoit merged commit 2d11ea6 into main Oct 29, 2026
39 checks passed
@github-actions github-actions Bot added the tests: in-progress This PR's integration tests are in progress. label Oct 29, 2026
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

❌  Integration test FAILED

Requested by @AustinBenoit on commit 2d11ea6
Last updated: Thu Oct 29 13:50 PDT 2026
View integration test log & download artifacts

Failures Configs
app_check [TEST] [FAILURE] [tvOS] [macos] [tvos_simulator]
(1 failed tests)  FirebaseAppCheckTest.TestGetTokenForcingRefresh
firestore [TEST] [FLAKINESS] [Android] [1/3 os: ubuntu] [1/2 android_device: android_target]
(1 failed tests)  CRASH/TIMEOUT

Add flaky tests to go/fpl-cpp-flake-tracker

@github-actions github-actions Bot added the tests: failed This PR's integration tests failed. label Oct 29, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests: failed This PR's integration tests failed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants