-
Notifications
You must be signed in to change notification settings - Fork 50
Fix two issues that undefined behavior sanitizer caught #419
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
base: main
Are you sure you want to change the base?
Fix two issues that undefined behavior sanitizer caught #419
Conversation
Add size check before calling std::memcpy to avoid undefined behavior when the source pointer is null with size 0. The C standard states that passing null to memcpy is undefined behavior even with size 0. Fixes UBSan warning: null pointer passed as argument 2, which is declared to never be null.
Replace null pointer member access with the standard offsetof macro from <cstddef> to compute object offset. This avoids undefined behavior caught by UBSan (member access within null pointer). The offsetof macro is portable across GCC, Clang, and MSVC: - GCC/Clang: internally uses __builtin_offsetof - MSVC: uses its own internal mechanism Fixes UBSan warning: member access within null pointer of type 'Object'.
Summary of ChangesHello @henrylhtsang, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses two distinct instances of undefined behavior identified by the Undefined Behavior Sanitizer (UBSan). The changes enhance the robustness and correctness of the codebase by ensuring safer memory operations and standard-compliant offset calculations, thereby preventing potential crashes or unexpected program behavior. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
cc @junrushao |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request correctly addresses two sources of undefined behavior. The change in object.h to use the standard offsetof macro instead of manual pointer arithmetic is a great improvement for correctness and portability. Similarly, adding a size check before std::memcpy in string.h correctly prevents undefined behavior. The changes are approved.
| if (size > 0) { | ||
| std::memcpy(dest_data, data, size); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great fix to prevent undefined behavior when memcpy is called with a size of 0 and a null source pointer. I noticed that the Bytes::InitData method in this same file has a similar implementation without this check. It would be beneficial to apply the same fix there to ensure consistency and prevent the same undefined behavior in the Bytes class.
Fixes #413
Changes
std::memcpycall with size check to avoid passing null source pointer with size 0offsetofmacro for computing object header offsetsI tested manually for clang and gcc. Didn't test locally for msvc since I don't have access to windows. CI should test that.
script:
commands: