Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor size_t #11

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
291d6ff
refactoring using clang-format
hmueller01 Feb 28, 2025
f23b2a2
refactoring using clang-format
hmueller01 Feb 28, 2025
2115925
refactoring using clang-format
hmueller01 Feb 28, 2025
fe0fae5
refactoring using clang-format, fixed casting, fixed alignment of _ad…
hmueller01 Feb 28, 2025
0f42c08
refactoring using clang-format, fixed casting
hmueller01 Feb 28, 2025
5c068f0
refactoring using clang-format, fixed casting (errors with -Werror)
hmueller01 Feb 28, 2025
c7f6571
fixed type of millis variable
hmueller01 Feb 28, 2025
8e5da77
strict / pedantic checking resulting in error
hmueller01 Feb 28, 2025
77a204b
refactoring using clang-format, fixed casting (errors with -Werror), …
hmueller01 Feb 28, 2025
e4bed08
refactoring using size_t
hmueller01 Feb 28, 2025
ec8c9b7
refactoring using size_t, fixed potential buffer overflow, fixed cast…
hmueller01 Feb 28, 2025
cc6bc6c
added function declarations
hmueller01 Feb 28, 2025
9524e9d
added function declarations
hmueller01 Feb 28, 2025
f14c3a3
fixed function declarations
hmueller01 Feb 28, 2025
b46a29d
fixed function declarations
hmueller01 Feb 28, 2025
f0f3b8c
included tests folder
hmueller01 Feb 28, 2025
f5c7374
added Arduino Uno
hmueller01 Feb 28, 2025
7043424
clang code formatting
hmueller01 Feb 28, 2025
c6e2b54
changed output
hmueller01 Feb 28, 2025
9693ead
exclude esp8266 examples from Arduino Uno
hmueller01 Feb 28, 2025
5b26d17
exclude esp8266 examples from Arduino Uno
hmueller01 Feb 28, 2025
d4a993b
updated callback signature
hmueller01 Feb 28, 2025
24e5d0c
added -Wnull-dereference
hmueller01 Mar 1, 2025
c90e5cd
using static_cast<unsigned long>, optimized buildHeader(), size_t is …
hmueller01 Mar 1, 2025
6efe095
sorted flags, added -Wreorder
hmueller01 Mar 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/build-arduino.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ arduino-cli config set library.enable_unsafe_install true
arduino-cli core update-index
#arduino-cli core search
# Install Arduino cores
#arduino-cli core install arduino:avr
arduino-cli core install arduino:avr
arduino-cli core install esp8266:esp8266
arduino-cli core install esp32:esp32
#arduino-cli board listall
Expand All @@ -27,6 +27,15 @@ arduino-cli lib install --git-url https://github.com/ennui2342/arduino-sram
# Link the project to the Arduino library
ln -s $GITHUB_WORKSPACE $HOME/Arduino/libraries/CI_Test_Library

# Compile all *.ino files for the Arduino
for f in **/*.ino ; do
if [[ "$f" != *mqtt_esp8266.ino && "$f" != *mqtt_large_message.ino ]]; then
echo "################################################################"
echo "Arduino Uno compiling file ${f}"
arduino-cli compile -b arduino:avr:uno $f
fi
done

# Compile all *.ino files for the ESP32
for f in **/*.ino ; do
if [[ "$f" != *"mqtt_stream.ino" ]]; then
Expand Down
4 changes: 2 additions & 2 deletions .github/clang-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ cd $GITHUB_WORKSPACE
sudo apt-get -y install clang-format-19
# Check clang-format output
for f in **/*.{h,c,hpp,cpp,ino} ; do
if [ -f "$f" ] && [[ "$f" != "tests/"* ]]; then
#if [ -f "$f" ] && [[ "$f" != "tests/"* ]]; then
if [ -f "$f" ]; then
echo "################################################################"
echo "Checking file ${f}"
diff $f <(clang-format-19 -assume-filename=main.cpp $f) 1>&2
echo -e "################################################################\n"
fi
done
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void setup() {

void loop() {
if (!client.connected()) {
long now = millis();
unsigned long now = millis();
if (now - lastReconnectAttempt > 5000) {
lastReconnectAttempt = now;
// Attempt to reconnect
Expand Down
81 changes: 41 additions & 40 deletions src/PubSubClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c
if (result == 1) {
nextMsgId = 1;
// Leave room in the buffer for header and variable length field
uint16_t length = MQTT_MAX_HEADER_SIZE;
unsigned int j;
size_t length = MQTT_MAX_HEADER_SIZE;

#if MQTT_VERSION == MQTT_VERSION_3_1
uint8_t d[9] = {0x00, 0x06, 'M', 'Q', 'I', 's', 'd', 'p', MQTT_VERSION};
Expand All @@ -143,7 +142,7 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c
uint8_t d[7] = {0x00, 0x04, 'M', 'Q', 'T', 'T', MQTT_VERSION};
#define MQTT_HEADER_VERSION_LENGTH 7
#endif
for (j = 0; j < MQTT_HEADER_VERSION_LENGTH; j++) {
for (size_t j = 0; j < MQTT_HEADER_VERSION_LENGTH; j++) {
this->buffer[length++] = d[j];
}

Expand Down Expand Up @@ -193,7 +192,7 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c
while (!_client->available()) {
yield();
unsigned long t = millis();
if (t - lastInActivity >= ((int32_t)this->socketTimeout * 1000UL)) {
if (t - lastInActivity >= ((unsigned long)this->socketTimeout * 1000UL)) {
DEBUG_PSC_PRINTF("connect aborting due to timeout\n");
_state = MQTT_CONNECTION_TIMEOUT;
_client->stop();
Expand Down Expand Up @@ -225,11 +224,11 @@ bool PubSubClient::connect(const char* id, const char* user, const char* pass, c

// reads a byte into result
bool PubSubClient::readByte(uint8_t* result) {
uint32_t previousMillis = millis();
unsigned long previousMillis = millis();
while (!_client->available()) {
yield();
uint32_t currentMillis = millis();
if (currentMillis - previousMillis >= ((int32_t)this->socketTimeout * 1000)) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= ((unsigned long)this->socketTimeout * 1000)) {
return false;
}
}
Expand Down Expand Up @@ -330,7 +329,7 @@ bool PubSubClient::loop() {
}
if (_client->available()) {
uint8_t llen;
uint16_t len = readPacket(&llen);
uint32_t len = readPacket(&llen);
uint16_t msgId = 0;
uint8_t* payload;
if (len > 0) {
Expand Down Expand Up @@ -386,23 +385,22 @@ bool PubSubClient::publish(const char* topic, const char* payload, bool retained
return publish(topic, (const uint8_t*)payload, payload ? strnlen(payload, this->bufferSize) : 0, retained);
}

bool PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) {
bool PubSubClient::publish(const char* topic, const uint8_t* payload, size_t plength) {
return publish(topic, payload, plength, false);
}

bool PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength, bool retained) {
bool PubSubClient::publish(const char* topic, const uint8_t* payload, size_t plength, bool retained) {
if (connected()) {
if (this->bufferSize < MQTT_MAX_HEADER_SIZE + 2 + strnlen(topic, this->bufferSize) + plength) {
// Too long
return false;
}
// Leave room in the buffer for header and variable length field
uint16_t length = MQTT_MAX_HEADER_SIZE;
size_t length = MQTT_MAX_HEADER_SIZE;
length = writeString(topic, this->buffer, length);

// Add payload
unsigned int i;
for (i = 0; i < plength; i++) {
for (size_t i = 0; i < plength; i++) {
this->buffer[length++] = payload[i];
}

Expand All @@ -420,16 +418,15 @@ bool PubSubClient::publish_P(const char* topic, const char* payload, bool retain
return publish_P(topic, (const uint8_t*)payload, payload ? strnlen_P(payload, MQTT_MAX_POSSIBLE_PACKET_SIZE) : 0, retained);
}

bool PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned int plength, bool retained) {
bool PubSubClient::publish_P(const char* topic, const uint8_t* payload, size_t plength, bool retained) {
uint8_t llen = 0;
uint8_t digit;
unsigned int rc = 0;
uint16_t tlen;
unsigned int pos = 0;
unsigned int i;
size_t rc = 0;
size_t tlen;
size_t pos = 0;
uint8_t header;
unsigned int len;
unsigned int expectedLength;
size_t len;
size_t expectedLength;

if (!connected()) {
return false;
Expand Down Expand Up @@ -457,8 +454,8 @@ bool PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned

rc += _client->write(this->buffer, pos);

for (i = 0; i < plength; i++) {
rc += _client->write((char)pgm_read_byte_near(payload + i));
for (size_t i = 0; i < plength; i++) {
rc += _client->write((uint8_t)pgm_read_byte_near(payload + i));
}

lastOutActivity = millis();
Expand All @@ -468,17 +465,17 @@ bool PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned
return (rc == expectedLength);
}

bool PubSubClient::beginPublish(const char* topic, unsigned int plength, bool retained) {
bool PubSubClient::beginPublish(const char* topic, size_t plength, bool retained) {
if (connected()) {
// Send the header and variable length field
uint16_t length = MQTT_MAX_HEADER_SIZE;
size_t length = MQTT_MAX_HEADER_SIZE;
length = writeString(topic, this->buffer, length);
uint8_t header = MQTTPUBLISH;
if (retained) {
header |= 1;
}
size_t hlen = buildHeader(header, this->buffer, plength + length - MQTT_MAX_HEADER_SIZE);
uint16_t rc = _client->write(this->buffer + (MQTT_MAX_HEADER_SIZE - hlen), length - (MQTT_MAX_HEADER_SIZE - hlen));
size_t rc = _client->write(this->buffer + (MQTT_MAX_HEADER_SIZE - hlen), length - (MQTT_MAX_HEADER_SIZE - hlen));
lastOutActivity = millis();
return (rc == (length - (MQTT_MAX_HEADER_SIZE - hlen)));
}
Expand All @@ -499,12 +496,12 @@ size_t PubSubClient::write(const uint8_t* buffer, size_t size) {
return _client->write(buffer, size);
}

size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, uint16_t length) {
size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, size_t length) {
uint8_t lenBuf[4];
uint8_t llen = 0;
uint8_t digit;
uint8_t pos = 0;
uint16_t len = length;
size_t len = length;
do {
digit = len & 127; // digit = len % 128
len >>= 7; // len = len / 128
Expand All @@ -513,23 +510,27 @@ size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, uint16_t length)
}
lenBuf[pos++] = digit;
llen++;
} while (len > 0);
} while (len > 0 && pos < 4);

if (len > 0) {
DEBUG_PSC_PRINTF("length too big %u, left %u, should be 0\r\n", length, len);
}

buf[4 - llen] = header;
for (int i = 0; i < llen; i++) {
for (uint8_t i = 0; i < llen; i++) {
buf[MQTT_MAX_HEADER_SIZE - llen + i] = lenBuf[i];
}
return llen + 1; // Full header size is variable length bit plus the 1-byte fixed header
}

bool PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
uint16_t rc;
uint8_t hlen = buildHeader(header, buf, length);
bool PubSubClient::write(uint8_t header, uint8_t* buf, size_t length) {
size_t rc;
size_t hlen = buildHeader(header, buf, length);

#ifdef MQTT_MAX_TRANSFER_SIZE
uint8_t* writeBuf = buf + (MQTT_MAX_HEADER_SIZE - hlen);
uint16_t bytesRemaining = length + hlen; // Match the length type
uint16_t bytesToWrite;
size_t bytesRemaining = length + hlen; // Match the length type
size_t bytesToWrite;
bool result = true;
while ((bytesRemaining > 0) && result) {
yield();
Expand Down Expand Up @@ -572,7 +573,7 @@ bool PubSubClient::subscribe(const char* topic, uint8_t qos) {
}
this->buffer[length++] = (nextMsgId >> 8);
this->buffer[length++] = (nextMsgId & 0xFF);
length = writeString((char*)topic, this->buffer, length);
length = writeString(topic, this->buffer, length);
this->buffer[length++] = qos;
return write(MQTTSUBSCRIBE | MQTTQOS1, this->buffer, length - MQTT_MAX_HEADER_SIZE);
}
Expand Down Expand Up @@ -613,12 +614,12 @@ void PubSubClient::disconnect() {
lastInActivity = lastOutActivity = millis();
}

uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t pos) {
size_t PubSubClient::writeString(const char* string, uint8_t* buf, size_t pos) {
const char* idp = string;
uint16_t i = 0;
size_t i = 0;
pos += 2;
while (*idp) {
buf[pos++] = *idp++;
buf[pos++] = (uint8_t)*idp++;
i++;
}
buf[pos - i - 2] = (i >> 8);
Expand Down Expand Up @@ -683,7 +684,7 @@ int PubSubClient::state() {
return this->_state;
}

bool PubSubClient::setBufferSize(uint16_t size) {
bool PubSubClient::setBufferSize(size_t size) {
if (size == 0) {
// Cannot set it back to 0
return false;
Expand All @@ -702,7 +703,7 @@ bool PubSubClient::setBufferSize(uint16_t size) {
return (this->buffer != NULL);
}

uint16_t PubSubClient::getBufferSize() {
size_t PubSubClient::getBufferSize() {
return this->bufferSize;
}

Expand Down
24 changes: 12 additions & 12 deletions src/PubSubClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@
* @brief Define the signature required by any callback function.
* @note The parameters are TOPIC, PAYLOAD, and LENGTH, respectively.
*/
#define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
#define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, size_t)> callback
#else
#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, size_t)
#endif

#define CHECK_STRING_LENGTH(l, s) \
Expand All @@ -136,7 +136,7 @@ class PubSubClient : public Print {
private:
Client* _client;
uint8_t* buffer;
uint16_t bufferSize;
size_t bufferSize;
uint16_t keepAlive;
uint16_t socketTimeout;
uint16_t nextMsgId;
Expand All @@ -147,13 +147,13 @@ class PubSubClient : public Print {
uint32_t readPacket(uint8_t*);
bool readByte(uint8_t* result);
bool readByte(uint8_t* result, uint16_t* index);
bool write(uint8_t header, uint8_t* buf, uint16_t length);
uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
bool write(uint8_t header, uint8_t* buf, size_t length);
size_t writeString(const char* string, uint8_t* buf, size_t pos);
// Build up the header ready to send
// Returns the size of the header
// Note: the header is built at the end of the first MQTT_MAX_HEADER_SIZE bytes, so will start
// (MQTT_MAX_HEADER_SIZE - <returned size>) bytes into the buffer
size_t buildHeader(uint8_t header, uint8_t* buf, uint16_t length);
size_t buildHeader(uint8_t header, uint8_t* buf, size_t length);
IPAddress ip;
const char* domain;
uint16_t port;
Expand Down Expand Up @@ -367,13 +367,13 @@ class PubSubClient : public Print {
* @return true If the buffer was resized.
* false If the buffer could not be resized.
*/
bool setBufferSize(uint16_t size);
bool setBufferSize(size_t size);

/**
* @brief Gets the current size of the internal buffer.
* @return The size of the internal buffer.
*/
uint16_t getBufferSize();
size_t getBufferSize();

/**
* @brief Connects the client.
Expand Down Expand Up @@ -464,7 +464,7 @@ class PubSubClient : public Print {
* @return true If the publish succeeded.
* false If the publish failed, either connection lost or message too large.
*/
bool publish(const char* topic, const uint8_t* payload, unsigned int plength);
bool publish(const char* topic, const uint8_t* payload, size_t plength);

/**
* @brief Publishes a message to the specified topic.
Expand All @@ -475,7 +475,7 @@ class PubSubClient : public Print {
* @return true If the publish succeeded.
* false If the publish failed, either connection lost or message too large.
*/
bool publish(const char* topic, const uint8_t* payload, unsigned int plength, bool retained);
bool publish(const char* topic, const uint8_t* payload, size_t plength, bool retained);

/**
* @brief Publishes a message stored in PROGMEM to the specified topic.
Expand All @@ -496,7 +496,7 @@ class PubSubClient : public Print {
* @return true If the publish succeeded.
* false If the publish failed, either connection lost or message too large.
*/
bool publish_P(const char* topic, const uint8_t* payload, unsigned int plength, bool retained);
bool publish_P(const char* topic, const uint8_t* payload, size_t plength, bool retained);

/**
* @brief Start to publish a message.
Expand All @@ -512,7 +512,7 @@ class PubSubClient : public Print {
* @return true If the publish succeeded.
* false If the publish failed, either connection lost or message too large.
*/
bool beginPublish(const char* topic, unsigned int plength, bool retained);
bool beginPublish(const char* topic, size_t plength, bool retained);

/**
* @brief Finish sending a message that was started with a call to beginPublish.
Expand Down
3 changes: 3 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ SHIM_FILES=${SRC_PATH}/lib/*.cpp
PSC_FILE=../src/PubSubClient.cpp
CC=g++
CFLAGS=-std=c++11 -I${SRC_PATH}/lib -I../src
CFLAGS+=-pedantic -Werror -Wall -Wextra -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2
CFLAGS+=-Winit-self -Wmissing-declarations -Wmissing-include-dirs -Woverloaded-virtual -Wredundant-decls
CFLAGS+=-Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch-default -Wundef -Wno-unused

all: $(TEST_BIN)

Expand Down
Loading