开发者

C++ mysterious code. any security issue? [closed]

开发者 https://www.devze.com 2023-02-07 19:56 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago. 开发者_开发知识库
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++) {}?

0

精彩评论

暂无评论...
验证码 换一张
取 消