Skip to content

Commit

Permalink
Clarify the peer pointer's validity using an assertion (CWE-476)
Browse files Browse the repository at this point in the history
The prior switch statement dereferences the peer pointer in each valid
case, so if peer really was NULL then the program will have crashed
due to derefercing a null pointer.

Thus, this peer != NULL statement is misleading: it implies that peer
might legitimately be NULL, when infact, we expect peer to be valid at
this point.

So instead, we replace this checking with our expectation (using an
assertion).

Flagged by Coverity:

In enet_protocol_handle_incoming_commands(_ENetHost *, _ENetEvent *):
All paths that lead to this null pointer comparison already dereference
the pointer earlier (CWE-476)
  • Loading branch information
kcgen committed Jul 28, 2022
1 parent 10be52f commit a1a909b
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion include/enet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2641,7 +2641,8 @@ extern "C" {
goto commandError;
}

if (peer != NULL && (command->header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) != 0) {
assert(peer);
if ((command->header.command & ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE) != 0) {
enet_uint16 sentTime;

if (!(flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME)) {
Expand Down

0 comments on commit a1a909b

Please sign in to comment.