Ver Fonte

Move check of device earlier to avoid a free call

Stéphane Raimbault há 11 anos atrás
pai
commit
d8881ea190
1 ficheiros alterados com 7 adições e 8 exclusões
  1. 7 8
      src/modbus-rtu.c

+ 7 - 8
src/modbus-rtu.c

@@ -1146,6 +1146,13 @@ modbus_t* modbus_new_rtu(const char *device,
     modbus_t *ctx;
     modbus_rtu_t *ctx_rtu;
 
+    /* Check device argument */
+    if (device == NULL || (*device) == 0) {
+        fprintf(stderr, "The device string is empty\n");
+        errno = EINVAL;
+        return NULL;
+    }
+
     /* Check baud argument */
     if (baud == 0) {
         fprintf(stderr, "The baud rate value must not be zero\n");
@@ -1160,14 +1167,6 @@ modbus_t* modbus_new_rtu(const char *device,
     ctx_rtu = (modbus_rtu_t *)ctx->backend_data;
     ctx_rtu->device = NULL;
 
-    /* Check device argument */
-    if (device == NULL || (*device) == 0) {
-        fprintf(stderr, "The device string is empty\n");
-        modbus_free(ctx);
-        errno = EINVAL;
-        return NULL;
-    }
-
     /* Device name and \0 */
     ctx_rtu->device = (char *) malloc((strlen(device) + 1) * sizeof(char));
     strcpy(ctx_rtu->device, device);