]> git.ipfire.org Git - thirdparty/postgresql.git/commit
seg: Fix seg_out() to preserve the upper boundary's certainty indicator
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Thu, 11 Jun 2026 09:33:48 +0000 (12:33 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Thu, 11 Jun 2026 09:33:48 +0000 (12:33 +0300)
commit0e1f1ed157e90741e12a3715909e1b2d71ff9344
tree78d90ad65780b74809f695baf9360e6789732943
parenteb4e7224a1c6f0058d708cdfda7326bbf884a871
seg: Fix seg_out() to preserve the upper boundary's certainty indicator

When printing the upper boundary of a seg interval, seg_out() decided
whether to emit the certainty indicator ('<', '>' or '~') by testing the
upper indicator (u_ext) for '<' and '>', but mistakenly tested the lower
indicator (l_ext) for '~'.  This is a copy-and-paste slip from the
symmetric code that prints the lower boundary a few lines above.

The consequences for valid input were:

  * A '~' on the upper boundary was dropped on output, e.g.
    '1.5 .. ~2.5'::seg printed as '1.5 .. 2.5'.

  * When the lower boundary carried '~' but the upper boundary had no
    indicator, the wrong test matched and sprintf(p, "%c", seg->u_ext)
    wrote a NUL byte (u_ext == '\0'), which truncated the result string
    and silently lost the entire upper boundary, e.g.
    '~6.5 .. 8.5'::seg printed as '~6.5 .. '.

Certainty indicators are documented to be preserved on output (they are
ignored by the operators, but kept as comments), so this broke the
input/output round-trip for the affected values.

The bug has existed since seg was added.  It went unnoticed because the
existing regression tests only exercised certainty indicators on
single-point segs, which are printed by a different branch of seg_out().
Add tests that place indicators on both boundaries of an interval.

Author: Ewan Young <kdbase.hack@gmail.com>
Discussion: https://www.postgresql.org/message-id/CAON2xHPYeRRCEVAv8XfE18KsEsEHCiYcJ5fOsoxFuMEfpxF1=g@mail.gmail.com
Backpatch-through: 14
contrib/seg/expected/seg.out
contrib/seg/seg.c
contrib/seg/sql/seg.sql