Skip to content

fix: clamp lag parameter to buffer size with warning (fixes #379)#459

Merged
pradeeban merged 1 commit intoControlCore-Project:devfrom
GaneshPatil7517:fix/lag-validation-379
Feb 26, 2026
Merged

fix: clamp lag parameter to buffer size with warning (fixes #379)#459
pradeeban merged 1 commit intoControlCore-Project:devfrom
GaneshPatil7517:fix/lag-validation-379

Conversation

@GaneshPatil7517
Copy link

Summary

Fixes #379lag parameter in plotymlag.py silently wraps around when lag >= buffer size, producing incorrect data without any warning.

Root Cause

The circular buffer in plotymlag.py uses a fixed size = 10. The lag value is user-configurable via concore.tryparam("lag", 0). When a user requests lag >= size (e.g., lag = 15), the expression (cur - lag) % size silently wraps due to Python's modulo arithmetic, causing lag = 15 to behave identically to lag = 5. This produces wrong output data with no error or warning.

Fix

  • Added input validation immediately after lag is read from tryparam.
  • When lag >= size, the value is clamped to size - 1 (the maximum safe lag).
  • A logging.warning() is emitted to alert the user that their requested lag was out of range.
  • The existing circular buffer logic ((cur - lag) % size) remains unchanged.

Changes

  • tools/plotymlag.py: Added import logging and a validation block (6 net new lines).

Testing

Verified locally with multiple lag values:

Input lag Effective lag Warning emitted
0 0 No
5 5 No
9 9 No
10 9 Yes
15 9 Yes
100 9 Yes
  • Syntax check passes (py_compile).
  • Backward compatible: existing users with lag < 10 see no change in behavior.

Verification Steps for Reviewers

  1. Apply patch and run plotymlag.py with lag=0 → works as before.
  2. Run with lag=15 → observe warning message and clamped behavior (lag=9), no crash.
  3. Confirm (cur - lag) % size logic is untouched.
  4. Confirm no new dependencies are introduced (only stdlib logging).

…re-Project#379)

The circular buffer in plotymlag.py has a fixed size of 10. When a user
sets lag >= size via tryparam, the modulo arithmetic silently wraps,
producing incorrect data (e.g., lag=15 behaves like lag=5).

Add input validation that clamps lag to size-1 and emits a
logging.warning when the requested value exceeds the buffer capacity.
This prevents silent data corruption while preserving backward
compatibility and the existing circular buffer logic.
@pradeeban pradeeban merged commit e055f5e into ControlCore-Project:dev Feb 26, 2026
6 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.

2 participants