[PATCH] Patch to fix a crash of psql - Mailing list pgsql-hackers

From JiangGuiqing
Subject [PATCH] Patch to fix a crash of psql
Date
Msg-id 50B71728.8040309@cn.fujitsu.com
Whole thread Raw
Responses Re: [PATCH] Patch to fix a crash of psql  (Tatsuo Ishii <ishii@postgresql.org>)
Re: [PATCH] Patch to fix a crash of psql  (Tatsuo Ishii <ishii@postgresql.org>)
List pgsql-hackers
hi

When i test psql under multi-lingual and different encoding environment,
I found a crash of psql.

----------------------------------------------------------------------
$ export PGCLIENTENCODING=SJIS
$ psql
psql (9.2rc1)
Type "help" for help.

postgres=# \i sql
CREATE DATABASE
You are now connected to database "mydb" as user "postgres".
CREATE SCHEMA
Segmentation fault (core dumped)
$
----------------------------------------------------------------------

I'm look into this problem and found that
only some especial character can cause psql crash.
conditions is:
1. some especial character
(my sql file contains japanese comment "-- コメント" .  It can cause
psql crash.)
2. PGCLIENTENCODING is SJIS
3. the encoding of input sql file is UTF-8


I investigated this problem. The reasons are as follows.
----------------------------------------------------------------------
src/bin/psql/mainloop.c
-> psql_scan_setup()    //Set up to perform lexing of the given input line.
-->prepare_buffer ()    //Set up a flex input buffer to scan the given data.
---->malloc character buffer.
---->set two \0 characters. (Flex wants two \0 characters after the
actual data.)
---->working in an unsafe encoding, the copy has multibyte sequences
replaced by FFs to avoid fooling the lexer rules.
****the encoding of input sql file is different from PGCLIENTENCODING, two
\0 characters are replaced by FFs. ****

---->yy_scan_buffer()   //Setup the input buffer state to scan directly
from a user-specified character buffer.
****because  two \0 characters are replaced by FFs,yy_scan_buffer() return
0.  input buffer state can not setup correctly.****

-> psql_scan()   //Do lexical analysis of SQL command text.
--> yylex()         //The main scanner function which does all the work.
****because input buffer state is not setup,so when access the input
buffer state,segmentation fault is happened.****
----------------------------------------------------------------------


I modify src/bin/psql/psqlscan.l to resolve this problem.
The diff file refer to the attachment "psqlscan.l.patch".


Regards,
Jiang Guiqing

Attachment

pgsql-hackers by date:

Previous
From: Amit Kapila
Date:
Subject: Re: Enabling frontend-only xlog "desc" routines
Next
From: "er"
Date:
Subject: Re: WIP: index support for regexp search