Commit 6914cf96f8f933e0849264ef36c9c662b850ee79

Authored by Stéphane Raimbault
1 parent 192fac7c

Test socket against positive value instead of -1

src/modbus-rtu.c
@@ -595,7 +595,7 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -595,7 +595,7 @@ static int _modbus_rtu_connect(modbus_t *ctx)
595 #endif 595 #endif
596 596
597 ctx->s = open(ctx_rtu->device, flags); 597 ctx->s = open(ctx_rtu->device, flags);
598 - if (ctx->s == -1) { 598 + if (ctx->s < 0) {
599 if (ctx->debug) { 599 if (ctx->debug) {
600 fprintf(stderr, "ERROR Can't open the device %s (%s)\n", 600 fprintf(stderr, "ERROR Can't open the device %s (%s)\n",
601 ctx_rtu->device, strerror(errno)); 601 ctx_rtu->device, strerror(errno));
@@ -1135,7 +1135,7 @@ static void _modbus_rtu_close(modbus_t *ctx) @@ -1135,7 +1135,7 @@ static void _modbus_rtu_close(modbus_t *ctx)
1135 (int)GetLastError()); 1135 (int)GetLastError());
1136 } 1136 }
1137 #else 1137 #else
1138 - if (ctx->s != -1) { 1138 + if (ctx->s >= 0) {
1139 tcsetattr(ctx->s, TCSANOW, &ctx_rtu->old_tios); 1139 tcsetattr(ctx->s, TCSANOW, &ctx_rtu->old_tios);
1140 close(ctx->s); 1140 close(ctx->s);
1141 ctx->s = -1; 1141 ctx->s = -1;
src/modbus-tcp.c
@@ -323,7 +323,7 @@ static int _modbus_tcp_connect(modbus_t *ctx) @@ -323,7 +323,7 @@ static int _modbus_tcp_connect(modbus_t *ctx)
323 #endif 323 #endif
324 324
325 ctx->s = socket(PF_INET, flags, 0); 325 ctx->s = socket(PF_INET, flags, 0);
326 - if (ctx->s == -1) { 326 + if (ctx->s < 0) {
327 return -1; 327 return -1;
328 } 328 }
329 329
@@ -432,7 +432,7 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx) @@ -432,7 +432,7 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx)
432 /* Closes the network connection and socket in TCP mode */ 432 /* Closes the network connection and socket in TCP mode */
433 static void _modbus_tcp_close(modbus_t *ctx) 433 static void _modbus_tcp_close(modbus_t *ctx)
434 { 434 {
435 - if (ctx->s != -1) { 435 + if (ctx->s >= 0) {
436 shutdown(ctx->s, SHUT_RDWR); 436 shutdown(ctx->s, SHUT_RDWR);
437 close(ctx->s); 437 close(ctx->s);
438 ctx->s = -1; 438 ctx->s = -1;
@@ -674,7 +674,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *s) @@ -674,7 +674,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *s)
674 ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen); 674 ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen);
675 #endif 675 #endif
676 676
677 - if (ctx->s == -1) { 677 + if (ctx->s < 0) {
678 return -1; 678 return -1;
679 } 679 }
680 680
@@ -704,7 +704,7 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s) @@ -704,7 +704,7 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s)
704 ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen); 704 ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen);
705 #endif 705 #endif
706 706
707 - if (ctx->s == -1) { 707 + if (ctx->s < 0) {
708 return -1; 708 return -1;
709 } 709 }
710 710
src/modbus.c
@@ -371,7 +371,7 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) @@ -371,7 +371,7 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
371 } 371 }
372 } 372 }
373 373
374 - if (ctx->s == -1) { 374 + if (ctx->s < 0) {
375 if (ctx->debug) { 375 if (ctx->debug) {
376 fprintf(stderr, "ERROR The connection is not established.\n"); 376 fprintf(stderr, "ERROR The connection is not established.\n");
377 } 377 }