Skip to content

Fix pointer truncation crash caused by missing get_database_name declaration#96

Merged
amutu merged 1 commit into
amutu:masterfrom
bigplaice:adapt_pg19
Jun 29, 2026
Merged

Fix pointer truncation crash caused by missing get_database_name declaration#96
amutu merged 1 commit into
amutu:masterfrom
bigplaice:adapt_pg19

Conversation

@bigplaice

Copy link
Copy Markdown
Contributor

PostgreSQL commit 20abdc9e82 ("Avoid including commands/dbcommands.h in
so many places") moved the declaration of get_database_name() from
commands/dbcommands.h to utils/lsyscache.h. Extensions that relied on
the old header location no longer get a proper function prototype,
causing the compiler to assume the function returns int (32-bit) instead
of char * (64-bit). On x86_64 this silently truncates the returned
pointer, leading to a segmentation fault when the truncated pointer is
later dereferenced (e.g. by strlen inside snprintf).

Use utils/lsyscache.h on PostgreSQL 19+ where the declaration lives
there, and fall back to commands/dbcommands.h on older versions where it
is still provided.

…aration

  PostgreSQL commit 20abdc9e82 ("Avoid including commands/dbcommands.h in
  so many places") moved the declaration of get_database_name() from
  commands/dbcommands.h to utils/lsyscache.h.  Extensions that relied on
  the old header location no longer get a proper function prototype,
  causing the compiler to assume the function returns int (32-bit) instead
  of char * (64-bit).  On x86_64 this silently truncates the returned
  pointer, leading to a segmentation fault when the truncated pointer is
  later dereferenced (e.g. by strlen inside snprintf).

  Use utils/lsyscache.h on PostgreSQL 19+ where the declaration lives
  there, and fall back to commands/dbcommands.h on older versions where it
  is still provided.
@bigplaice

Copy link
Copy Markdown
Contributor Author

Base code: postgresql-19beta1

In contrib directory,
wget https://github.com/amutu/zhparser/archive/refs/tags/v2.3.tar.gz
make PG_CONFIG=/home/highgo/pg/inst/bin/pg_config
make PG_CONFIG=/home/highgo/pg/inst/bin/pg_config install

When build source code, there is WARNING:
zhparser.c: In function ‘init’:
zhparser.c:85:13: warning: old-style function definition [-Wold-style-definition]
zhparser.c:216:34: warning: implicit declaration of function ‘get_database_name’; did you mean ‘GetDatabasePath’? [-Wimplicit-function-declaration]
216 | DataDir, get_database_name(MyDatabaseId));
| ^~~~~~~~~~~~~~~~~
| GetDatabasePath
zhparser.c:215:61: warning: format ‘%s’ expects argument of type ‘char *’, but argument 5 has type ‘int’ [-Wformat=]
215 | snprintf(dict_path, MAXPGPATH, "%s/base/zhprs_dict_%s.txt",
| ~^
| |
| char *
| %d
216 | DataDir, get_database_name(MyDatabaseId));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| int

Then create extension and run example query:

-- create the extension
CREATE EXTENSION zhparser;

-- make test configuration using parser
CREATE TEXT SEARCH CONFIGURATION testzhcfg (PARSER = zhparser);

-- add token mapping
ALTER TEXT SEARCH CONFIGURATION testzhcfg ADD MAPPING FOR n,v,a,i,e,l WITH simple;

-- ts_parse
SELECT * FROM ts_parse('zhparser', 'hello world! 2010年保障房建设在全国范围内获全面启动,从中央到地方纷纷加大 了保障房的建设和投入力度 。2011年,保障房进入了更大规模的建设阶段。住房城乡建设部党组书记、部长姜伟新去年底在全国住房城乡建设工作会议上表示,要继续推进保障性安居工程建设。');

Crash happens:
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: Succeeded.

@amutu
amutu merged commit 2e995c4 into amutu:master Jun 29, 2026
2 checks passed
@amutu

amutu commented Jun 29, 2026

Copy link
Copy Markdown
Owner

Thanks for your patch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants