Skip to content

Commit

Permalink
Move check of device earlier to avoid a free call
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane committed Apr 9, 2014
1 parent 93a0598 commit d8881ea
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/modbus-rtu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down

0 comments on commit d8881ea

Please sign in to comment.