Thanks to visit codestin.com
Credit goes to github.com

Skip to content

socket_status is very easy to use wrong (and you actually do it yourself) #49

@Windfisch

Description

@Windfisch

The socket_status struct wraps the return value from the network operations and provides an operator bool():

kissnet/kissnet.hpp

Lines 580 to 585 in 9db92fb

///implicitly convert this object to const bool (as the status should not change)
operator bool() const
{
//See the above enum: every value <= 0 correspond to an error, and will return false. Every value > 0 returns true
return value > 0;
}

Unfortunately, this makes the following code behave rather unobvious:

auto status = my_sock.recv(...);
if (status != socket_status::valid) { cout << "Error!"; }

One is tempted to think that this code checks whether status is socket_status::valid, but this is not what happens. At least under MSVC, this is equivalent to

if (status.operator bool() != static_cast<bool>(socket_status::valid))

which basically turns it into a check if the resulting status is positive (i.e. valid, wouldblock, connection closed etc.).

In

if (connect(curr_addr, timeout, false) != socket_status::valid)
and
if (connect(curr_addr, timeout, false) != socket_status::valid)
, kissnet itself has an example of such a bug.

I am not entirely sure whether this is a compiler bug or a bug in kissnet, but I do recommend to just drop the operator bool() entirely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions