From d8881ea190182225c52b111c61578a907084b489 Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Wed, 9 Apr 2014 12:39:54 +0200 Subject: [PATCH] Move check of device earlier to avoid a free call --- src/modbus-rtu.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/modbus-rtu.c b/src/modbus-rtu.c index 7076262..04f0ef2 100644 --- a/src/modbus-rtu.c +++ b/src/modbus-rtu.c @@ -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); -- libgit2 0.21.4