Skip to content

Commit

Permalink
Buffer: Annotate test_buffer.cpp for coverity
Browse files Browse the repository at this point in the history
This change adds coverity specific tags before each unit test line that
intentionally accesses the buffer after it was moved from. This is to
suppress coverity warnings about the buffer being accessed after it was
moved from. This is done because ensuring use-after-move works correctly
is a part of the test.

These comments should inform both Coverity and maintainers of this.

Signed-off-by: Charlie Vigue <[email protected]>
  • Loading branch information
cvigue committed Dec 6, 2024
1 parent 7480a67 commit 185ab41
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions test/unittests/test_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,23 @@ TEST(buffer, invariants_after_move_safe)

BufferAllocated buf2(std::move(buf));

// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf.size(), 0u);
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf.capacity(), 0u);
// coverity[USE_AFTER_MOVE]
EXPECT_THROW(buf[0], BufferException);
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf.c_data(), nullptr);
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf.c_data_raw(), nullptr);
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf.data(), nullptr);
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf.data_raw(), nullptr);
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf.offset(), 0u);
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf.remaining(), 0u);
}

Expand All @@ -451,9 +460,13 @@ TEST(buffer, push_back_after_move_safe)
buf.realloc(11);
buf.push_back('X');

// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf2.size(), 11u);
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf2[0], 'h');
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf2[10], 'd');
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf[0], 'X');
}

Expand All @@ -467,8 +480,12 @@ TEST(buffer, append_after_move_safe)
buf_append_string(buf3, "hello again");
buf = buf3;

// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf2.size(), 11u);
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf2[0], 'h');
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf2[10], 'd');
// coverity[USE_AFTER_MOVE]
EXPECT_EQ(buf, buf3);
}

0 comments on commit 185ab41

Please sign in to comment.