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

[ASAN] AddressSanitizer FN, failed to report memory leaks #110360

Open
wr-web opened this issue Sep 28, 2024 · 3 comments
Open

[ASAN] AddressSanitizer FN, failed to report memory leaks #110360

wr-web opened this issue Sep 28, 2024 · 3 comments
Labels
compiler-rt:asan Address sanitizer compiler-rt:lsan Leak sanitizer

Comments

@wr-web
Copy link

wr-web commented Sep 28, 2024

reproduce: https://godbolt.org/z/WEjY9rY1G
bug code:

#include <iostream>

void unsuspectingBug() {
    int* ptr = new int(42);

    // Perform some logical operations that don't affect ptr directly
    for (int i = 0; i < 100; ++i) {
        int temp = i * i; // Irrelevant computation
        if (temp == 42) {
            delete ptr;
        }
    }

    // Forgetting to nullify ptr after deletion
    // Logical usage without realizing it's been erased
    if (ptr) {
        std::cout << *ptr << std::endl; // Dereference of dangling pointer not realized due to logical oversight
    }
}

int main() {
    unsuspectingBug();
    return 0;
}
@wr-web wr-web changed the title [ASAN] AddressSanitizer FN, failed to report memory leak [ASAN] AddressSanitizer FN, failed to report memory leaks Sep 28, 2024
@DimitryAndric
Copy link
Collaborator

Note that this code only leaks memory, it doesn't deference the pointer after freeing it, as it is never freed. This is because i * i can never become 42. If you use -fsanitize=leak it will show the same report as gcc.

@DimitryAndric DimitryAndric added compiler-rt:lsan Leak sanitizer compiler-rt:asan Address sanitizer and removed new issue labels Sep 28, 2024
@DimitryAndric
Copy link
Collaborator

I'm unsure how godbolt's clang is configured and built, but apparently gcc enables LeakSanitizer when using AddressSanitizer?

@wr-web
Copy link
Author

wr-web commented Sep 28, 2024

Note that this code only leaks memory, it doesn't deference the pointer after freeing it, as it is never freed. This is because i * i can never become 42. If you use -fsanitize=leak it will show the same report as gcc.

It seems that enabling both sanitizers fails to report the leak. You can see this here: https://godbolt.org/z/e5oMnEP91.

However, enabling only LeakSanitizer for Clang will correctly detect the issue: https://godbolt.org/z/9W8Mrc3Mv.

This is likely a false negative (FN)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-rt:asan Address sanitizer compiler-rt:lsan Leak sanitizer
Projects
None yet
Development

No branches or pull requests

2 participants