graphcycles_test: Avoid applying a non-zero offset to a null pointer

Applying a non-zero offset to a null pointer is undefined behavior.

Our UBSAN tests were missing `-fno-sanitize-recover`, which
means UBSAN logs a warning, but the program continues,
causing the test not to fail.

`-fno-sanitize-recover` will be added once all errors are fixed.

PiperOrigin-RevId: 733395060
Change-Id: Ibf71d0d2a27fac14f0c33dbdf83f4089645b8b37
This commit is contained in:
Derek Mauro
2025-03-04 11:07:48 -08:00
committed by Copybara-Service
parent 38b61bf51f
commit 0856410fc6

View File

@@ -15,6 +15,7 @@
#include "absl/synchronization/internal/graphcycles.h"
#include <climits>
#include <cstdint>
#include <map>
#include <random>
#include <unordered_set>
@@ -461,20 +462,20 @@ TEST_F(GraphCyclesTest, ManyEdges) {
TEST(GraphCycles, IntegerOverflow) {
GraphCycles graph_cycles;
char *buf = (char *)nullptr;
GraphId prev_id = graph_cycles.GetId(buf);
uintptr_t buf = 0;
GraphId prev_id = graph_cycles.GetId(reinterpret_cast<void*>(buf));
buf += 1;
GraphId id = graph_cycles.GetId(buf);
GraphId id = graph_cycles.GetId(reinterpret_cast<void*>(buf));
ASSERT_TRUE(graph_cycles.InsertEdge(prev_id, id));
// INT_MAX / 40 is enough to cause an overflow when multiplied by 41.
graph_cycles.TestOnlyAddNodes(INT_MAX / 40);
buf += 1;
GraphId newid = graph_cycles.GetId(buf);
GraphId newid = graph_cycles.GetId(reinterpret_cast<void*>(buf));
graph_cycles.HasEdge(prev_id, newid);
graph_cycles.RemoveNode(buf);
graph_cycles.RemoveNode(reinterpret_cast<void*>(buf));
}
} // namespace synchronization_internal