Skip to content

[ld2420] Fix sizeof vs value bug in register memcpy#14286

Merged
swoboda1337 merged 1 commit intoesphome:devfrom
swoboda1337:fix-ld2420-sizeof-bug
Feb 25, 2026
Merged

[ld2420] Fix sizeof vs value bug in register memcpy#14286
swoboda1337 merged 1 commit intoesphome:devfrom
swoboda1337:fix-ld2420-sizeof-bug

Conversation

@swoboda1337
Copy link
Member

What does this implement/fix?

Fixes a bug where sizeof(CMD_REG_DATA_REPLY_SIZE) was used instead of CMD_REG_DATA_REPLY_SIZE as the memcpy length at three call sites.

CMD_REG_DATA_REPLY_SIZE is a static constexpr uint8_t = 0x02, so sizeof() returns 1 (size of uint8_t) instead of the intended value of 2. This causes memcpy to copy only 1 byte instead of 2 for register read/write operations.

Affected sites:

  • handle_ack_data_() line 593: reading register reply data
  • set_reg_value() lines 732, 734: writing register address and value

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Developer breaking change (an API change that could break external components)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

N/A

Pull request in esphome-docs with documentation (if applicable):

N/A

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • LN882x
  • nRF52840

Example entry for config.yaml:

# No config changes needed - internal bug fix
ld2420:

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

sizeof(CMD_REG_DATA_REPLY_SIZE) evaluates to sizeof(uint8_t) = 1,
but the intended copy size is the value of CMD_REG_DATA_REPLY_SIZE
which is 2. This caused register read/write commands to only copy
1 byte instead of 2, corrupting the data.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 25, 2026 19:06
@github-actions
Copy link
Contributor

To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file:

external_components:
  - source: github://pr#14286
    components: [ld2420]
    refresh: 1h

(Added by the PR bot)

@github-actions
Copy link
Contributor

👋 Hi there! This PR modifies 1 file(s) with codeowners.

@descipher - As codeowner(s) of the affected files, your review would be appreciated! 🙏

Note: Automatic review request may have failed, but you're still welcome to review.

((buffer[CMD_FRAME_DATA_LENGTH] - 4) / CMD_REG_DATA_REPLY_SIZE));
index += CMD_REG_DATA_REPLY_SIZE) {
memcpy(&this->cmd_reply_.data[reg_element], &buffer[data_pos + index], sizeof(CMD_REG_DATA_REPLY_SIZE));
memcpy(&this->cmd_reply_.data[reg_element], &buffer[data_pos + index], CMD_REG_DATA_REPLY_SIZE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that sure looks wrong

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, it looks wrong, and it is wrong.

@codecov-commenter
Copy link

codecov-commenter commented Feb 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.28%. Comparing base (8bb577d) to head (e3cfec8).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev   #14286   +/-   ##
=======================================
  Coverage   74.28%   74.28%           
=======================================
  Files          55       55           
  Lines       11595    11595           
  Branches     1583     1583           
=======================================
  Hits         8613     8613           
  Misses       2578     2578           
  Partials      404      404           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdraco bdraco added this to the 2026.2.2 milestone Feb 25, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an LD2420 register read/write framing bug where sizeof(CMD_REG_DATA_REPLY_SIZE) (1 byte) was mistakenly used instead of the constant value (2 bytes), causing truncated memcpy operations during register access.

Changes:

  • Correct memcpy lengths in handle_ack_data_() when parsing register read reply data.
  • Correct memcpy lengths in set_reg_value() when serializing the register address and value.

@swoboda1337
Copy link
Member Author

Thanks

@swoboda1337 swoboda1337 enabled auto-merge (squash) February 25, 2026 19:18
@swoboda1337 swoboda1337 merged commit 62da60d into esphome:dev Feb 25, 2026
43 checks passed
jesserockz pushed a commit that referenced this pull request Feb 26, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jesserockz jesserockz mentioned this pull request Feb 26, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Feb 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants