Details
-
Bug
-
Status: In Review (View Workflow)
-
Critical
-
Resolution: Unresolved
-
11.4, 13.3
Description
FORMAT_BYTES() and FORMAT_PICO_TIME() print large values with %4.2e. For a negative value whose exponent has three digits, the text is exactly as long as the fixed result buffer, so snprintf() has to put its terminating NUL where the last character goes, and the function returns that NUL byte as part of the result:
- FORMAT_BYTES(POWER(-100, 63)) = '-8.67e+107 Ei\0' (LENGTH 14; the HEX ends in 456900: E, i, NUL) instead of '-8.67e+107 EiB';
- FORMAT_BYTES(-1e125) = '-8.67e+106 Ei\0', so FORMAT_BYTES(-1e125) LIKE '%EiB' is 0;
- FORMAT_PICO_TIME(-1e126) = '-1.16e+109 \0' instead of '-1.16e+109 d'.
The same magnitudes with a positive sign (FORMAT_BYTES(1e126) = '8.67e+107 EiB') and negative values with a two-digit exponent (FORMAT_BYTES(-1e117) = '-8.67e+98 EiB') are correct. FORMAT_BYTES is affected for every value below about -1.15e118, FORMAT_PICO_TIME for every value below about -8.64e116. MySQL 26.10.0 returns '-8.67e+107 EiB', '-8.67e+106 EiB' and '-1.16e+109 d' for the same calls.
The documentation says the result ends with the unit: FORMAT_BYTES "returns a string consisting of a value and the units in a human-readable format. The units will be in bytes, KiB (kibibytes), ... or EiB (exbibytes)"; FORMAT_PICO_TIME "returns a human-readable time value and unit indicator", the units ending with "d - days".
Cause
The buffers are sized for a two-digit exponent (sql/item_strfunc.h:2681-2682 and :2654-2655):
class Item_func_format_bytes : public Item_str_ascii_func |
{
|
/* Format is '-A.AAe+BB UUU' = 13 or 'AAAA.BB UUU' = 11 characters or 'AAAA bytes' = 10 characters. */ |
char m_value_buffer[14]; |
...
|
class Item_func_format_pico_time : public Item_str_ascii_func |
{
|
/* Format is 'AAAA.BB UUU' = 11 characters or 'AAA ps' = 6 characters. */ |
char m_value_buffer[12]; |
and the length of the result is set from snprintf()'s return value, which is the length of the untruncated text (sql/item_strfunc.cc:6098-6104, FORMAT_PICO_TIME the same at :6023-6029):
double value= bytes / divisor; |
if (fabs(value) >= 100000.0) |
len= snprintf(m_value_buffer, sizeof(m_value_buffer), "%4.2e %s", value, unit); |
else |
len= snprintf(m_value_buffer, sizeof(m_value_buffer), "%4.2f %s", value, unit); |
}
|
m_value.length(len);
|
'-8.67e+107 EiB' is 14 characters and '-1.16e+109 d' is 12, so snprintf() writes 13 / 11 characters and the NUL, returns 14 / 12, and m_value.length(len) makes the NUL the last character of the result. (With these units and doubles the text is never longer than 14 / 12 characters, so the length does not go past the buffer.) MySQL's implementation, which these functions follow, uses char m_value_buffer[20] for both.
How to repeat
SELECT FORMAT_BYTES(POWER(-100, 63)) AS b, |
HEX(FORMAT_BYTES(POWER(-100, 63))) AS b_hex, |
LENGTH(FORMAT_BYTES(POWER(-100, 63))) AS b_len; |
SELECT HEX(FORMAT_BYTES(-1e125)), HEX(FORMAT_BYTES(1e126)), |
HEX(FORMAT_BYTES(-1e117));
|
SELECT FORMAT_PICO_TIME(-1e126) AS t, |
HEX(FORMAT_PICO_TIME(-1e126)) AS t_hex, |
LENGTH(FORMAT_PICO_TIME(-1e126)) AS t_len; |
SELECT CONCAT('[', FORMAT_BYTES(-1e125), ']') AS b2, |
FORMAT_BYTES(-1e125) LIKE '%EiB' AS ends_with_unit; |
Output on main (mariadb --table; the client prints the NUL byte as a blank):
+----------------+------------------------------+-------+
|
| b | b_hex | b_len |
|
+----------------+------------------------------+-------+
|
| -8.67e+107 Ei | 2D382E3637652B31303720456900 | 14 |
|
+----------------+------------------------------+-------+
|
+------------------------------+----------------------------+----------------------------+
|
| HEX(FORMAT_BYTES(-1e125)) | HEX(FORMAT_BYTES(1e126)) | HEX(FORMAT_BYTES(-1e117)) |
|
+------------------------------+----------------------------+----------------------------+
|
| 2D382E3637652B31303620456900 | 382E3637652B31303720456942 | 2D382E3637652B393820456942 |
|
+------------------------------+----------------------------+----------------------------+
|
+--------------+--------------------------+-------+
|
| t | t_hex | t_len |
|
+--------------+--------------------------+-------+
|
| -1.16e+109 | 2D312E3136652B3130392000 | 12 |
|
+--------------+--------------------------+-------+
|
+------------------+----------------+
|
| b2 | ends_with_unit |
|
+------------------+----------------+
|
| [-8.67e+106 Ei ] | 0 |
|
+------------------+----------------+
|
Expected (MySQL 26.10.0 gives exactly this): b = '-8.67e+107 EiB', hex 2D382E3637652B31303720456942; HEX(FORMAT_BYTES(-1e125)) = 2D382E3637652B31303620456942; t = '-1.16e+109 d', hex 2D312E3136652B3130392064; b2 = '[-8.67e+106 EiB]', ends_with_unit = 1.
Suggested fix
Make each buffer one byte longer than the longest text, e.g. char m_value_buffer[15] (FORMAT_BYTES) and char m_value_buffer[13] (FORMAT_PICO_TIME), and declare the result length as sizeof(m_value_buffer) - 1 in fix_length_and_dec() so the column width stays 14 / 12; and fix the comment ("'-A.AAe+BBB UUU' = 14"). Clamping len to sizeof(m_value_buffer) - 1 after snprintf() would also keep a future format change from returning bytes past the text.