xmlelement() with xmlattributes() hands server-encoding bytes to libxml's xmlTextWriterWriteAttribute() as if they were UTF-8, so non-ASCII attribute values in a non-UTF8 database are silently replaced by wrong character references or fail with "could not write xml attribute" ==================================================================================================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a229e3-1ab2-7ee4-b7bd-3957880705b7 Filed: 2026-11-30 00:37:59 UTC Severity: 24/100 Blamed commit: c0e977c18f1939cb0d948c51740aa8816d69c720 ## Summary In a database whose server encoding is not UTF8, XMLATTRIBUTES values containing non-ASCII characters are not written correctly by `xmlelement()`. The value is passed to libxml in the server encoding, but libxml expects UTF-8. Depending on the bytes, the result is a silently wrong attribute value (the bytes happen to form valid UTF-8, so libxml turns them into a numeric character reference for an unrelated code point) or the error `could not write xml attribute`. Element content of the same string is written correctly. ## Reproduction In a database created with `ENCODING 'EUC_JP'` (client encoding UTF8): ```sql -- U+8868 is EUC_JP bytes C9 BD select xmlelement(name a, convert_from('\xc9bd'::bytea, 'EUC_JP')); select xmlelement(name a, xmlattributes(convert_from('\xc9bd'::bytea, 'EUC_JP') as b)); -- U+65E5 U+672C U+8A9E select xmlelement(name a, convert_from('\xc6fccbdcb8ec'::bytea, 'EUC_JP')); select xmlelement(name a, xmlattributes(convert_from('\xc6fccbdcb8ec'::bytea, 'EUC_JP') as b)); ``` Actual: ``` U+8868 -- correct -- wrong: should be U+65E5 U+672C U+8A9E -- correct ERROR: could not write xml attribute ``` (U+xxxx stands for the actual characters.) Expected: attribute values are written like element content, `` and ``. A value that mixes a special character with non-ASCII text shows the same corruption: U+65E5 followed by `&` yields `Ƽ&`, where `Ƽ` is the EUC_JP bytes C6 FC read as UTF-8. The same applies to other server encodings: in a LATIN1 database the value `'&é<'` raises the same error, and a value made of the two characters U+00C3 U+00A9 is written as `é`. ## Analysis `xmlelement()` in `xml.c` converts each attribute value with `map_sql_value_to_xml_value(..., false)`, which returns the value in the server encoding, and passes it directly to `xmlTextWriterWriteAttribute()`. libxml treats `xmlChar *` data as UTF-8, escapes it with its own entity encoder, and substitutes `&#xHHHH;` references for the code points it decodes. For byte sequences that are not valid UTF-8 it reports an error, which surfaces as "could not write xml attribute". Element content bypasses the escaper through `xmlTextWriterWriteRaw()` on already-escaped text, which is why it is not affected. The attribute call was introduced when `xmlelement()` was rewritten to use libxml's xmlwriter; no server-to-UTF-8 conversion of the attribute value (and of the result) was ever added. The documentation notes that some XML functions may not work on non-ASCII data when the server encoding is not UTF-8 and names `xmltable()` and `xpath()`, but it does not mention XMLATTRIBUTES, and silently producing a different attribute value is worse than failing. A fix would convert attribute values to UTF-8 before the call (`pg_do_encoding_conversion()` as `xml_parse()` does) and convert the writer's output back to the server encoding, or build the attribute text with `escape_xml()`-style escaping like the content path. ## Notes Branches: master (reproduced at a68bb26f294c7f1280284f753b26ea7fb3f25964); REL_14 through REL_19 likely by code inspection (xmlelement() passes attribute values to xmlTextWriterWriteAttribute() without conversion on every stable branch). Related (different root cause, same producer path): https://ai-annex.j-davis.com/report/01a1f099-5519-72e7-a52a-5fe9361cc6fa Tested on master a68bb26f294c7f1280284f753b26ea7fb3f25964 (catversion 202611241), EUC_JP and LATIN1 databases.