Thread: BUG #3413: character string or multibyte character to "char"

BUG #3413: character string or multibyte character to "char"

From
"Toru SHIMOGAKI"
Date:
The following bug has been logged online:

Bug reference:      3413
Logged by:          Toru SHIMOGAKI
Email address:      shimogaki.toru@oss.ntt.co.jp
PostgreSQL version: 8.2.4
Operating system:   Red Hat Enterprise Linux AS4
Description:        character string or multibyte character to "char"
Details:

When a character string or a multibyte character is inserted to "char"
column, no error occurs. Is this a bug? Should it be checked as "not single
character" in charin(), charrecv() and charout()?

Anyway, I can't find any spec descriptions in the following document;
  http://www.postgresql.org/docs/8.2/static/datatype-character.html

Best regards,


----

postgres=# select version();
                                              version
----------------------------------------------------------------------------
-----------------------
 PostgreSQL 8.2.4 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.6
20060404 (Red Hat 3.4.6-3)
(1 row)

postgres=# create table test(flag "char");
CREATE TABLE
postgres=# \d test
     Table "public.test"
 Column |  Type  | Modifiers
--------+--------+-----------
 flag   | "char" |

postgres=# insert into test values('a');
INSERT 0 1
postgres=# insert into test values('bb');
INSERT 0 1
postgres=# insert into test values('eeeee');
INSERT 0 1
postgres=# insert into test values('あ');
INSERT 0 1
postgres=# select * from test;
 flag
------
 a
 b
 e

(4 rows)

Re: BUG #3413: character string or multibyte character to "char"

From
Toru SHIMOGAKI
Date:
This is a sample patch for charin() and charrecv(). I'm not sure for charout();
it can return non-ASCII character...


Toru SHIMOGAKI wrote:
> The following bug has been logged online:
>
> Bug reference:      3413
> Logged by:          Toru SHIMOGAKI
> Email address:      shimogaki.toru@oss.ntt.co.jp
> PostgreSQL version: 8.2.4
> Operating system:   Red Hat Enterprise Linux AS4
> Description:        character string or multibyte character to "char"
> Details:
>
> When a character string or a multibyte character is inserted to "char"
> column, no error occurs. Is this a bug? Should it be checked as "not single
> character" in charin(), charrecv() and charout()?
>
> Anyway, I can't find any spec descriptions in the following document;
>   http://www.postgresql.org/docs/8.2/static/datatype-character.html
>
> Best regards,
>
>
> ----
>
> postgres=# select version();
>                                               version
> ----------------------------------------------------------------------------
> -----------------------
>  PostgreSQL 8.2.4 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.6
> 20060404 (Red Hat 3.4.6-3)
> (1 row)
>
> postgres=# create table test(flag "char");
> CREATE TABLE
> postgres=# \d test
>      Table "public.test"
>  Column |  Type  | Modifiers
> --------+--------+-----------
>  flag   | "char" |
>
> postgres=# insert into test values('a');
> INSERT 0 1
> postgres=# insert into test values('bb');
> INSERT 0 1
> postgres=# insert into test values('eeeee');
> INSERT 0 1
> postgres=# insert into test values('あ');
> INSERT 0 1
> postgres=# select * from test;
>  flag
> ------
>  a
>  b
>  e
>
> (4 rows)
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: You can help support the PostgreSQL project by donating at
>
>                 http://www.postgresql.org/about/donate
>
>

--
Toru SHIMOGAKI<shimogaki.toru@oss.ntt.co.jp>
NTT Open Source Software Center
Index: src/backend/utils/adt/char.c
===================================================================
--- src/backend/utils/adt/char.c    (revision 1156)
+++ src/backend/utils/adt/char.c    (working copy)
@@ -34,6 +34,11 @@
 {
     char       *ch = PG_GETARG_CSTRING(0);

+    if (ch[0] != '\0' && ch[1] != '\0')
+        ereport(ERROR,
+            (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
+             errmsg("value too long for type \"char\"")));
+
     PG_RETURN_CHAR(ch[0]);
 }

@@ -67,6 +72,11 @@
 {
     StringInfo    buf = (StringInfo) PG_GETARG_POINTER(0);

+    if (buf->len > 1)
+        ereport(ERROR,
+            (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
+             errmsg("value too long for type \"char\"")));
+
     PG_RETURN_CHAR(pq_getmsgbyte(buf));
 }


Re: BUG #3413: character string or multibyte character to "char"

From
Tom Lane
Date:
"Toru SHIMOGAKI" <shimogaki.toru@oss.ntt.co.jp> writes:
> When a character string or a multibyte character is inserted to "char"
> column, no error occurs. Is this a bug?

That's the historical behavior of the datatype, and given that it's
been like that since Berkeley days, changing it seems ill-advised.
If you want a column that behaves sanely for multibyte data, use
char(1).

            regards, tom lane