diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/MTS_IO_CellularRadio.cpp | 31 | ||||
-rw-r--r-- | src/MTS_IO_TelitRadio.cpp | 8 |
2 files changed, 25 insertions, 14 deletions
diff --git a/src/MTS_IO_CellularRadio.cpp b/src/MTS_IO_CellularRadio.cpp index 19f1b7b..49a2b32 100644 --- a/src/MTS_IO_CellularRadio.cpp +++ b/src/MTS_IO_CellularRadio.cpp @@ -21,6 +21,7 @@ #include "mts/MTS_IO_CellularRadio.h" #include <unistd.h> +#include <sys/stat.h> #include <mts/MTS_IO_MccMncTable.h> #include <mts/MTS_Thread.h> @@ -1138,10 +1139,21 @@ ICellularRadio::CODE CellularRadio::sendData(const char* pData, size_t nBytes) { return ERROR; } + // This limit comes from the Connection::write implementation. Otherwise we can get overflows. + const size_t maxInt32 = INT32_MAX; // iSize parameter type in Connection::write + const size_t maxUInt32 = UINT32_MAX; // return value type in Connection::write + const size_t nSizeLimit = std::min(maxInt32, maxUInt32); + + if (nBytes > nSizeLimit) { + printError("RADIO| Chunks larger than %d bytes are not supported", nSizeLimit); + return INVALID_ARGS; + } + + // Now we can ignore conversion and overflow warnings emitted by compiler. int32_t iResult; iResult = m_apIo->write(pData, nBytes); - if(iResult == -1) { + if(iResult != static_cast<int32_t>(nBytes)) { printError("RADIO| Failed to send data to radio"); return ERROR; } @@ -1272,21 +1284,20 @@ ICellularRadio::CODE CellularRadio::getFileSize(int fd, size_t& nBytes) { CODE rc = FAILURE; do { - ssize_t dScrollPos; - dScrollPos = lseek(fd, 0, SEEK_SET); - if (dScrollPos < 0) { - printError("Failed to seek to the start of the file: %d", errno); + struct stat fileStatus; + + // On success, zero is returned. On error, -1 is returned, and errno is set appropriately. + if (fstat(fd, &fileStatus) < 0) { + printError("Failed to determine file size: %d", errno); break; } - dScrollPos = lseek(fd, 0, SEEK_END); - if (dScrollPos < 0) { - printError("Failed to determine the file size: %d", errno); + if (fileStatus.st_size < 0) { + printError("Failed to determine file size, file size is negative: %d", fileStatus.st_size); break; } - nBytes = static_cast<size_t>(dScrollPos); - + nBytes = static_cast<size_t>(fileStatus.st_size); rc = SUCCESS; } while (false); diff --git a/src/MTS_IO_TelitRadio.cpp b/src/MTS_IO_TelitRadio.cpp index a82004c..8c7e174 100644 --- a/src/MTS_IO_TelitRadio.cpp +++ b/src/MTS_IO_TelitRadio.cpp @@ -943,7 +943,7 @@ ICellularRadio::CODE TelitRadio::fumoWriteGroupsABD(int fd, ICellularRadio::Upda printTrace("File size: %d bytes and %d chunks", dPayloadLength, nChunks); printTrace("Starting file upload..."); - rc = startWrite(); + rc = startFotaWriteABD(); if (rc != SUCCESS) { return rc; } @@ -980,12 +980,12 @@ ICellularRadio::CODE TelitRadio::fumoWriteGroupsABD(int fd, ICellularRadio::Upda } // send +++ - abortWrite(); + abortFotaWriteABD(); return rc; } -ICellularRadio::CODE TelitRadio::startWrite() { +ICellularRadio::CODE TelitRadio::startFotaWriteABD() { const std::vector<std::string> vBailStrings{ ICellularRadio::RSP_CONNECT, ICellularRadio::RSP_ERROR }; const int dTimeout = 1000; //ms std::string sCommand, sResult; @@ -1001,7 +1001,7 @@ ICellularRadio::CODE TelitRadio::startWrite() { return SUCCESS; } -ICellularRadio::CODE TelitRadio::abortWrite() { +ICellularRadio::CODE TelitRadio::abortFotaWriteABD() { /* * To prevent the “+++” from being mistaken for data, the following sequence should be followed: * 1) Do not input any character within 1s or longer before inputting “+++”. |