Commit 0e2f470a315f19124122d757397145cf3e60c0f8
1 parent
2fb7c2eb
Fix late check of ctx in modbus_reply[|_exception] (closes #269)
Showing
1 changed file
with
16 additions
and
7 deletions
src/modbus.c
| @@ -680,10 +680,10 @@ static int response_exception(modbus_t *ctx, sft_t *sft, | @@ -680,10 +680,10 @@ static int response_exception(modbus_t *ctx, sft_t *sft, | ||
| 680 | int modbus_reply(modbus_t *ctx, const uint8_t *req, | 680 | int modbus_reply(modbus_t *ctx, const uint8_t *req, |
| 681 | int req_length, modbus_mapping_t *mb_mapping) | 681 | int req_length, modbus_mapping_t *mb_mapping) |
| 682 | { | 682 | { |
| 683 | - int offset = ctx->backend->header_length; | ||
| 684 | - int slave = req[offset - 1]; | ||
| 685 | - int function = req[offset]; | ||
| 686 | - uint16_t address = (req[offset + 1] << 8) + req[offset + 2]; | 683 | + int offset; |
| 684 | + int slave; | ||
| 685 | + int function; | ||
| 686 | + uint16_t address; | ||
| 687 | uint8_t rsp[MAX_MESSAGE_LENGTH]; | 687 | uint8_t rsp[MAX_MESSAGE_LENGTH]; |
| 688 | int rsp_length = 0; | 688 | int rsp_length = 0; |
| 689 | sft_t sft; | 689 | sft_t sft; |
| @@ -693,6 +693,11 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req, | @@ -693,6 +693,11 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req, | ||
| 693 | return -1; | 693 | return -1; |
| 694 | } | 694 | } |
| 695 | 695 | ||
| 696 | + offset = ctx->backend->header_length; | ||
| 697 | + slave = req[offset - 1]; | ||
| 698 | + function = req[offset]; | ||
| 699 | + address = (req[offset + 1] << 8) + req[offset + 2]; | ||
| 700 | + | ||
| 696 | sft.slave = slave; | 701 | sft.slave = slave; |
| 697 | sft.function = function; | 702 | sft.function = function; |
| 698 | sft.t_id = ctx->backend->prepare_response_tid(req, &req_length); | 703 | sft.t_id = ctx->backend->prepare_response_tid(req, &req_length); |
| @@ -1062,9 +1067,9 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req, | @@ -1062,9 +1067,9 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req, | ||
| 1062 | int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, | 1067 | int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, |
| 1063 | unsigned int exception_code) | 1068 | unsigned int exception_code) |
| 1064 | { | 1069 | { |
| 1065 | - int offset = ctx->backend->header_length; | ||
| 1066 | - int slave = req[offset - 1]; | ||
| 1067 | - int function = req[offset]; | 1070 | + int offset; |
| 1071 | + int slave; | ||
| 1072 | + int function; | ||
| 1068 | uint8_t rsp[MAX_MESSAGE_LENGTH]; | 1073 | uint8_t rsp[MAX_MESSAGE_LENGTH]; |
| 1069 | int rsp_length; | 1074 | int rsp_length; |
| 1070 | int dummy_length = 99; | 1075 | int dummy_length = 99; |
| @@ -1075,6 +1080,10 @@ int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, | @@ -1075,6 +1080,10 @@ int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, | ||
| 1075 | return -1; | 1080 | return -1; |
| 1076 | } | 1081 | } |
| 1077 | 1082 | ||
| 1083 | + offset = ctx->backend->header_length; | ||
| 1084 | + slave = req[offset - 1]; | ||
| 1085 | + function = req[offset]; | ||
| 1086 | + | ||
| 1078 | sft.slave = slave; | 1087 | sft.slave = slave; |
| 1079 | sft.function = function + 0x80;; | 1088 | sft.function = function + 0x80;; |
| 1080 | sft.t_id = ctx->backend->prepare_response_tid(req, &dummy_length); | 1089 | sft.t_id = ctx->backend->prepare_response_tid(req, &dummy_length); |