Escape CR/LF in invalid database, role, and tablespace name errors

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Mahendra Singh Thalor <mahi6run(at)gmail(dot)com>, Andres Freund <andres(at)anarazel(dot)de>
Subject: Escape CR/LF in invalid database, role, and tablespace name errors
Date: 2026-07-02 04:16:34
Message-ID: 7A01E3AA-454F-499C-8A2E-31C127FBDA5C@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

While testing “[b380a56a3] Disallow CR and LF in database, role, and tablespace names”, I saw that CR/LF are properly rejected. However, the raw problematic names are printed directly in the error message, which can also reach the server log. This causes LF to split the error message and CR to overwrite/hide the beginning of the message, which seems not good.

See this simple repro:

1. Newline case
```
evantest=# create role "a
evantest"# b";
ERROR: role name "a
b" contains a newline or carriage return character
```

And server log looks like:
```
2026-07-02 10:57:51.518 CST [96799] ERROR: role name "a
b" contains a newline or carriage return character
```

2. Carriage return case
```
% psql -d evantest -c $'CREATE ROLE "a\rb";'
b" contains a newline or carriage return character
```

As shown above, printing the raw name in the error message can make the log entry confusing: LF splits the error message across multiple lines, while CR can overwrite the beginning of the line and lose information.

I think we should escape CR/LF before including these names in error messages and line-oriented reports.

Since pg_upgrade also writes bad names into a report file, I looked for a place to add a helper that can be shared by both backend and frontend code. I ended up adding one in src/common/string.c.

See the attached patch for details.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachment Content-Type Size
v1-0001-Escape-CR-LF-in-invalid-global-object-name-errors.patch application/octet-stream 10.7 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bertrand Drouvot 2026-07-02 04:23:16 Re: Prevent crash when calling pgstat functions with unregistered stats kind
Previous Message jian he 2026-07-02 04:14:55 Re: Row pattern recognition