Skip to content

Fix out-of-bounds access in setFunctionCode()#412

Merged
Miq1 merged 1 commit into
eModbus:masterfrom
KOMAD-bit:fix/setFunctionCode-resize-instead-of-reserve
Jun 5, 2025
Merged

Fix out-of-bounds access in setFunctionCode()#412
Miq1 merged 1 commit into
eModbus:masterfrom
KOMAD-bit:fix/setFunctionCode-resize-instead-of-reserve

Conversation

@KOMAD-bit

Copy link
Copy Markdown
Contributor

The setFunctionCode() method incorrectly uses reserve() instead of resize() to prepare the vector MM_data for writing at indices 0 and 1.

reserve() only allocates memory but does not change the vector size, so accessing MM_data[0] or MM_data[1] causes undefined behavior when the vector is empty or too small.

This commit changes reserve() to resize() to ensure valid indices exist before writing to MM_data, preventing out-of-bounds writes and potential crashes.

The setFunctionCode() method incorrectly uses reserve() instead of resize()
to prepare the vector MM_data for writing at indices 0 and 1.

reserve() only allocates memory but does not change the vector size,
so accessing MM_data[0] or MM_data[1] causes undefined behavior
when the vector is empty or too small.

This commit changes reserve() to resize() to ensure valid indices exist
before writing to MM_data, preventing out-of-bounds writes and potential crashes.
@Miq1

Miq1 commented Jun 5, 2025

Copy link
Copy Markdown
Contributor

Your observation is correct, I missed the point that using operator[] still is an out-of-bounds access even if the memory had been allocated with reserve().

There are more spots where I used reserve(), so I will have to check there as well...

@Miq1 Miq1 merged commit 669078b into eModbus:master Jun 5, 2025
14 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.

3 participants