瀏覽代碼

Fix the fix of device string check.

Thanks to Jan Kardell.
Stéphane Raimbault 11 年之前
父節點
當前提交
b09ff2fbb4
共有 1 個文件被更改,包括 4 次插入6 次删除
  1. 4 6
      src/modbus-rtu.c

+ 4 - 6
src/modbus-rtu.c

@@ -1133,25 +1133,23 @@ modbus_t* modbus_new_rtu(const char *device,
 {
     modbus_t *ctx;
     modbus_rtu_t *ctx_rtu;
-    size_t device_size;
 
     ctx = (modbus_t *) malloc(sizeof(modbus_t));
     _modbus_init_common(ctx);
-
     ctx->backend = &_modbus_rtu_backend;
     ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t));
     ctx_rtu = (modbus_rtu_t *)ctx->backend_data;
 
-    /* Device name and \0 */
-    device_size = (strlen(device) + 1) * sizeof(char);
-    if (device_size == 0) {
+    /* Check device argument */
+    if (device == NULL || (*device) == 0) {
         fprintf(stderr, "The device string is empty\n");
         modbus_free(ctx);
         errno = EINVAL;
         return NULL;
     }
 
-    ctx_rtu->device = (char *) malloc(device_size);
+    /* Device name and \0 */
+    ctx_rtu->device = (char *) malloc((strlen(device) + 1) * sizeof(char));
     strcpy(ctx_rtu->device, device);
 
     ctx_rtu->baud = baud;