-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
encoding/base64 Decode panics if given too short dst #54532
Comments
Duplicate of #50117 |
Probably could workaround using #53693 if it is accepted. |
@seankhliao #50117 was closed on the grounds that it's fine to panic if you pass in If we (@natefinch, me, whoever) put together a PR to return an error on insufficient capacity, would that get a fair chance at review? |
In my opinion, the "invalid data is ok to panic on" is not applicable here. You can't know your buffer is invalid until you try (inside certain parameters). Is that 44 byte base64 string going to fit in my 32 byte buffer? Maybe. Maybe not. If the answer is to make a bigger buffer, then it defeats the purpose of being able to pass in my own buffer. That's the reason you use this API, so you can pass in a buffer to fill. |
it's easy to check, the last character of input must be |
@seankhliao then why doesn't the base64 package check that for me? What's the point of having a package for the format if I have to know the nitty gritty details of the encoding rules in order to keep the package from panicking? |
What version of Go are you using (
go version
)?go 1.18
Does this issue reproduce with the latest release?
yes
What did you do?
https://go.dev/play/p/kPdFvPbf4iJ
What set this off is that I accidentally set the wrong value for an environment variable, which got sent into base64.Decode, which panicked. That seemed weird because I would not expect the standard library to panic... especially since Decode returns an error anyway, so I would expect it to just... return an error.
I did some googling, and saw some people say "oh, just use DecodedLen to figure out if your buffer is big enough" ... so I was like, ok, I'll do that. Except that DecodedLen gives the maximum size your buffer needs to be, not the actual size. So, some encoded strings will appear to need a larger buffer than they actually require. Our fake key used in tests is a good example:
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
... decodedLen gives you 33, but it actually decodes to 32. If you only have a 32 byte buffer... there's no way to check beforehand that it'll fit. You just have to try it. And then the standard library might panic, you can't know.To properly write code that simply tries to decode a string that should be a 32 byte value and not panic... I had to write something like this (which seems kinda ridiculous)
What did you expect to see?
Preferably, just return an error if the slice is too small.
At the very least give me a better panic message than an index out of range error.
What did you see instead?
The text was updated successfully, but these errors were encountered: