Skip to content

Commit 0856410

Browse files
derekmaurocopybara-github
authored andcommitted
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
1 parent 38b61bf commit 0856410

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

absl/synchronization/internal/graphcycles_test.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "absl/synchronization/internal/graphcycles.h"
1616

1717
#include <climits>
18+
#include <cstdint>
1819
#include <map>
1920
#include <random>
2021
#include <unordered_set>
@@ -461,20 +462,20 @@ TEST_F(GraphCyclesTest, ManyEdges) {
461462

462463
TEST(GraphCycles, IntegerOverflow) {
463464
GraphCycles graph_cycles;
464-
char *buf = (char *)nullptr;
465-
GraphId prev_id = graph_cycles.GetId(buf);
465+
uintptr_t buf = 0;
466+
GraphId prev_id = graph_cycles.GetId(reinterpret_cast<void*>(buf));
466467
buf += 1;
467-
GraphId id = graph_cycles.GetId(buf);
468+
GraphId id = graph_cycles.GetId(reinterpret_cast<void*>(buf));
468469
ASSERT_TRUE(graph_cycles.InsertEdge(prev_id, id));
469470

470471
// INT_MAX / 40 is enough to cause an overflow when multiplied by 41.
471472
graph_cycles.TestOnlyAddNodes(INT_MAX / 40);
472473

473474
buf += 1;
474-
GraphId newid = graph_cycles.GetId(buf);
475+
GraphId newid = graph_cycles.GetId(reinterpret_cast<void*>(buf));
475476
graph_cycles.HasEdge(prev_id, newid);
476477

477-
graph_cycles.RemoveNode(buf);
478+
graph_cycles.RemoveNode(reinterpret_cast<void*>(buf));
478479
}
479480

480481
} // namespace synchronization_internal

0 commit comments

Comments
 (0)