Commit 1d4241dff55c5bbaabef9518aed54b883edb492f
1 parent
97ccd92c
Detect abnormal use of compute_header_length and fix comment
Showing
1 changed file
with
21 additions
and
16 deletions
src/modbus.c
| ... | ... | @@ -268,8 +268,7 @@ static unsigned int compute_response_length(modbus_t *ctx, uint8_t *req) |
| 268 | 268 | case FC_READ_HOLDING_REGISTERS: |
| 269 | 269 | case FC_READ_INPUT_REGISTERS: |
| 270 | 270 | /* Header + 2 * nb values */ |
| 271 | - length = 2 + 2 * (req[offset + 3] << 8 | | |
| 272 | - req[offset + 4]); | |
| 271 | + length = 2 + 2 * (req[offset + 3] << 8 | req[offset + 4]); | |
| 273 | 272 | break; |
| 274 | 273 | case FC_READ_EXCEPTION_STATUS: |
| 275 | 274 | length = 3; |
| ... | ... | @@ -497,26 +496,32 @@ typedef enum { |
| 497 | 496 | MSG_CONFIRMATION |
| 498 | 497 | } msg_type_t; |
| 499 | 498 | |
| 500 | -/* Computes the header length (to reach the the function code) */ | |
| 499 | +/* Computes the header length (to reach the real data) */ | |
| 501 | 500 | static uint8_t compute_header_length(int function, msg_type_t msg_type) |
| 502 | 501 | { |
| 503 | 502 | int length; |
| 504 | 503 | |
| 505 | - if (function <= FC_WRITE_SINGLE_COIL || | |
| 506 | - function == FC_WRITE_SINGLE_REGISTER) { | |
| 507 | - length = 4; | |
| 508 | - } else if (function == FC_WRITE_MULTIPLE_COILS || | |
| 509 | - function == FC_WRITE_MULTIPLE_REGISTERS) { | |
| 510 | - length = 5; | |
| 511 | - } else if (function == FC_REPORT_SLAVE_ID) { | |
| 512 | - if (msg_type == MSG_INDICATION) | |
| 504 | + if (msg_type == MSG_INDICATION) { | |
| 505 | + if (function == FC_REPORT_SLAVE_ID) { | |
| 513 | 506 | length = 0; |
| 514 | - else | |
| 507 | + } else { | |
| 508 | + /* Should never happen, the other header lengths are precomputed */ | |
| 509 | + abort(); | |
| 510 | + } | |
| 511 | + } else /* MSG_CONFIRMATION */ { | |
| 512 | + if (function <= FC_WRITE_SINGLE_COIL || | |
| 513 | + function == FC_WRITE_SINGLE_REGISTER) { | |
| 514 | + length = 4; | |
| 515 | + } else if (function == FC_WRITE_MULTIPLE_COILS || | |
| 516 | + function == FC_WRITE_MULTIPLE_REGISTERS) { | |
| 517 | + length = 5; | |
| 518 | + } else if (function == FC_REPORT_SLAVE_ID) { | |
| 515 | 519 | length = 1; |
| 516 | - } else if (function == FC_READ_AND_WRITE_REGISTERS) { | |
| 517 | - length = 9; | |
| 518 | - } else { | |
| 519 | - length = 0; | |
| 520 | + } else if (function == FC_READ_AND_WRITE_REGISTERS) { | |
| 521 | + length = 9; | |
| 522 | + } else { | |
| 523 | + length = 0; | |
| 524 | + } | |
| 520 | 525 | } |
| 521 | 526 | return length; |
| 522 | 527 | } | ... | ... |