From 0e2f470a315f19124122d757397145cf3e60c0f8 Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Thu, 11 Jun 2015 22:11:25 +0200 Subject: [PATCH] Fix late check of ctx in modbus_reply[|_exception] (closes #269) --- src/modbus.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/modbus.c b/src/modbus.c index adfc645..3edbfc8 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -680,10 +680,10 @@ static int response_exception(modbus_t *ctx, sft_t *sft, int modbus_reply(modbus_t *ctx, const uint8_t *req, int req_length, modbus_mapping_t *mb_mapping) { - int offset = ctx->backend->header_length; - int slave = req[offset - 1]; - int function = req[offset]; - uint16_t address = (req[offset + 1] << 8) + req[offset + 2]; + int offset; + int slave; + int function; + uint16_t address; uint8_t rsp[MAX_MESSAGE_LENGTH]; int rsp_length = 0; sft_t sft; @@ -693,6 +693,11 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req, return -1; } + offset = ctx->backend->header_length; + slave = req[offset - 1]; + function = req[offset]; + address = (req[offset + 1] << 8) + req[offset + 2]; + sft.slave = slave; sft.function = function; 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, int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, unsigned int exception_code) { - int offset = ctx->backend->header_length; - int slave = req[offset - 1]; - int function = req[offset]; + int offset; + int slave; + int function; uint8_t rsp[MAX_MESSAGE_LENGTH]; int rsp_length; int dummy_length = 99; @@ -1075,6 +1080,10 @@ int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, return -1; } + offset = ctx->backend->header_length; + slave = req[offset - 1]; + function = req[offset]; + sft.slave = slave; sft.function = function + 0x80;; sft.t_id = ctx->backend->prepare_response_tid(req, &dummy_length); -- libgit2 0.21.4