Permalink
Browse files

Move check of device earlier to avoid a free call

  • Loading branch information...
1 parent 93a0598 commit d8881ea190182225c52b111c61578a907084b489 @stephane committed Apr 9, 2014
Showing with 7 additions and 8 deletions.
  1. +7 −8 src/modbus-rtu.c
View
@@ -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);

0 comments on commit d8881ea

Please sign in to comment.