foo(foo &afoo): va(foo,va++){
}
What is the security issue or problem of this code snippet.
This with g++ compiles, and I don't think there's any UB
struct Va
{
Va(struct Foo&, int) {}
};
int operator++(const Va&, int) { return 42; }
struct Foo
{
Va va;
Foo(Foo &afoo) : va(afoo,va++) {}
};
to be specific operator++
is not doing anything with the not-yet-initialized va
data member. It's more or less like passing *this
(as reference) or this
(as pointer) to a base class or a function in the initialization list... it's correctly reported by some compilers as a dangerous operation but it's legal if the referenced object is not accessed (and it's actually sometimes useful if you only need the address).
It is UB because it changes the value of va twice in a single command.
But isn't: foo(foo &afoo): va(afoo,va++) {}
?
精彩评论