Make WordPress Core

Opened 11 years ago

Closed 10 years ago

Last modified 2 months ago

#36635 closed defect (bug) (fixed)

Issue with size_format function when value is 0

Reported by: bangelov Owned by: ocean90
Priority: normal Milestone: 4.6
Component: General Version: 2.3
Severity: normal Keywords: has-patch has-unit-tests commit
Cc: Focuses:

Description (last modified by swissspidy)

When you invoke size_format(0) function with zero value it should return 0 B instead it returns false which is wrong. For example when you are looping files and some of them is zero byte file it will return false and will break the UI if function is directly used in the view (most cases).

Attachments (3)

36635.diff​ (2.1 KB ) - added by swissspidy 11 years ago.
36635.2.diff​ (3.4 KB ) - added by swissspidy 11 years ago.
36635.3.diff​ (3.4 KB ) - added by swissspidy 10 years ago.

Download all attachments as: .zip

Change History (12)

#1 @swissspidy
11 years ago

  • Description modified (diff)

#2 @swissspidy
11 years ago

  • Keywords needs-patch needs-unit-tests added
  • Version 4.5 → 2.3

Seems legit to me. Will whip up a patch shortly.

#3 @swissspidy
11 years ago

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
  • Milestone Awaiting Review → 4.6

#4 @SergeyBiryukov
11 years ago

There are some size_format() tests in tests/functions.php, might be worth combining them.

Also related: #23626.

#5 @swissspidy
11 years ago

Thanks @SergeyBiryukov, totally missed those!

36635.2.diff​ combines the tests.

#6 @ocean90
10 years ago

  • Keywords needs-refresh added

#7 @swissspidy
10 years ago

  • Keywords commit added; needs-refresh removed

#8 @ocean90
10 years ago

  • Owner set to ocean90
  • Resolution → fixed
  • Status new → closed

In 37962:

General: Return "O B" when passing 0 to size_format().

Props swissspidy.
Fixes #36635.

#9 @westonruter
2 months ago

In 63461:

General: Recognize any numeric zero in size_format().

The zero shortcut used a strict 0 === $bytes comparison, so '0', '0.0' and 0.0 fell through to the magnitude loop and returned false while the integer 0 returned '0 B'. Normalize $bytes to a float once, up front, and compare it against 0.0 from then on. This treats every numeric spelling of zero alike, and also removes the repeated per-iteration cast the loop was doing along with resolving a PHPStan error.

Moving the cast up requires the value to be numeric, so non-numeric input now returns false before it rather than relying on the loop to carry it out the bottom. The documented type for $bytes is changes from int|string to int|float|numeric-string. That drops the site health call site from the argument.type baseline, where at level 5 it is no longer a wholly incompatible argument.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/7248.
Follow-up to r37962, r63024.

Props narenin, marian1, westonruter, martinkrcho, vrajadas, hellofromtonya, mukesh27.
See #36635.
Fixes #61930.

Note: See TracTickets for help on using tickets.