Skip to content
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

crypto/cipher: StreamReader.Read() panics with slice bounds out of range #16487

Closed
riobard opened this issue Jul 25, 2016 · 5 comments
Closed

Comments

@riobard
Copy link

riobard commented Jul 25, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?

    1.6.3

  2. What operating system and processor architecture are you using (go env)?

    darwin/amd64

  3. What did you do?
    If possible, provide a recipe for reproducing the error.
    A complete runnable program is good.
    A link on play.golang.org is best.

    https://play.golang.org/p/K9hAxbE5fL

  4. What did you expect to see?

    It should not panic.

  5. What did you see instead?

    panic: runtime error: slice bounds out of range

@bradfitz bradfitz changed the title crypto/cipher.StreamReader.Read() panics with slice bounds out of range. crypto/cipher: StreamReader.Read() panics with slice bounds out of range Jul 25, 2016
@riobard
Copy link
Author

riobard commented Jul 25, 2016

The issue happens because StreamReader.Read() does not check for errors from the embedded Reader.Read().

Relevant code here:

r.S.XORKeyStream(dst[:n], dst[:n])

A simple fix would be:

func (r StreamReader) Read(dst []byte) (n int, err error) {
    n, err = r.R.Read(dst)
    if n <= 0 { return }
    r.S.XORKeyStream(dst[:n], dst[:n])
    return
}

@minux
Copy link
Member

minux commented Jul 25, 2016 via email

@riobard
Copy link
Author

riobard commented Jul 25, 2016

@minux It can happen, for example with syscall.Read due to the convention of returning negative numbers for errors.

@minux
Copy link
Member

minux commented Jul 25, 2016 via email

@bradfitz
Copy link
Contributor

Yeah, there's nothing to fix here. -1 is not a valid return value from an io.Reader, which has a different contract from the syscall package.

Normally when a function returns a value and an error, the value is ignored if the error is non-nil, but io.Reader specifically says that both the integer and error are used.

As @minux pointed out, it's documented as needing to be >= 0.

@golang golang locked and limited conversation to collaborators Jul 25, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants