Commit b09ff2fbb4d08205ee2f3fed7d370e4f79247117

Authored by Stéphane Raimbault
1 parent f7799a4f

Fix the fix of device string check.

Thanks to Jan Kardell.
Showing 1 changed file with 4 additions and 6 deletions
src/modbus-rtu.c
@@ -1133,25 +1133,23 @@ modbus_t* modbus_new_rtu(const char *device, @@ -1133,25 +1133,23 @@ modbus_t* modbus_new_rtu(const char *device,
1133 { 1133 {
1134 modbus_t *ctx; 1134 modbus_t *ctx;
1135 modbus_rtu_t *ctx_rtu; 1135 modbus_rtu_t *ctx_rtu;
1136 - size_t device_size;  
1137 1136
1138 ctx = (modbus_t *) malloc(sizeof(modbus_t)); 1137 ctx = (modbus_t *) malloc(sizeof(modbus_t));
1139 _modbus_init_common(ctx); 1138 _modbus_init_common(ctx);
1140 -  
1141 ctx->backend = &_modbus_rtu_backend; 1139 ctx->backend = &_modbus_rtu_backend;
1142 ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t)); 1140 ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t));
1143 ctx_rtu = (modbus_rtu_t *)ctx->backend_data; 1141 ctx_rtu = (modbus_rtu_t *)ctx->backend_data;
1144 1142
1145 - /* Device name and \0 */  
1146 - device_size = (strlen(device) + 1) * sizeof(char);  
1147 - if (device_size == 0) { 1143 + /* Check device argument */
  1144 + if (device == NULL || (*device) == 0) {
1148 fprintf(stderr, "The device string is empty\n"); 1145 fprintf(stderr, "The device string is empty\n");
1149 modbus_free(ctx); 1146 modbus_free(ctx);
1150 errno = EINVAL; 1147 errno = EINVAL;
1151 return NULL; 1148 return NULL;
1152 } 1149 }
1153 1150
1154 - ctx_rtu->device = (char *) malloc(device_size); 1151 + /* Device name and \0 */
  1152 + ctx_rtu->device = (char *) malloc((strlen(device) + 1) * sizeof(char));
1155 strcpy(ctx_rtu->device, device); 1153 strcpy(ctx_rtu->device, device);
1156 1154
1157 ctx_rtu->baud = baud; 1155 ctx_rtu->baud = baud;