Opened 6 weeks ago
Last modified 3 weeks ago
#61930 new defect (bug)
size_format(): Inconsistent behaviour when zero bytes are passed
Reported by: | marian1 | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | trivial | Version: | 4.6 |
Component: | General | Keywords: | |
Focuses: | Cc: |
Description
The size_format()
function returns false
if zero is passed as a string and returns '0 B'
if zero is passed as an integer.
The root cause is a strict comparison when checking against zero (if ( 0 === $bytes ) {
). If the $bytes
parameter is passed as a string ('0'
), the condition fails.
Proposed Fix: if ( 0 === $bytes || '0' === $bytes ) {
This behaviour was introduced in WP 4.6.0. Before v4.6.0, both 0
and '0'
resulted in size_format()
returning false
.
Change History (5)
This ticket was mentioned in PR #7248 on WordPress/wordpress-develop by @narenin.
6 weeks ago
#1
- Keywords has-patch added
#2
@
6 weeks ago
- Keywords has-patch removed
Hi @marian1
Welcome to the Core Trac!
Thanks for the detailed information.
I have added the patch for the same.
#3
@
5 weeks ago
This issue is indeed rooted in the strict comparison (===) used in the size_format() function. The strict comparison checks not only the value but also the type, so 0 === $bytes fails when $bytes is a string ('0').
Explanation:
Before WP 4.6.0: Both 0 (integer) and '0' (string) would cause size_format() to return false because the condition likely used a loose comparison (==), which checks only the value and not the type.
From WP 4.6.0 onwards: The strict comparison was introduced, so 0 === $bytes only returns true when $bytes is strictly an integer 0. If $bytes is a string '0', the condition fails, and the function continues processing, eventually returning '0 B' instead of false.
#4
@
5 weeks ago
To handle both cases (integer 0 and string '0'), the proposed fix checks for both explicitly:
if ( 0 === $bytes || '0' === $bytes ) {
<?php if ( 0 === $bytes || '0' === $bytes ) {
Impact:
With the Fix: Both 0 (integer) and '0' (string) will result in the function returning false, restoring the behavior before WP 4.6.0.
Without the Fix: The function behaves inconsistently, depending on whether 0 is passed as an integer or a string.
This fix aligns with the intended behavior of the function, ensuring that both 0 and '0' are treated equivalently.
#5
@
3 weeks ago
- Version changed from 6.6.1 to 4.6
Trac ticket: https://core.trac.wordpress.org/ticket/61930