Skip to content

Commit

Permalink
Change GoogleTest flag removal to not read beyond the end of the array
Browse files Browse the repository at this point in the history
to the NULL terminator.  #4532 says ASAN complains about this on some
platforms, although it is not clear if ASAN or the platform implementation
is incorrect about accessing the terminating NULL.

Fixes #4532

PiperOrigin-RevId: 635886009
Change-Id: Ibb4237055488c895b1dd09145ab979347bb9a390
  • Loading branch information
derekmauro authored and Copybara-Service committed May 21, 2024
1 parent c8393f8 commit 9b4993c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions googletest/src/gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6700,17 +6700,17 @@ void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) {
}

if (remove_flag) {
// Shift the remainder of the argv list left by one. Note
// that argv has (*argc + 1) elements, the last one always being
// NULL. The following loop moves the trailing NULL element as
// well.
for (int j = i; j != *argc; j++) {
argv[j] = argv[j + 1];
// Shift the remainder of the argv list left by one.
for (int j = i + 1; j < *argc; ++j) {
argv[j - 1] = argv[j];
}

// Decrements the argument count.
(*argc)--;

// Terminate the array with nullptr.
argv[*argc] = nullptr;

// We also need to decrement the iterator as we just removed
// an element.
i--;
Expand Down

0 comments on commit 9b4993c

Please sign in to comment.