BUG #19843: decode(..., 'base64url') returns an extra byte for "ab="

Started by PG Bug reporting form15 days ago4 messagesbugs
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

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.

#2Atisss hh
125atishprak@gmail.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19843: decode(..., 'base64url') returns an extra byte for "ab="

On Sat, 7 Nov 2026 at 19:43, PG Bug reporting form <noreply@postgresql.org>
wrote:

Show quoted text

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:

t254609_2
0001-fix-base64url-decode-short-padding.patchtext/x-patch; charset=US-ASCII; name=0001-fix-base64url-decode-short-padding.patchDownload+3-1
#3shihao zhong
zhong950419@gmail.com
In reply to: Atisss hh (#2)
Re: BUG #19843: decode(..., 'base64url') returns an extra byte for "ab="

Hi Atish,

Thanks for the report. I think this is already fixed by the patch I
posted for BUG #19702[1]/messages/by-id/19702-9ed4a131fcfadb9d@postgresql.org. With it 'ab=' and 'ab==cd' both raise the same
error as base64, and its tests include this case.

[1]: /messages/by-id/19702-9ed4a131fcfadb9d@postgresql.org

Thanks,
Shihao

#4Daniel Gustafsson
daniel@yesql.se
In reply to: shihao zhong (#3)
Re: BUG #19843: decode(..., 'base64url') returns an extra byte for "ab="

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]/messages/by-id/19702-9ed4a131fcfadb9d@postgresql.org

Thanks for the pointer, continuing over in that thread.

--
Daniel Gustafsson