Skip to content

Fix char array setValue() using buffer size instead of string length#1134

Merged
h2zero merged 1 commit intomasterfrom
copilot/fix-wrong-message-length-calculation
Mar 29, 2026
Merged

Fix char array setValue() using buffer size instead of string length#1134
h2zero merged 1 commit intomasterfrom
copilot/fix-wrong-message-length-calculation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 29, 2026

When a char[] array is passed to setValue(), template deduction matches T = char[N] as an exact reference bind — beating the const char* overload that requires array-to-pointer decay. The template's fallback then uses sizeof(T) (the full buffer size) instead of strlen(), causing oversized transmissions or the value exceeds max error.

Changes

  • src/NimBLEAttValue.h — pre-C++17 path: Added a new enable_if template overload specifically for char[] arrays (detected via std::is_array + std::is_same<std::remove_extent_t<T>, char>). Uses strnlen(s, sizeof(T)) to cap at the buffer boundary. Updated the existing generic template's enable_if condition to exclude char arrays, keeping the two overloads mutually exclusive.

  • src/NimBLEAttValue.h — C++17 path: Added an else if constexpr branch in the if constexpr chain to handle char arrays before the final sizeof fallback.

Example

char str[45];
sprintf(str, "12345678901234567890A");  // 21 chars
characteristic->setValue(str);
// Before: sent 45 bytes (full buffer)
// After:  sent 21 bytes (strlen of content)

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Copilot AI linked an issue Mar 29, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix NimBLECharacteristic::setValue() message length calculation Fix char array setValue() using buffer size instead of string length Mar 29, 2026
Copilot AI requested a review from h2zero March 29, 2026 22:25
@h2zero h2zero force-pushed the copilot/fix-wrong-message-length-calculation branch from a220ff5 to 76561ae Compare March 29, 2026 22:49
@h2zero h2zero marked this pull request as ready for review March 29, 2026 22:59
@h2zero h2zero merged commit 8370464 into master Mar 29, 2026
41 checks passed
@h2zero h2zero deleted the copilot/fix-wrong-message-length-calculation branch March 29, 2026 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants