Skip to content

[C++] Fix Undefined behavior for zero length vectors#5355

Merged
aardappel merged 2 commits into
google:masterfrom
emkornfield:fix_ubsan
May 30, 2019
Merged

[C++] Fix Undefined behavior for zero length vectors#5355
aardappel merged 2 commits into
google:masterfrom
emkornfield:fix_ubsan

Conversation

@emkornfield

Copy link
Copy Markdown
Contributor

UBSan fails when checking for nullability with clang in this header file without this change.

Comment thread include/flatbuffers/flatbuffers.h Outdated

void push(const uint8_t *bytes, size_t num) {
memcpy(make_space(num), bytes, num);
// CreateVector above, can pass through a nullptr for bytes

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If so, lets make the fix in CreateVector, not here. This is performance sensitive code, so I would like as few if-thens (or as far from the core) as possible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, are there benchmarks as part of the project I can run to see if there will be impact from my changes?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No there aren't. There are some super old benchmarks in a branch that are win32 only.

Regardless, if you can pull an if-then further away from a hot function, why not do it? especially when all other callers of this function already comply with memcpy's rules?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think its fine, just wondering if I can assess the impact further upstream. I will update the PR in the next day or two. Thank you for the feedback.

@emkornfield

Copy link
Copy Markdown
Contributor Author

@aardappel appveyor failures look unrelated to this change, but I'm not too familiar with the build, please let me know if you think this broke something?

@emkornfield emkornfield reopened this May 25, 2019
@aardappel

Copy link
Copy Markdown
Collaborator

Yes, CI looks unrelated.
Thanks, that's a better solution :)

@aardappel aardappel merged commit 79f0df3 into google:master May 30, 2019
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.

2 participants