Joe Conway wrote:
> Tatsuo Ishii wrote:
> >>> Tatsuo Ishii wrote:
> >>>
> >>>>
> >>>> Try a multibyte encoding database. For example,
> >>>>
> >>>> $ createdb -E EUC_JP test $ psql -c 'SELECT
> >>>> SUBSTRING('1234567890' FROM 3)' test substring ----------- 3456
> >>>>
>
> >>>> (1 row)
> >>>>
> >>>> Apparently this is wrong. -- Tatsuo Ishii
> >>>
> >>> This problem exists in CVS tip *without* the unknownin/out
> >>> patch:
> >>
> >> Sure. That has been broken for a while.
> >
> >
> > I guess this actually happened in 1.79 of varlena.c:
> >
> Yes, I was just looking at that also. It doesn't consider the case of n
> = -1 for MB. See the lines:
>
> #ifdef MULTIBYTE
> eml = pg_database_encoding_max_length ();
>
> if (eml > 1)
> {
> sm = 0;
> sn = (m + n) * eml + 3;
> }
> #endif
>
> When n = -1 this does the wrong thing. And also a few lines later:
>
> #ifdef MULTIBYTE
> len = pg_mbstrlen_with_len (VARDATA (string), sn - 3);
>
> I think both places need to test for n = -1. Do you agree?
>
>
> Joe
>
The attached patch should fix the bug reported by Tatsuo.
# psql -U postgres testjp
Welcome to psql, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit
testjp=# SELECT SUBSTRING('1234567890' FROM 3);
substring
------------
34567890
(1 row)
Joe
*** pgsql/src/backend/utils/adt/varlena.c Sun Apr 7 11:44:54 2002
--- pgsql.orig/src/backend/utils/adt/varlena.c Mon Apr 8 22:32:16 2002
***************
*** 413,419 ****
if (eml > 1)
{
sm = 0;
! sn = (m + n) * eml + 3; /* +3 to avoid mb characters overhanging slice end */
}
#endif
--- 373,382 ----
if (eml > 1)
{
sm = 0;
! if (n > -1)
! sn = (m + n) * eml + 3; /* +3 to avoid mb characters overhanging slice end */
! else
! sn = n; /* n < 0 is special-cased by heap_tuple_untoast_attr_slice */
}
#endif
***************
*** 427,433 ****
PG_RETURN_NULL(); /* notreached: suppress compiler warning */
#endif
#ifdef MULTIBYTE
! len = pg_mbstrlen_with_len (VARDATA (string), sn - 3);
if (m > len)
{
--- 390,399 ----
PG_RETURN_NULL(); /* notreached: suppress compiler warning */
#endif
#ifdef MULTIBYTE
! if (n > -1)
! len = pg_mbstrlen_with_len (VARDATA (string), sn - 3);
! else /* n < 0 is special-cased; need full string length */
! len = pg_mbstrlen (VARDATA (string));
if (m > len)
{