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

Avoid undefined behavior when subtracting tagged pointers #715

Merged
merged 2 commits into from
Sep 12, 2024

Conversation

thurstond
Copy link
Contributor

@thurstond thurstond commented Sep 12, 2024

When compiling with HardwareAddressSanitizer (https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html), the top byte of pointers are tagged, hence subtracting two pointers may overflow. Overflow is undefined behavior for signed integers, resulting in errors such as:

    third_party/nanobind/src/nb_type.cpp:123:50: runtime error: signed integer overflow: -6701078944169102656 - 9079534012674239392 cannot be represented in type 'intptr_t' (aka 'long')

This patch fixes the issue by using unsigned integers, for which overflow is well-defined.

When compiling with HardwareAddressSanitizer, the top byte of pointers
are tagged, hence subtracting two pointers may overflow. Overflow is
undefined behavior for signed integers, resulting in errors such as:
    third_party/nanobind/src/nb_type.cpp:123:50: runtime error: signed integer overflow: -6701078944169102656 - 9079534012674239392 cannot be represented in type 'intptr_t' (aka 'long')

This patch fixes the issue by using unsigned integers, for which
overflow is well-defined.
@wjakob
Copy link
Owner

wjakob commented Sep 12, 2024

Thank you!

@wjakob wjakob merged commit bceec36 into wjakob:master Sep 12, 2024
25 checks passed
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