Skip to content

fix: clamp convert_size_bytes_to_string at largest unit (#184)#185

Open
patchwright wants to merge 1 commit into
fabiocaccamo:mainfrom
patchwright:fix/size-converter-yb-overflow
Open

fix: clamp convert_size_bytes_to_string at largest unit (#184)#185
patchwright wants to merge 1 commit into
fabiocaccamo:mainfrom
patchwright:fix/size-converter-yb-overflow

Conversation

@patchwright

Copy link
Copy Markdown

Describe your changes

Fixes #184. convert_size_bytes_to_string() raises IndexError for any size of 1024 YB or more (2**90 bytes and up). Confirmed on main HEAD c98dbf6.

Root cause: the unit-selection loop guard is factor <= factor_limit. When factor reaches the last index (YB) and the value is still >= 1024, the loop runs once more and increments factor past the end of SIZE_UNITS, so units[factor] is out of range.

Fix: change the guard to factor < factor_limit so the loop stops at the largest unit and the size saturates in YB. One line changed. Sizes below 2**90 are unaffected (byte-identical output).

Related issue

#184

How to test

Before this PR:

>>> convert_size_bytes_to_string(2**90)
IndexError: list index out of range

After this PR:

>>> convert_size_bytes_to_string(2**90)
'1024.00 YB'

Added a parametrized regression test (280, 290, 2**100). Full converter suite: 32 passed.

Backward compatibility

No breaking changes. Only inputs that previously raised IndexError change behavior.

Checklist before requesting a review

  • I have performed a self-review of my code.
  • I have added tests for the proposed changes.
  • I have run the tests and there are not errors.

Assisted-by: Claude (code generation, reviewed and tested locally)

@codecov

codecov Bot commented Jun 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.33%. Comparing base (c98dbf6) to head (4cc8abe).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #185   +/-   ##
=======================================
  Coverage   98.33%   98.33%           
=======================================
  Files          13       13           
  Lines         780      780           
=======================================
  Hits          767      767           
  Misses         13       13           
Flag Coverage Δ
unittests 98.33% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

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.

convert_size_bytes_to_string raises IndexError for sizes >= 1024 YB

1 participant