Commit a1513415db13091ed5db604e1fb1ef4040db80f4
1 parent
19fc907a
- Test only the msg_length_computed on change
- Return test on read and recv slightly more robust - Fix a comment
Showing
1 changed file
with
13 additions
and
9 deletions
modbus/modbus.c
| ... | ... | @@ -512,21 +512,18 @@ int receive_msg(modbus_param_t *mb_param, |
| 512 | 512 | else |
| 513 | 513 | read_ret = recv(mb_param->fd, p_msg, length_to_read, 0); |
| 514 | 514 | |
| 515 | - if (read_ret == -1) { | |
| 515 | + if (read_ret == 0) { | |
| 516 | + printf("Connection closed\n"); | |
| 517 | + return CONNECTION_CLOSED; | |
| 518 | + } else if (read_ret < 0) { | |
| 519 | + /* The only negative possible value is -1 */ | |
| 516 | 520 | error_treat(mb_param, PORT_SOCKET_FAILURE, |
| 517 | 521 | "Read port/socket failure"); |
| 518 | 522 | return PORT_SOCKET_FAILURE; |
| 519 | - } else if (read_ret == 0) { | |
| 520 | - printf("Connection closed\n"); | |
| 521 | - return CONNECTION_CLOSED; | |
| 522 | 523 | } |
| 523 | 524 | |
| 524 | 525 | /* Sums bytes received */ |
| 525 | 526 | (*msg_length) += read_ret; |
| 526 | - if ((*msg_length) > MAX_MESSAGE_LENGTH) { | |
| 527 | - error_treat(mb_param, TOO_MANY_DATA, "Too many data"); | |
| 528 | - return TOO_MANY_DATA; | |
| 529 | - } | |
| 530 | 527 | |
| 531 | 528 | /* Display the hex code of each character received */ |
| 532 | 529 | if (mb_param->debug) { |
| ... | ... | @@ -544,11 +541,18 @@ int receive_msg(modbus_param_t *mb_param, |
| 544 | 541 | /* Function code position */ |
| 545 | 542 | length_to_read = compute_query_length_header(msg[mb_param->header_length + 1]); |
| 546 | 543 | msg_length_computed += length_to_read; |
| 544 | + /* It's useless to check | |
| 545 | + msg_length_computed value in this | |
| 546 | + case (only defined values are used). */ | |
| 547 | 547 | state = BYTE; |
| 548 | 548 | break; |
| 549 | 549 | case BYTE: |
| 550 | 550 | length_to_read = compute_query_length_data(mb_param, msg); |
| 551 | 551 | msg_length_computed += length_to_read; |
| 552 | + if (msg_length_computed > MAX_MESSAGE_LENGTH) { | |
| 553 | + error_treat(mb_param, TOO_MANY_DATA, "Too many data"); | |
| 554 | + return TOO_MANY_DATA; | |
| 555 | + } | |
| 552 | 556 | state = COMPLETE; |
| 553 | 557 | break; |
| 554 | 558 | case COMPLETE: |
| ... | ... | @@ -1042,7 +1046,7 @@ static int read_registers(modbus_param_t *mb_param, int slave, int function, |
| 1042 | 1046 | |
| 1043 | 1047 | offset = mb_param->header_length; |
| 1044 | 1048 | |
| 1045 | - /* If response_ret is negative, the loop is jumped ! */ | |
| 1049 | + /* If ret is negative, the loop is jumped ! */ | |
| 1046 | 1050 | for (i = 0; i < ret; i++) { |
| 1047 | 1051 | /* shift reg hi_byte to temp OR with lo_byte */ |
| 1048 | 1052 | data_dest[i] = response[offset + 3 + (i << 1)] << 8 | | ... | ... |