summaryrefslogtreecommitdiff
path: root/src/MTS_IO_CellularRadio.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/MTS_IO_CellularRadio.cpp')
-rw-r--r--src/MTS_IO_CellularRadio.cpp31
1 files changed, 21 insertions, 10 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);