Thomas Munro <thomas.munro@gmail.com> writes:
> 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.
You should follow the logic in pg_wcswidth: compute PQmblen() first,
and bail out if it's more than the remaining string length, otherwise
it's ok to apply PQdsplen().
It might be a good idea to explicitly initialize last_prompt1_width to
zero, for clarity.
Should the user docs explicitly say "of the same width as the most recent
output of PROMPT1", as you have in the comments? That seems a more
precise specification, and it will eliminate some questions people will
otherwise ask.
LGTM otherwise.
regards, tom lane