to_number() skips a non-digit multibyte character one byte at a time at a 9/0 position, so it uses up one digit position per byte and drops following digits (to_number('1é23','9999') is 12, to_number('1a23','9999') is 123) ============================================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a2273b-ad32-7b47-8a65-df29f41c2087 Filed: 2026-11-29 12:15:52 UTC Severity: 20/100 Blamed commit: b866d2e2d79416f8497e4dffa7e800298d018f6c ## Summary to_number() ignores a non-digit character where the template has a 9 or 0 (to_number('1a23', '999') reads 1 and 2). For an ASCII character that uses up one digit position. For a multibyte character it uses up one position per byte, so the same input with é, 日 or an emoji in place of a gets fewer digits read. ## Reproduction ``` SELECT to_number('1a23', '9999') AS ascii_junk, to_number('1é23', '9999') AS two_byte_junk, to_number('1日23', '9999') AS three_byte_junk, to_number('1😀23', '9999') AS four_byte_junk; SELECT to_number('1a2', '999') AS ascii_junk, to_number('1é2', '999') AS two_byte_junk, to_number('1日2', '999') AS three_byte_junk; ``` Actual output (UTF8 database, psql -X): ``` ascii_junk | two_byte_junk | three_byte_junk | four_byte_junk ------------+---------------+-----------------+---------------- 123 | 12 | 1 | 1 ascii_junk | two_byte_junk | three_byte_junk ------------+---------------+----------------- 12 | 1 | 1 ``` Expected: the same result whatever the byte length of the skipped character. Whether junk should be skipped at all is a separate question; the point is that the outcome depends on the encoding length of the character. ## Analysis In NUM_processor() (the to_number branch), a 9, 0 or decimal point pattern calls NUM_numpart_from_char(), which reads a digit or decimal point if there is one. When the current input character is anything else, nothing is read and the loop ends with the unconditional `Np->inout_p++` that advances one byte. The next pattern then starts in the middle of the multibyte character, where it again sees a non-digit and again advances one byte, until the character has been passed over. The non-data patterns in the same loop already advance by whole characters (`pg_mblen_range()`), and commit e87d4965bd39 ("Prevent to_number() from losing data when template doesn't match exactly") changed them from bytes to characters for that reason. The skip at the bottom of the loop was left byte-wise. Stepping to the character boundary there with pg_mblen_range() would make the result independent of the character's length. ## Notes Tested on master f836f25b6f095f3b23762026831d2fab6117087c with assertions enabled, server_encoding UTF8. Branches: master (reproduced at f836f25b); REL_14 through REL_19 likely by code inspection (the same byte-wise advance at the bottom of the NUM_processor() loop, next to the character-wise skip for non-data patterns, is present on REL_14 and REL_18). Related: https://ai-annex.j-davis.com/report/01a1d16e-d244-7722-8e03-60ac2ebecc25 (to_number() with FM and a group separator drops digits; same blame commit, different cause)