pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #19843: decode(..., 'base64url') returns an extra byte for "ab="
4+ messages / 4 participants
[nested] [flat]
* BUG #19843: decode(..., 'base64url') returns an extra byte for "ab="
@ 2026-11-07 14:12 PG Bug reporting form <noreply@postgresql.org>
0 siblings, 1 reply; 4+ messages in thread
From: PG Bug reporting form @ 2026-11-07 14:12 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: 125atishprak@gmail.com
The following bug has been logged on the website:
Bug reference: 19843
Logged by: Atish Prakash
Email address: 125atishprak@gmail.com
PostgreSQL version: 19rc1
Operating system: Linux x86_64
Description:
decode() with the new base64url format returns a spurious trailing byte when
the input has one '=' where two would be needed:
SELECT decode('ab', 'base64url'); -- \x69
SELECT decode('ab==', 'base64url'); -- \x69
SELECT decode('ab=', 'base64url'); -- \x69b0
Expected: the third query returns \x69, like the other two, or raises an
error.
The base64 format raises "invalid base64 end sequence" for the same input.
The extra byte 0xb0 comes from the leftover bits of 'b' and the padding, and
is not data encoded in the input.
Cause: in pg_base64_decode_internal(), the '=' sets end = 1 and advances pos
to
3. The input then ends, and the "url && pos == 3" branch emits two bytes
without looking at end.
A possible patch is attached
(0001-fix-base64url-decode-short-padding.patch). It
makes the result \x69. Rejecting the input would also be reasonable, and I
have
no preference. Regression tests pass with it on master; it adds no new test.
Found with semantic differential tests. I reproduced and checked the result
myself.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: BUG #19843: decode(..., 'base64url') returns an extra byte for "ab="
@ 2026-11-07 14:14 Atisss hh <125atishprak@gmail.com>
parent: PG Bug reporting form <noreply@postgresql.org>
0 siblings, 1 reply; 4+ messages in thread
From: Atisss hh @ 2026-11-07 14:14 UTC (permalink / raw)
To: 125atishprak@gmail.com; pgsql-bugs@lists.postgresql.org
On Sat, 7 Nov 2026 at 19:43, PG Bug reporting form <noreply@postgresql.org>
wrote:
> The following bug has been logged on the website:
>
> Bug reference: 19843
> Logged by: Atish Prakash
> Email address: 125atishprak@gmail.com
> PostgreSQL version: 19rc1
> Operating system: Linux x86_64
> Description:
>
> decode() with the new base64url format returns a spurious trailing byte
> when
> the input has one '=' where two would be needed:
>
> SELECT decode('ab', 'base64url'); -- \x69
> SELECT decode('ab==', 'base64url'); -- \x69
> SELECT decode('ab=', 'base64url'); -- \x69b0
>
> Expected: the third query returns \x69, like the other two, or raises an
> error.
> The base64 format raises "invalid base64 end sequence" for the same input.
> The extra byte 0xb0 comes from the leftover bits of 'b' and the padding,
> and
> is not data encoded in the input.
>
> Cause: in pg_base64_decode_internal(), the '=' sets end = 1 and advances
> pos
> to
> 3. The input then ends, and the "url && pos == 3" branch emits two bytes
> without looking at end.
>
> A possible patch is attached
> (0001-fix-base64url-decode-short-padding.patch). It
> makes the result \x69. Rejecting the input would also be reasonable, and I
> have
> no preference. Regression tests pass with it on master; it adds no new
> test.
>
> Found with semantic differential tests. I reproduced and checked the result
> myself.
>
>
>
Attachments:
0001-fix-base64url-decode-short-padding.patch (text/x-patch, 504B) download
diff --git a/src/backend/utils/adt/encode.c b/src/backend/utils/adt/encode.c
index b9d4f2811d..63849e1de5 100644
--- a/src/backend/utils/adt/encode.c
+++ b/src/backend/utils/adt/encode.c
@@ -592,7 +592,9 @@ pg_base64_decode_internal(const char *src, size_t len, char *dst, bool url)
{
buf <<= 6;
*p++ = (buf >> 16) & 0xFF;
- *p++ = (buf >> 8) & 0xFF;
+ /* "ab=" carries only one byte; the padding bits are not data */
+ if (end == 0)
+ *p++ = (buf >> 8) & 0xFF;
}
else if (pos != 0)
{
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: BUG #19843: decode(..., 'base64url') returns an extra byte for "ab="
@ 2026-11-09 04:57 shihao zhong <zhong950419@gmail.com>
parent: Atisss hh <125atishprak@gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: shihao zhong @ 2026-11-09 04:57 UTC (permalink / raw)
To: Atisss hh <125atishprak@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org, Daniel Gustafsson <daniel@yesql.se>
Hi Atish,
Thanks for the report. I think this is already fixed by the patch I
posted for BUG #19702[1]. With it 'ab=' and 'ab==cd' both raise the same
error as base64, and its tests include this case.
[1]https://postgr.es/m/19702-9ed4a131fcfadb9d@postgresql.org
Thanks,
Shihao
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: BUG #19843: decode(..., 'base64url') returns an extra byte for "ab="
@ 2026-11-09 22:26 Daniel Gustafsson <daniel@yesql.se>
parent: shihao zhong <zhong950419@gmail.com>
0 siblings, 0 replies; 4+ messages in thread
From: Daniel Gustafsson @ 2026-11-09 22:26 UTC (permalink / raw)
To: shihao zhong <zhong950419@gmail.com>; +Cc: Atisss hh <125atishprak@gmail.com>; pgsql-bugs@lists.postgresql.org
> On 9 Nov 2026, at 05:57, shihao zhong <zhong950419@gmail.com> wrote:
>
> Hi Atish,
>
> Thanks for the report. I think this is already fixed by the patch I
> posted for BUG #19702[1]. With it 'ab=' and 'ab==cd' both raise the same
> error as base64, and its tests include this case.
>
> [1]https://postgr.es/m/19702-9ed4a131fcfadb9d@postgresql.org
Thanks for the pointer, continuing over in that thread.
--
Daniel Gustafsson
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2026-11-09 22:26 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-07 14:12 BUG #19843: decode(..., 'base64url') returns an extra byte for "ab=" PG Bug reporting form <noreply@postgresql.org>
2026-11-07 14:14 ` Atisss hh <125atishprak@gmail.com>
2026-11-09 04:57 ` shihao zhong <zhong950419@gmail.com>
2026-11-09 22:26 ` Daniel Gustafsson <daniel@yesql.se>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox