Re: Fix direct conversion tables for single-byte Cyrillic encodings (Bug #19831)

From: koussaymehdouani(at)gmail(dot)com
To: koussaymehdouani(at)gmail(dot)com, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Fix direct conversion tables for single-byte Cyrillic encodings (Bug #19831)
Date: 2026-11-26 18:19:01
Message-ID: CA+aTUPtrsY4jmDYWs8iGKrsZd=uejjzpyODf_utCYkyO6W9JbQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

CFBot reported "Patch format detection failed" for the v2 I sent on 21
November, so I am resending the identical change as a git format-patch
attachment (v3). The content is unchanged: the same 234 table
corrections and the same regression tests, rebased onto master
892cfb4cbfe0e37cc492f0e750e80cfa6394d26d.

This revision was prepared with AI assistance (Muse Spark, Meta AI);
the build, reproduction and regression runs were executed and checked
locally.

Regards,
Koussay Mehdouani

On Sat, 21 Nov 2026 10:15:31 -0800, koussaymehdouani(at)gmail(dot)com wrote:
> Hi,
>
> Attached is v2 of the patch, rebased onto current master
> (892cfb4cbfe0e37cc492f0e750e80cfa6394d26d) after CFBot flagged v1
> as needing a rebase.
>
> The rebase is mechanical: upstream renamed
> src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c
> to src/backend/utils/mb/conversion_procs/cyrillic/cyrillic.c and
> removed the MULE_INTERNAL conversions, so the patch now targets
> the new file. The corrected tables and the regression tests are
> unchanged in substance.
>
> Re-verified on master: composing the generated Unicode maps still
> gives exactly the same 234 disagreements with the shipped tables,
> and the corrected tables agree with the composed maps everywhere.
> The conversion regression tests pass with the fix; with the
> original tables they fail, with every difference inside the new
> test section.
>
> This revision was prepared with AI assistance (Muse Spark,
> Meta AI); the build, reproduction and regression runs were
> executed and checked locally.
>
> Regards,
> Koussay Mehdouani
>
> On Thu, 12 Nov 2026 07:07:19 -0500, koussaymehdouani(at)gmail(dot)com wrote:
> > Hi,
> >
> > This patch fixes Bug #19831, reported by chunling qin on pgsql-bugs
> > (https://www.postgresql.org/message-id/19831-7ebbea4e9e6d9c34@postgresql.org)
> > convert() between single-byte Cyrillic encodings can return a wrong
> > character, can silently succeed where the character has no representation
> > in the target encoding, or can fail where it does. The same direct
> > conversions are used for client encoding conversion and COPY ... ENCODING,
> > so this is silent data corruption, not just a function-level oddity.
> >
> > I reproduced every case from the report on 18.6. For example, KOI8R byte
> > 0xa4 (U+2553) converts directly to WIN866 as 0xf3 (a different character),
> > while going through UTF-8 with PostgreSQL's own generated maps yields 0xd6.
> >
> > Root cause: the hand-written per-byte tables in
> > src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c
> > have drifted from the Unicode-generated maps. To find every bad entry, I
> > composed the generated maps (X_to_utf8 followed by utf8_to_Y) for all 12
> > direct pairs among KOI8R, WIN866, WIN1251 and ISO-8859-5, and compared
> > against the tables. There were 234 disagreeing entries: 12 wrong
> > characters, 26 entries that produced a byte where conversion must fail,
> > and 196 entries that failed where conversion must succeed (the tables are
> > mostly zero across the 0x80-0xBF pseudographics range). The patch corrects
> > exactly those entries; no code changes.
> >
> > Regression tests: conversion.sql now checks, for each of the 12 pairs,
> > converting every byte that has a representation in the target encoding,
> > plus an individual must-fail case for each byte that previously slipped
> > through. The new tests fail on the unpatched tree and pass with the fix.
> > I also verified on a live server that, after the fix, the direct result
> > agrees with the via-UTF-8 result for all 128 high bytes of all 12 pairs
> > (before the fix, the per-pair mismatch counts were exactly the 234 above).
> >
> > One scope question: the same hand-written-table pattern exists in
> > conversion_procs/latin2_and_win1250 (LATIN2 <-> WIN1250), and the
> > report's LATIN2 case (byte 0x80 passing through unchanged) reproduces
> > there too. I scoped this patch to the Cyrillic family covered by the
> > bug report; I am happy to extend it or split it if reviewers prefer.
> >
> > This patch was prepared with AI assistance (Muse Spark, Meta AI);
> > I built, reproduced, and tested everything locally myself.
> >
> > Regards,
> > Koussay Mehdouani

Attachment Content-Type Size
fix-19831-v3.patch application/octet-stream 37.0 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Aleksander Alekseev 2026-11-26 18:29:40 Re: Write skew observed under serializable isolation
Previous Message Greg Lamberson 2026-11-26 17:39:09 Re: Extensible sync handler registration (register_sync_handler)