Skip to content

NimBLECharacteristic::setValue() is calculating the wrong message length. #562

@orbian

Description

@orbian

I am programming and ESP32 with platform.io.

NimBLECharacteristic::setValue(const char *s) is setting the attribute size to the entire size of the buffer, not just the '\0' terminated string.

For example this does the correct thing and sends 22 bytes to the client (string data plus null terminator):

uint16_t cap;
std::string value;

outgoingCharateristic->setValue("12345678901234567890A");
cap = outgoingCharateristic->getValue().capacity();
printf("Capacity: %d\n", cap);
value = outgoingCharateristic->getValue();
printf("%s\n", value.c_str());

The server prints out:

Capacity: 22
12345678901234567890A

That's what the client receives as well.

But if I declare a char buffer and fill it in, what gets sent is the entire buffer, no matter how large it is.

char str[45];
uint16_t cap;
std::string value;

sprintf(str, "12345678901234567890A");
outgoingCharateristic->setValue(str);
cap = outgoingCharateristic->getValue().capacity();
printf("Capacity: %d\n", cap);
value = outgoingCharateristic->getValue();
printf("%s\n", value.c_str());

In this case the server prints:

Capacity: 45
12345678901234567890A

My client receives a 45 byte buffer and thinks there is more data than there really is. It's still the null terminated string but followed with garbage.

Changing the buffer to char str[1500] causes and error.

E NimBLEAttValue: value exceeds max, len=1500, max=512

setValue() uses strlen() to determine the length of the data so it shouldn't give a value that exceeds the null termination. If I strlen() the string before passing it to setValue() it does report the correct value. However once inside setValue(), only the buffer length is returned.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions