Commit 192fac7c1a078f7648312f6e8ae8c811459ffd5d

Authored by Stéphane Raimbault
1 parent 987d5af1

Avoid negative value in FD_SET call

src/modbus.c
... ... @@ -371,6 +371,13 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
371 371 }
372 372 }
373 373  
  374 + if (ctx->s == -1) {
  375 + if (ctx->debug) {
  376 + fprintf(stderr, "ERROR The connection is not established.\n");
  377 + }
  378 + return -1;
  379 + }
  380 +
374 381 /* Add a file descriptor to the set */
375 382 FD_ZERO(&rset);
376 383 FD_SET(ctx->s, &rset);
... ...
tests/unit-test-client.c
... ... @@ -786,6 +786,13 @@ int test_server(modbus_t *ctx, int use_backend)
786 786 modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
787 787 modbus_set_response_timeout(ctx, 0, 600000);
788 788  
  789 + int old_s = modbus_get_socket(ctx);
  790 + modbus_set_socket(ctx, -1);
  791 + rc = modbus_receive(ctx, rsp);
  792 + modbus_set_socket(ctx, old_s);
  793 + printf("* modbus_receive with invalid socket: ");
  794 + ASSERT_TRUE(rc == -1, "FAILED (%d)\n", rc);
  795 +
789 796 req_length = modbus_send_raw_request(ctx, read_raw_req, READ_RAW_REQ_LEN);
790 797 printf("* modbus_send_raw_request: ");
791 798 ASSERT_TRUE(req_length == (backend_length + 5), "FAILED (%d)\n", req_length);
... ...