Skip to content

Commit

Permalink
Avoid negative value in FD_SET call
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane committed Aug 18, 2022
1 parent 987d5af commit 192fac7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
}
}

if (ctx->s == -1) {
if (ctx->debug) {
fprintf(stderr, "ERROR The connection is not established.\n");
}
return -1;
}

/* Add a file descriptor to the set */
FD_ZERO(&rset);
FD_SET(ctx->s, &rset);
Expand Down
7 changes: 7 additions & 0 deletions tests/unit-test-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,13 @@ int test_server(modbus_t *ctx, int use_backend)
modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
modbus_set_response_timeout(ctx, 0, 600000);

int old_s = modbus_get_socket(ctx);
modbus_set_socket(ctx, -1);
rc = modbus_receive(ctx, rsp);
modbus_set_socket(ctx, old_s);
printf("* modbus_receive with invalid socket: ");
ASSERT_TRUE(rc == -1, "FAILED (%d)\n", rc);

req_length = modbus_send_raw_request(ctx, read_raw_req, READ_RAW_REQ_LEN);
printf("* modbus_send_raw_request: ");
ASSERT_TRUE(req_length == (backend_length + 5), "FAILED (%d)\n", req_length);
Expand Down

0 comments on commit 192fac7

Please sign in to comment.