Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bad assert in hsRAMStream #1591

Merged
merged 2 commits into from
Jun 2, 2024
Merged

Conversation

dpogue
Copy link
Member

@dpogue dpogue commented Jun 2, 2024

This was added because UBSan was warning that we were calling memcpy with a null destination, and while that is technically true, it was also a no-op because it was copying 0 bytes. A null buffer gets properly resized with the data in the next step, so this is actually expected behaviour that is broken by asserting too early.

Instead, detect the cases where we're calling memcpy with actual data, and assert in those cases that the RAMStream buffer is non-null.

This was added because UBSan was warning that we were calling memcpy
with a null destination, and while that is technically true, it was also
a no-op because it was copying 0 bytes. A null buffer gets properly resized
with the data in the next step, so this is actually expected behaviour
that is broken by asserting too early.

Instead, detect the cases where we're calling memcpy with actual data,
and assert in those cases that the RAMStream buffer is non-null.
@dpogue
Copy link
Member Author

dpogue commented Jun 2, 2024

I added a (pretty minimal) unit test here that would trigger the previous assert, and is working now with the modified asserts.

@dpogue dpogue marked this pull request as ready for review June 2, 2024 20:27
@colincornaby colincornaby self-assigned this Jun 2, 2024
@dpogue dpogue merged commit c6895a6 into H-uru:master Jun 2, 2024
17 checks passed
@dpogue dpogue deleted the ramstream-assert branch June 2, 2024 21:19
Copy link
Member

@Hoikas Hoikas left a comment

Choose a reason for hiding this comment

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

Some comments about what the unit test is doing would potentially be a good addition.


TEST(hsRAMStream, initializeBufferOnFirstWrite)
{
const char* str = "hsRAMStream initializeBufferOnFirstWrite";
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const char* str = "hsRAMStream initializeBufferOnFirstWrite";
constexpr std::string_view str = "hsRAMStream initializeBufferOnFirstWrite";

hsRAMStream s;

s.WriteSafeString(str);
EXPECT_EQ(s.GetPosition(), strlen(str) + 2);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
EXPECT_EQ(s.GetPosition(), strlen(str) + 2);
EXPECT_EQ(s.GetPosition(), str.size() + 2);

EXPECT_EQ(s.GetPosition(), strlen(str) + 2);

s.WriteLE32(1);
EXPECT_EQ(s.GetPosition(), strlen(str) + 2 + 4);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
EXPECT_EQ(s.GetPosition(), strlen(str) + 2 + 4);
EXPECT_EQ(s.GetPosition(), str.size() + 2 + 4);


s.Skip(-4);
s.WriteLE32(5);
EXPECT_EQ(s.GetPosition(), strlen(str) + 2 + 4);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
EXPECT_EQ(s.GetPosition(), strlen(str) + 2 + 4);
EXPECT_EQ(s.GetPosition(), str.size() + 2 + 4);

*==LICENSE==*/

#include <gtest/gtest.h>

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#include <string_view>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants