ibarwick / firebird_fdw

A PostgreSQL foreign data wrapper (FDW) for Firebird - latest version 1.4.0 (2024-05-11)
https://sql-info.de/postgresql/firebird-fdw/index.html
Other
36 stars 9 forks source link

PostgreSQL crash on VARCHAR overflow with multibyte characters #5

Closed ibarwick closed 6 years ago

ibarwick commented 6 years ago

Table definitions:

SQL> \d instest
                         Table "instest"
 Column | Field type  | Modifiers | Default value | Description
--------+-------------+-----------+---------------+-------------
 id     | INTEGER     |           |               |
 val    | VARCHAR(20) |           |               |

postgres=# \d fb_instest
                       Foreign table "repmgr.fb_instest"
 Column |         Type          | Collation | Nullable | Default | FDW options
--------+-----------------------+-----------+----------+---------+-------------
 id     | integer               |           |          |         |
 val    | character varying(20) |           |          |         |
Server: fb_test
FDW options: (table_name 'instest')

Insert attempts:

postgres=# insert into fb_instest values(1,'QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ');
ERROR:  value too long for type character varying(20)

postgres=# insert into fb_instest values(1,'なんじゃこれ');
INSERT 0 1

postgres=# insert into fb_instest values(1,'なんじゃこれあ');

server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
!?>
!?> \q

Firebird counts bytes, not characters?

postgres=# select id, val, length(val) from fb_instest ;
 id |     val      | length
----+--------------+--------
  1 | foo          |      3
  1 | xxx          |      3
  1 | なんじゃこれ |      6
(3 rows)

SQL> SELECT id, val, char_length(val) from instest;
 id | val          | char_length
----+--------------+-------------
  1 | foo          |           3
  1 | xxx          |           3
  1 | なんじゃこれ |          18
(3 rows)
Time: 38.438 ms

This should at least result in an error being raised by Firebird and handled by the FDW; direct insert attempt:

SQL> INSERT INTO instest values(1,'なんじゃこれあ');
error executing non-SELECT
arithmetic exception, numeric overflow, or string truncation
ibarwick commented 6 years ago

Fixed; it now does this:

postgres=# insert into fb_instest values(1,'なんじゃこれあ');
ERROR:  [firebird_fdw]
DETAIL:
[firebird_fdw] isc_dsql_execute() error
[firebird_fdw] Dynamic SQL Error
[firebird_fdw] arithmetic exception, numeric overflow, or string truncation
[firebird_fdw] string right truncation

(not as elegant as it could be, but much better than crashing the server)