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

Minor bug #54

Merged
merged 1 commit into from
Jun 20, 2017
Merged

Minor bug #54

merged 1 commit into from
Jun 20, 2017

Conversation

namaho
Copy link

@namaho namaho commented Jun 19, 2017

But it causes runtime panic when indexing specific size domain name

`panic: runtime error: slice bounds out of range

goroutine 2026854 [running]:
github.com/shadowsocks/go-shadowsocks2/socks.ReadAddr(0x7f3db541df80, 0xc429c676e0, 0xc429c676e0, 0x7f3db541df80, 0xc429c676e0, 0x0, 0xc420021f30)`

@riobard
Copy link

riobard commented Jun 19, 2017

Thanks for the PR! I do not fully understand the bug. Why does it happen and why does the change fix it?

@namaho
Copy link
Author

namaho commented Jun 19, 2017

b[1] is of type uint8, with a maximum value 255. When b[1] do calculation with numbers, the return value would be also an uint8. If the size of the domain name exceed 251, say 252 for example, then the result of 2+b[1]+2 would be truncated to 0, while what we expect is 256, thus we should cast b[1] to a larger type of value, then we are safe to do the calculation.

A snippet of code would be more clear.

package main

import (
	"fmt"
)

func main() {
	b := make([]byte, 4)
	b[0] = byte(250)
	b[1] = byte(251)
	b[2] = byte(252)
	b[3] = byte(253)
	
	k0 := 2 + b[0] + 2
	k1 := 2 + b[1] + 2
	k2 := 2 + b[2] + 2
	k3 := 2 + b[3] + 2
	
	fmt.Printf("%v\n", k0)
	fmt.Printf("%v\n", k1)
	fmt.Printf("%v\n", k2)
	fmt.Printf("%v\n", k3)
}

Output:

254
255
0
1

@riobard
Copy link

riobard commented Jun 20, 2017

Ah I see! THAT is a tricky one! :D

One more question: wouldn't we need to change the line on 58,59,86 as well?

socks/socks.go Outdated
@@ -82,7 +82,7 @@ func ReadAddr(r io.Reader) (Addr, error) {
if err != nil {
return nil, err
}
_, err = io.ReadFull(r, b[2:2+b[1]+2])
_, err = io.ReadFull(r, b[2:2+int(b[1])+2])
return b[:1+1+b[1]+2], err
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be b[:1+1+int(b[1])+2]?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the right way to parse the address.

@riobard riobard merged commit 4ad831b into shadowsocks:master Jun 20, 2017
@riobard
Copy link

riobard commented Jun 20, 2017

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants