On Tue, Nov 19, 2019 at 6:21 AM Tom Lane <t...@sss.pgh.pa.us> wrote: > Thomas Munro <thomas.mu...@gmail.com> writes: > > Yeah. Maybe pg_wcswidth() would be OK though, and it's available in > > psql, though I guess you'd have to make a copy with the escaped bits > > stripped out. > > Right, you should use pg_wcswidth() or the underlying PQdsplen() function > to compute display width. The latter might be more convenient since > you could apply it character by character rather than making a copy > of the string.
Right, a PQdsplen()/PQmblen() loop works nicely, as attached. I spotted a potential problem: I suppose I could write a PROMPT1 that includes an invalid multibyte sequence at the end of the buffer and trick PQmblen() or PQdsplen() into reading a few bytes past the end. Two defences against that would be (1) use pg_encoding_verifymb() instead of PQmblen() and (2) use pg_encoding_max_length() to make sure you can't get close enough to the end of the buffer, but neither of those functions are available to psql.
v2-0001-Allow-invisible-PROMPT2-in-psql.patch
Description: Binary data