WordPress.org

Make WordPress Core

Opened 14 months ago

Closed 14 months ago

Last modified 12 months ago

#23626 closed defect (bug) (fixed)

wp_convert_bytes_to_hr tests and NaN

Reported by: soulseekah Owned by: SergeyBiryukov
Milestone: 3.6 Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch
Focuses: Cc:

Description

[1218/tests] were written for the deprecated wp_convert_bytes_to_hr, these tests are broken at least on 64-bit machines, at most - on all machines.

On 64-bit PHP (PHP_INT_SIZE == 8) floating point does not match expected output (gets 1022.9990234375MB). The last digits in floating point should not be depended upon due to these inaccuracies. Rounding in PHP < 5.3 is not an option either, since rounding mode was introduced later and different systems get different results (round up vs. round down by default). So perhaps looking at error and discrepancy would be a decent solution (maybe even less error 0.00001?).

Next [23440], in the function itself we get (int)double(NAN) result from feeding -1 and 0, which results in $power to be a huge negative number instead of 0. This results in both a Warning (undefined array index) being thrown, and the "B" missing in tests, i.e. expected "NANB" != "NAN" and expected "0B" != "0".

Related #19067

Attachments (6)

19067.function.patch (449 bytes) - added by soulseekah 14 months ago.
test_19067.patch (748 bytes) - added by soulseekah 14 months ago.
23626.patch (594 bytes) - added by SergeyBiryukov 14 months ago.
23626.test.patch (1.1 KB) - added by SergeyBiryukov 14 months ago.
23626.2.patch (781 bytes) - added by SergeyBiryukov 14 months ago.
23626.test.2.patch (1.2 KB) - added by SergeyBiryukov 14 months ago.

Download all attachments as: .zip

Change History (17)

soulseekah14 months ago

comment:1 kovshenin14 months ago

  • Cc kovshenin added
  • Keywords has-patch added

comment:2 SergeyBiryukov14 months ago

  • Milestone changed from Awaiting Review to 3.6

#12915 suggested rounding the output to 2 decimal digits.

#22295 also mentioned the notice.

comment:3 soulseekah14 months ago

See: http://www.php.net/manual/en/function.round.php#98704 for PHP < 5.3 round direction is not predictable it seems.

comment:4 SergeyBiryukov14 months ago

In 1220/tests:

Fix wp_convert_bytes_to_hr() test for 64-bit systems. props soulseekah. see #23626.

comment:5 SergeyBiryukov14 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 23502:

Avoid an undefined offset notice in wp_convert_bytes_to_hr(). props soulseekah. fixes #23626.

SergeyBiryukov14 months ago

comment:6 SergeyBiryukov14 months ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Still getting different results:

  • 32-bit, wp_convert_bytes_to_hr( -1 ): NANB
  • 64-bit, wp_convert_bytes_to_hr( -1 ): -1B

I didn't commit 19067.function.patch as is, because for 1 terabyte, the function would return "1B" (before [23502] it would be "1" and a notice).

Testing 23626.patch and 23626.test.patch now.

comment:7 soulseekah14 months ago

Perhaps it should return false for all sorts of weirdness?

Version 0, edited 14 months ago by soulseekah (next)

SergeyBiryukov14 months ago

comment:8 SergeyBiryukov14 months ago

Replying to soulseekah:

Perhaps it should return false for all sorts of weirdness? Or 0B.

It's supposed to return a string. It also does not state that $bytes should be positive, so for "-1", I guess "-1B" is a more appropriate result than "0B".

We could also add "TB" to the array: 23626.2.patch, 23626.test.2.patch.

So far getting consistent results with the latest patches.

comment:9 SergeyBiryukov14 months ago

In 1226/tests:

Add more assertions for wp_convert_bytes_to_hr(). Correct expected result for a negative number. see #23626.

comment:10 SergeyBiryukov14 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 23551:

Make wp_convert_bytes_to_hr() return consistent results on 32-bit and 64-bit systems. fixes #23626.

comment:11 SergeyBiryukov12 months ago

In 1270/tests:

Fix wp_convert_bytes_to_hr() test for decimal comma locales. see #23626.

Note: See TracTickets for help on using tickets.