Błąd: stack-use-after-scope
Błąd oczyszczania adresu: użycie pamięci stosu poza zakresem
Użycie adresu stosu poza zakresem leksykalnym okresu istnienia zmiennej może mieć miejsce na wiele sposobów w języku C lub C++.
Przykład 1 — proste zagnieżdżone lokalne
// example1.cpp
// stack-use-after-scope error
int *gp;
bool b = true;
int main() {
if (b) {
int x[5];
gp = x+1;
}
return *gp; // Boom!
}
Aby skompilować i przetestować ten przykład, uruchom następujące polecenia w wierszu polecenia dewelopera w wersji 16.9 lub nowszej Visual Studio 2019 r.:
cl example1.cpp /fsanitize=address /Zi
devenv /debugexe example1.exe
Wynikowy błąd — prosty zagnieżdżony lokalny
Przykład 2 — przechwytywanie lambda
// example2.cpp
// stack-use-after-scope error
#include <functional>
int main() {
std::function<int()> f;
{
int x = 0;
f = [&x]() {
return x; // Boom!
};
}
return f(); // Boom!
}
Aby skompilować i przetestować ten przykład, uruchom następujące polecenia w wierszu polecenia dewelopera w wersji 16.9 lub nowszej Visual Studio 2019 r.:
cl example2.cpp /fsanitize=address /Zi
devenv /debugexe example2.exe
Wynikowy błąd — przechwytywanie lambda
Przykład 3 — porządkowanie destruktora za pomocą ustawień lokalnych
// example3.cpp
// stack-use-after-scope error
#include <stdio.h>
struct IntHolder {
explicit IntHolder(int* val = 0) : val_(val) { }
~IntHolder() {
printf("Value: %d\n", *val_); // Bom!
}
void set(int* val) { val_ = val; }
int* get() { return val_; }
int* val_;
};
int main(int argc, char* argv[]) {
// It's incorrect to use "x" inside the IntHolder destructor,
// because the lifetime of "x" ends earlier. Per the C++ standard,
// local lifetimes end in reverse order of declaration.
IntHolder holder;
int x = argc;
holder.set(&x);
return 0;
}
Aby skompilować i przetestować ten przykład, uruchom następujące polecenia w wierszu polecenia dewelopera w wersji 16.9 lub nowszej Visual Studio 2019 r.:
cl example3.cpp /fsanitize=address /Zi /O1
devenv /debugexe example3.exe
Wynikowy błąd — porządkowanie destruktora
Przykład 4 — tymczasowe
// example4.cpp
// stack-use-after-scope error
#include <iostream>
struct A {
A(const int& v) {
p = &v;
}
void print() {
std::cout << *p;
}
const int* p;
};
void explicit_temp() {
A a(5); // the temp for 5 is no longer live;
a.print();
}
void temp_from_conversion() {
double v = 5;
A a(v); // local v is no longer live.
a.print();
}
void main() {
explicit_temp();
temp_from_conversion();
}
Aby skompilować i przetestować ten przykład, uruchom następujące polecenia w wierszu polecenia dewelopera w wersji 16.9 lub nowszej Visual Studio 2019 r.:
cl example4.cpp /EHsc /fsanitize=address /Zi
devenv /debugexe example4.exe
Wynikowy błąd — tymczasowe
Zobacz też
Omówienie narzędzia AddressSanitizer
Rozwiązywanie znanych problemów z rozwiązaniemSanitizer
Dokumentacja kompilatora AddressSanitizer i języka
Dokumentacja środowiska uruchomieniowego addressSanitizer
Bajty cieni addressSanitizer
AddressSanitizer — chmura lub testowanie rozproszone
Integracja debugera AddressSanitizer
Przykłady błędów addressSanitizer