Commit d8aa5a4214582532a2fea8e6ea8d8f5c3ba96206
1 parent
55d9a371
Always set socket to -1 on close()
Showing
2 changed files
with
8 additions
and
4 deletions
src/modbus-rtu.c
src/modbus-tcp.c
| ... | ... | @@ -318,6 +318,7 @@ static int _modbus_tcp_connect(modbus_t *ctx) |
| 318 | 318 | rc = _modbus_tcp_set_ipv4_options(ctx->s); |
| 319 | 319 | if (rc == -1) { |
| 320 | 320 | close(ctx->s); |
| 321 | + ctx->s = -1; | |
| 321 | 322 | return -1; |
| 322 | 323 | } |
| 323 | 324 | |
| ... | ... | @@ -331,6 +332,7 @@ static int _modbus_tcp_connect(modbus_t *ctx) |
| 331 | 332 | rc = _connect(ctx->s, (struct sockaddr *)&addr, sizeof(addr), &ctx->response_timeout); |
| 332 | 333 | if (rc == -1) { |
| 333 | 334 | close(ctx->s); |
| 335 | + ctx->s = -1; | |
| 334 | 336 | return -1; |
| 335 | 337 | } |
| 336 | 338 | |
| ... | ... | @@ -421,6 +423,7 @@ static void _modbus_tcp_close(modbus_t *ctx) |
| 421 | 423 | if (ctx->s != -1) { |
| 422 | 424 | shutdown(ctx->s, SHUT_RDWR); |
| 423 | 425 | close(ctx->s); |
| 426 | + ctx->s = -1; | |
| 424 | 427 | } |
| 425 | 428 | } |
| 426 | 429 | |
| ... | ... | @@ -616,8 +619,8 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) |
| 616 | 619 | } |
| 617 | 620 | |
| 618 | 621 | /* On success, the function return a non-negative integer that is a descriptor |
| 619 | - for the accepted socket. On error, -1 is returned, and errno is set | |
| 620 | - appropriately. */ | |
| 622 | +for the accepted socket. On error, socket is set to -1, -1 is returned and errno | |
| 623 | +is set appropriately. */ | |
| 621 | 624 | int modbus_tcp_accept(modbus_t *ctx, int *socket) |
| 622 | 625 | { |
| 623 | 626 | struct sockaddr_in addr; |
| ... | ... | @@ -638,7 +641,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *socket) |
| 638 | 641 | |
| 639 | 642 | if (ctx->s == -1) { |
| 640 | 643 | close(*socket); |
| 641 | - *socket = 0; | |
| 644 | + *socket = -1; | |
| 642 | 645 | return -1; |
| 643 | 646 | } |
| 644 | 647 | |
| ... | ... | @@ -664,7 +667,7 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *socket) |
| 664 | 667 | ctx->s = accept(*socket, (void *)&addr, &addrlen); |
| 665 | 668 | if (ctx->s == -1) { |
| 666 | 669 | close(*socket); |
| 667 | - *socket = 0; | |
| 670 | + *socket = -1; | |
| 668 | 671 | } |
| 669 | 672 | |
| 670 | 673 | if (ctx->debug) { | ... | ... |