Commit d8881ea190182225c52b111c61578a907084b489

Authored by Stéphane Raimbault
1 parent 93a05986

Move check of device earlier to avoid a free call

Showing 1 changed file with 7 additions and 8 deletions
src/modbus-rtu.c
... ... @@ -1146,6 +1146,13 @@ modbus_t* modbus_new_rtu(const char *device,
1146 1146 modbus_t *ctx;
1147 1147 modbus_rtu_t *ctx_rtu;
1148 1148  
  1149 + /* Check device argument */
  1150 + if (device == NULL || (*device) == 0) {
  1151 + fprintf(stderr, "The device string is empty\n");
  1152 + errno = EINVAL;
  1153 + return NULL;
  1154 + }
  1155 +
1149 1156 /* Check baud argument */
1150 1157 if (baud == 0) {
1151 1158 fprintf(stderr, "The baud rate value must not be zero\n");
... ... @@ -1160,14 +1167,6 @@ modbus_t* modbus_new_rtu(const char *device,
1160 1167 ctx_rtu = (modbus_rtu_t *)ctx->backend_data;
1161 1168 ctx_rtu->device = NULL;
1162 1169  
1163   - /* Check device argument */
1164   - if (device == NULL || (*device) == 0) {
1165   - fprintf(stderr, "The device string is empty\n");
1166   - modbus_free(ctx);
1167   - errno = EINVAL;
1168   - return NULL;
1169   - }
1170   -
1171 1170 /* Device name and \0 */
1172 1171 ctx_rtu->device = (char *) malloc((strlen(device) + 1) * sizeof(char));
1173 1172 strcpy(ctx_rtu->device, device);
... ...