From 80a4ea02c38bb35be2495a6f3423e0b98b9e3c69 Mon Sep 17 00:00:00 2001 From: Alexander Emelianov Date: Wed, 25 Oct 2017 11:35:22 +0500 Subject: [PATCH 1/6] Buffer overflow fix --- arduinoIDE/ModbusIP_ESP8266/ModbusIP_ESP8266.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arduinoIDE/ModbusIP_ESP8266/ModbusIP_ESP8266.cpp b/arduinoIDE/ModbusIP_ESP8266/ModbusIP_ESP8266.cpp index 70c103f..6a13aed 100644 --- a/arduinoIDE/ModbusIP_ESP8266/ModbusIP_ESP8266.cpp +++ b/arduinoIDE/ModbusIP_ESP8266/ModbusIP_ESP8266.cpp @@ -44,7 +44,7 @@ void ModbusIP::task() { _frame = (byte*) malloc(_len); raw_len = raw_len - 7; - for (int i=0; i< raw_len; i++) _frame[i] = client.read(); //Get Modbus PDU + for (int i=0; i< _len; i++) _frame[i] = client.read(); //Get Modbus PDU this->receivePDU(_frame); client.flush(); From 6eee258aea1e33b0476b216dd9c2172bd17ec601 Mon Sep 17 00:00:00 2001 From: Alexander Emelianov Date: Thu, 15 Feb 2018 14:00:09 +0500 Subject: [PATCH 2/6] exceptionalResponce call copy/paste error fix --- arduinoIDE/Modbus/Modbus.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arduinoIDE/Modbus/Modbus.cpp b/arduinoIDE/Modbus/Modbus.cpp index 6e9c7da..a590e8d 100644 --- a/arduinoIDE/Modbus/Modbus.cpp +++ b/arduinoIDE/Modbus/Modbus.cpp @@ -1,6 +1,6 @@ /* Modbus.cpp - Source for Modbus Base Library - Copyright (C) 2014 André Sarmento Barbosa + Copyright (C) 2014 André Sarmento Barbosa */ #include "Modbus.h" @@ -352,7 +352,7 @@ void Modbus::readInputStatus(word startreg, word numregs) { //Check Address //*** See comments on readCoils method. if (!this->searchRegister(startreg + 10001)) { - this->exceptionResponse(MB_FC_READ_COILS, MB_EX_ILLEGAL_ADDRESS); + this->exceptionResponse(MB_FC_READ_INPUT_STAT, MB_EX_ILLEGAL_ADDRESS); return; } @@ -403,7 +403,7 @@ void Modbus::readInputRegisters(word startreg, word numregs) { //Check Address //*** See comments on readCoils method. if (!this->searchRegister(startreg + 30001)) { - this->exceptionResponse(MB_FC_READ_COILS, MB_EX_ILLEGAL_ADDRESS); + this->exceptionResponse(MB_FC_READ_INPUT_REGS, MB_EX_ILLEGAL_ADDRESS); return; } From 2462b065fdd165750080e159a5d09384bd3c119a Mon Sep 17 00:00:00 2001 From: Alexander Emelianov Date: Thu, 15 Feb 2018 14:06:47 +0500 Subject: [PATCH 3/6] malloc error checking in addReg and exceptionResponse --- arduinoIDE/Modbus/Modbus.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arduinoIDE/Modbus/Modbus.cpp b/arduinoIDE/Modbus/Modbus.cpp index a590e8d..5468b5f 100644 --- a/arduinoIDE/Modbus/Modbus.cpp +++ b/arduinoIDE/Modbus/Modbus.cpp @@ -26,6 +26,7 @@ void Modbus::addReg(word address, word value) { TRegister *newreg; newreg = (TRegister *) malloc(sizeof(TRegister)); + if (!newreg) return; newreg->address = address; newreg->value = value; newreg->next = 0; @@ -176,6 +177,10 @@ void Modbus::exceptionResponse(byte fcode, byte excode) { free(_frame); _len = 2; _frame = (byte *) malloc(_len); + if (!_frame) { + _reply = MB_REPLY_OFF; + return; + } _frame[0] = fcode + 0x80; _frame[1] = excode; From b99ab26bf225db0faa1ec320a0792e10d882476c Mon Sep 17 00:00:00 2001 From: Alexander Emelianov Date: Fri, 22 Jun 2018 09:48:55 +0500 Subject: [PATCH 4/6] Fix wrong value for every 8-th reg in read/writeCoils, readInputStats --- .vscode/arduino.json | 6 +++ .vscode/c_cpp_properties.json | 78 +++++++++++++++++++++++++++++++++++ arduinoIDE/Modbus/Modbus.cpp | 27 ++++++------ 3 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 .vscode/arduino.json create mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/arduino.json b/.vscode/arduino.json new file mode 100644 index 0000000..5d5054e --- /dev/null +++ b/.vscode/arduino.json @@ -0,0 +1,6 @@ +{ + "board": "esp8266:esp8266:nodemcuv2", + "configuration": "CpuFrequency=80,FlashSize=4M1M,LwIPVariant=v2mss536,Debug=Serial,DebugLevel=None____,FlashErase=none,UploadSpeed=921600", + "port": "/dev/cu.SLAB_USBtoUART", + "sketch": "examples/Master/Master.ino" +} \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..0f232f7 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,78 @@ +{ + "configurations": [ + { + "name": "Mac", + "includePath": [ + "/usr/include", + "/usr/local/include", + "${workspaceRoot}" + ], + "defines": [], + "intelliSenseMode": "clang-x64", + "browse": { + "path": [ + "/usr/include", + "/usr/local/include", + "${workspaceRoot}" + ], + "limitSymbolsToIncludedHeaders": true, + "databaseFilename": "" + }, + "macFrameworkPath": [ + "/System/Library/Frameworks", + "/Library/Frameworks" + ], + "compilerPath": "/usr/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17" + }, + { + "name": "Linux", + "includePath": [ + "/usr/include", + "/usr/local/include", + "${workspaceRoot}" + ], + "defines": [], + "intelliSenseMode": "clang-x64", + "browse": { + "path": [ + "/usr/include", + "/usr/local/include", + "${workspaceRoot}" + ], + "limitSymbolsToIncludedHeaders": true, + "databaseFilename": "" + }, + "cStandard": "c11", + "cppStandard": "c++17" + }, + { + "name": "Win32", + "includePath": [ + "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino", + "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\avr\\include", + "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\lib\\gcc\\avr\\4.9.2\\include", + "C:\\Documents and Settings\\Alex\\Documents\\Arduino\\hardware\\espressif\\esp32\\libraries\\WiFi\\src", + "C:\\Documents and Settings\\Alex\\Local Settings\\Arduino15\\packages\\esp8266\\hardware\\esp8266\\2.4.0-rc2\\variants\\nodemcu", + "${workspaceRoot}" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "intelliSenseMode": "msvc-x64", + "browse": { + "path": [ + "${workspaceRoot}" + ], + "limitSymbolsToIncludedHeaders": true, + "databaseFilename": "" + }, + "cStandard": "c11", + "cppStandard": "c++17" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/arduinoIDE/Modbus/Modbus.cpp b/arduinoIDE/Modbus/Modbus.cpp index 5468b5f..1d8309c 100644 --- a/arduinoIDE/Modbus/Modbus.cpp +++ b/arduinoIDE/Modbus/Modbus.cpp @@ -329,17 +329,18 @@ void Modbus::readCoils(word startreg, word numregs) { _frame[1] = _len - 2; //byte count (_len - function code and byte count) byte bitn = 0; - word totregs = numregs; - word i; + word i = 0; while (numregs--) { - i = (totregs - numregs) / 8; if (this->Coil(startreg)) bitSet(_frame[2+i], bitn); else bitClear(_frame[2+i], bitn); //increment the bit index bitn++; - if (bitn == 8) bitn = 0; + if (bitn == 8) { + bitn = 0; + i++; + } //increment the register startreg++; } @@ -380,17 +381,18 @@ void Modbus::readInputStatus(word startreg, word numregs) { _frame[1] = _len - 2; byte bitn = 0; - word totregs = numregs; - word i; + word i = 0; while (numregs--) { - i = (totregs - numregs) / 8; if (this->Ists(startreg)) bitSet(_frame[2+i], bitn); else bitClear(_frame[2+i], bitn); //increment the bit index bitn++; - if (bitn == 8) bitn = 0; + if (bitn == 8) { + bitn = 0; + i++; + } //increment the register startreg++; } @@ -499,14 +501,15 @@ void Modbus::writeMultipleCoils(byte* frame,word startreg, word numoutputs, byte _frame[4] = numoutputs & 0x00FF; byte bitn = 0; - word totoutputs = numoutputs; - word i; + word i = 0; while (numoutputs--) { - i = (totoutputs - numoutputs) / 8; this->Coil(startreg, bitRead(frame[6+i], bitn)); //increment the bit index bitn++; - if (bitn == 8) bitn = 0; + if (bitn == 8) { + bitn = 0; + i++; + } //increment the register startreg++; } From 947fa26f3f653352fec18d3f245af0e1b533aa4d Mon Sep 17 00:00:00 2001 From: Alexander Emelianov Date: Fri, 22 Jun 2018 09:55:36 +0500 Subject: [PATCH 5/6] Delete arduino.json --- .vscode/arduino.json | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .vscode/arduino.json diff --git a/.vscode/arduino.json b/.vscode/arduino.json deleted file mode 100644 index 5d5054e..0000000 --- a/.vscode/arduino.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "board": "esp8266:esp8266:nodemcuv2", - "configuration": "CpuFrequency=80,FlashSize=4M1M,LwIPVariant=v2mss536,Debug=Serial,DebugLevel=None____,FlashErase=none,UploadSpeed=921600", - "port": "/dev/cu.SLAB_USBtoUART", - "sketch": "examples/Master/Master.ino" -} \ No newline at end of file From f9ad1449d4d1ffc7aebdc1fe2a82b4ca6b11d9c1 Mon Sep 17 00:00:00 2001 From: Alexander Emelianov Date: Fri, 22 Jun 2018 09:56:06 +0500 Subject: [PATCH 6/6] Delete c_cpp_properties.json --- .vscode/c_cpp_properties.json | 78 ----------------------------------- 1 file changed, 78 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 0f232f7..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,78 +0,0 @@ -{ - "configurations": [ - { - "name": "Mac", - "includePath": [ - "/usr/include", - "/usr/local/include", - "${workspaceRoot}" - ], - "defines": [], - "intelliSenseMode": "clang-x64", - "browse": { - "path": [ - "/usr/include", - "/usr/local/include", - "${workspaceRoot}" - ], - "limitSymbolsToIncludedHeaders": true, - "databaseFilename": "" - }, - "macFrameworkPath": [ - "/System/Library/Frameworks", - "/Library/Frameworks" - ], - "compilerPath": "/usr/bin/clang", - "cStandard": "c11", - "cppStandard": "c++17" - }, - { - "name": "Linux", - "includePath": [ - "/usr/include", - "/usr/local/include", - "${workspaceRoot}" - ], - "defines": [], - "intelliSenseMode": "clang-x64", - "browse": { - "path": [ - "/usr/include", - "/usr/local/include", - "${workspaceRoot}" - ], - "limitSymbolsToIncludedHeaders": true, - "databaseFilename": "" - }, - "cStandard": "c11", - "cppStandard": "c++17" - }, - { - "name": "Win32", - "includePath": [ - "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino", - "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\avr\\include", - "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr\\lib\\gcc\\avr\\4.9.2\\include", - "C:\\Documents and Settings\\Alex\\Documents\\Arduino\\hardware\\espressif\\esp32\\libraries\\WiFi\\src", - "C:\\Documents and Settings\\Alex\\Local Settings\\Arduino15\\packages\\esp8266\\hardware\\esp8266\\2.4.0-rc2\\variants\\nodemcu", - "${workspaceRoot}" - ], - "defines": [ - "_DEBUG", - "UNICODE", - "_UNICODE" - ], - "intelliSenseMode": "msvc-x64", - "browse": { - "path": [ - "${workspaceRoot}" - ], - "limitSymbolsToIncludedHeaders": true, - "databaseFilename": "" - }, - "cStandard": "c11", - "cppStandard": "c++17" - } - ], - "version": 4 -} \ No newline at end of file