From 6914cf96f8f933e0849264ef36c9c662b850ee79 Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Thu, 18 Aug 2022 13:39:00 +0200 Subject: [PATCH] Test socket against positive value instead of -1 --- src/modbus-rtu.c | 4 ++-- src/modbus-tcp.c | 8 ++++---- src/modbus.c | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/modbus-rtu.c b/src/modbus-rtu.c index 00a2faf..38f6deb 100644 --- a/src/modbus-rtu.c +++ b/src/modbus-rtu.c @@ -595,7 +595,7 @@ static int _modbus_rtu_connect(modbus_t *ctx) #endif ctx->s = open(ctx_rtu->device, flags); - if (ctx->s == -1) { + if (ctx->s < 0) { if (ctx->debug) { fprintf(stderr, "ERROR Can't open the device %s (%s)\n", ctx_rtu->device, strerror(errno)); @@ -1135,7 +1135,7 @@ static void _modbus_rtu_close(modbus_t *ctx) (int)GetLastError()); } #else - if (ctx->s != -1) { + if (ctx->s >= 0) { tcsetattr(ctx->s, TCSANOW, &ctx_rtu->old_tios); close(ctx->s); ctx->s = -1; diff --git a/src/modbus-tcp.c b/src/modbus-tcp.c index 015d66f..4b454c4 100644 --- a/src/modbus-tcp.c +++ b/src/modbus-tcp.c @@ -323,7 +323,7 @@ static int _modbus_tcp_connect(modbus_t *ctx) #endif ctx->s = socket(PF_INET, flags, 0); - if (ctx->s == -1) { + if (ctx->s < 0) { return -1; } @@ -432,7 +432,7 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx) /* Closes the network connection and socket in TCP mode */ static void _modbus_tcp_close(modbus_t *ctx) { - if (ctx->s != -1) { + if (ctx->s >= 0) { shutdown(ctx->s, SHUT_RDWR); close(ctx->s); ctx->s = -1; @@ -674,7 +674,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *s) ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen); #endif - if (ctx->s == -1) { + if (ctx->s < 0) { return -1; } @@ -704,7 +704,7 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s) ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen); #endif - if (ctx->s == -1) { + if (ctx->s < 0) { return -1; } diff --git a/src/modbus.c b/src/modbus.c index 83ffdc4..0dbf82b 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -371,7 +371,7 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) } } - if (ctx->s == -1) { + if (ctx->s < 0) { if (ctx->debug) { fprintf(stderr, "ERROR The connection is not established.\n"); } -- libgit2 0.21.4