Skip to content

Fix delete/delete[] mismatches and off-by-one null terminator - #77

Merged
davidchatting merged 1 commit into
interactionresearchstudio:masterfrom
davidchatting-bot:fix/delete-array-mismatches
Aug 5, 2026
Merged

Fix delete/delete[] mismatches and off-by-one null terminator#77
davidchatting merged 1 commit into
interactionresearchstudio:masterfrom
davidchatting-bot:fix/delete-array-mismatches

Conversation

@davidchatting-bot

Copy link
Copy Markdown
Contributor

Fixes #57, #58, #59.

#57 - off-by-one null terminator

In `onYoYoRequestPOST()`, after the copy loop `i == len` (clamped to at most 1023), so `json[i+1] = '\0';` wrote one byte past the end of the 1024-byte buffer. Changed to `json[i] = '\0';`.

#58 - delete vs delete[]

Every `new char[...]`/`new uint8_t[...]` allocation in `src/YoYoWiFiManager.cpp` was freed with plain `delete` instead of `delete[]` - undefined behaviour in C++ regardless of element type. Fixed all 16 live sites (there are two more inside `onYoYoRequestDELETE()`'s already-commented-out dead code, left untouched since #69 covers reimplementing that function properly rather than patching dead code).

Single-object allocations (`new IPAddress()`, `new IPAddress(WiFi.softAPIP())`, etc.) were already correctly freed with plain `delete` and are unchanged.

#59 - comma-operator leak

Four of the above sites used `delete a, b;`, which (comma operator) only deletes `a` - `b` is evaluated and discarded, silently leaking every time:

  • `addKnownNetworks()`
  • `loop()` (on every status change)
  • `updateMode()` (on every mode change)
  • `getCredentialsAsJson()` (every `GET /yoyo/credentials`)

Split into separate `delete[]` statements at each site.

Testing

Not hardware-tested (no ESP8266/ESP32 device in this environment) - purely mechanical corrections to string-copy/deallocation call sites, no control-flow changes.

Fixes interactionresearchstudio#57, interactionresearchstudio#58, interactionresearchstudio#59.

- onYoYoRequestPOST(): json[i+1] = '\0' wrote one byte past the
  1024-byte buffer after the copy loop (i == len at that point) -
  changed to json[i].
- Every new char[]/new uint8_t[] allocation in this file was freed
  with plain delete instead of delete[] - undefined behaviour even
  for POD element types. Fixed all 16 live sites.
- Four of those were also using the comma operator (delete a, b;),
  which only deletes a and silently leaks b - split into separate
  delete[] statements at each site (addKnownNetworks(), loop(),
  updateMode(), getCredentialsAsJson()).

Single object allocations (new IPAddress(), new IPAddress(...)) were
left as plain delete, which is already correct for those.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@davidchatting
davidchatting merged commit 80f87ce into interactionresearchstudio:master Aug 5, 2026
1 of 2 checks passed
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.

Off-by-one buffer overflow parsing POST body in onYoYoRequestPOST()

2 participants