Commit 6914cf96f8f933e0849264ef36c9c662b850ee79
1 parent
192fac7c
Test socket against positive value instead of -1
Showing
3 changed files
with
7 additions
and
7 deletions
src/modbus-rtu.c
| ... | ... | @@ -595,7 +595,7 @@ static int _modbus_rtu_connect(modbus_t *ctx) |
| 595 | 595 | #endif |
| 596 | 596 | |
| 597 | 597 | ctx->s = open(ctx_rtu->device, flags); |
| 598 | - if (ctx->s == -1) { | |
| 598 | + if (ctx->s < 0) { | |
| 599 | 599 | if (ctx->debug) { |
| 600 | 600 | fprintf(stderr, "ERROR Can't open the device %s (%s)\n", |
| 601 | 601 | ctx_rtu->device, strerror(errno)); |
| ... | ... | @@ -1135,7 +1135,7 @@ static void _modbus_rtu_close(modbus_t *ctx) |
| 1135 | 1135 | (int)GetLastError()); |
| 1136 | 1136 | } |
| 1137 | 1137 | #else |
| 1138 | - if (ctx->s != -1) { | |
| 1138 | + if (ctx->s >= 0) { | |
| 1139 | 1139 | tcsetattr(ctx->s, TCSANOW, &ctx_rtu->old_tios); |
| 1140 | 1140 | close(ctx->s); |
| 1141 | 1141 | ctx->s = -1; | ... | ... |
src/modbus-tcp.c
| ... | ... | @@ -323,7 +323,7 @@ static int _modbus_tcp_connect(modbus_t *ctx) |
| 323 | 323 | #endif |
| 324 | 324 | |
| 325 | 325 | ctx->s = socket(PF_INET, flags, 0); |
| 326 | - if (ctx->s == -1) { | |
| 326 | + if (ctx->s < 0) { | |
| 327 | 327 | return -1; |
| 328 | 328 | } |
| 329 | 329 | |
| ... | ... | @@ -432,7 +432,7 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx) |
| 432 | 432 | /* Closes the network connection and socket in TCP mode */ |
| 433 | 433 | static void _modbus_tcp_close(modbus_t *ctx) |
| 434 | 434 | { |
| 435 | - if (ctx->s != -1) { | |
| 435 | + if (ctx->s >= 0) { | |
| 436 | 436 | shutdown(ctx->s, SHUT_RDWR); |
| 437 | 437 | close(ctx->s); |
| 438 | 438 | ctx->s = -1; |
| ... | ... | @@ -674,7 +674,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *s) |
| 674 | 674 | ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen); |
| 675 | 675 | #endif |
| 676 | 676 | |
| 677 | - if (ctx->s == -1) { | |
| 677 | + if (ctx->s < 0) { | |
| 678 | 678 | return -1; |
| 679 | 679 | } |
| 680 | 680 | |
| ... | ... | @@ -704,7 +704,7 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s) |
| 704 | 704 | ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen); |
| 705 | 705 | #endif |
| 706 | 706 | |
| 707 | - if (ctx->s == -1) { | |
| 707 | + if (ctx->s < 0) { | |
| 708 | 708 | return -1; |
| 709 | 709 | } |
| 710 | 710 | ... | ... |
src/modbus.c