Эх сурвалжийг харах

Avoid negative value in FD_SET call

Stéphane Raimbault 2 жил өмнө
parent
commit
192fac7c1a

+ 7 - 0
src/modbus.c

@@ -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);

+ 7 - 0
tests/unit-test-client.c

@@ -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);