Re: encode/decode support for base64url

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
Cc: Daniel Gustafsson <daniel(at)yesql(dot)se>, Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>, "David E(dot) Wheeler" <david(at)justatheory(dot)com>, Aleksander Alekseev <aleksander(at)tigerdata(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, Cary Huang <cary(dot)huang(at)highgo(dot)ca>, Przemysław Sztoch <przemyslaw(at)sztoch(dot)pl>
Subject: Re: encode/decode support for base64url
Date: 2025-09-19 06:56:27
Message-ID: 9D9864DE-86B9-4702-97CB-5D41A79BF77B@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Sep 19, 2025, at 14:45, Florents Tselai <florents(dot)tselai(at)gmail(dot)com> wrote:
>>
>> I reviewed and tested this patch. Overall looks good to me. Actually, I think this patched fixed a bug of current implementation of base64 encoding by moving the logic of handling newline into “if (pos<0)”.
>
> IIUC what you mean, I can’t confirm that.
>
> Both existing and new implementation handle new lines the same
>
> SELECT decode(E'QUFB\nQUFB', 'base64url');
> decode
> ----------------
> \x414141414141
> (1 row)
>

The current implementation isn’t actually wrong, but at least not optimized as your version. Because we don’t need to check “if (p >= lend)” after p is bumped, and only when “if (pos <0)”, p is bumped.

>
>>
>> 3.
>> ```
>> ereport(ERROR,
>> (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
>> - errmsg("unexpected \"=\" while decoding base64 sequence")));
>> + errmsg("unexpected \"=\" while decoding %s sequence", url ? "base64url" : "base64")));
>> ```
>>
>> This is a normal usage that injects sub-strings based on condition. However, PG doesn’t like that, see here: https://www.postgresql.org/docs/devel/nls-programmer.html#NLS-GUIDELINES
>
> Well, that’s a very interesting catch.
> I’ll let a comitter confirm & advise.

I got to know this because once I reviewed a Tom Lane’s patch, it had the similarly situation, but Tom wrote code like:

```
If (something)
Ereport(“function xxx”)
Else
Ereport(“procedure xxx”)
```

I raised a comment to suggest avoid duplicate code in the way like your code do, and I got a response with “no” and the link.

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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Frédéric Yhuel 2025-09-19 07:06:49 Re: [BUG] temporary file usage report with extended protocol and unnamed portals
Previous Message Chao Li 2025-09-19 06:45:22 Fix jsonb_from_cstring parameter type for length