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

use bufio.Reader may error #23

Open
ghost opened this issue Sep 16, 2013 · 1 comment
Open

use bufio.Reader may error #23

ghost opened this issue Sep 16, 2013 · 1 comment

Comments

@ghost
Copy link

ghost commented Sep 16, 2013

func ServConn(local_reader *bufio.Reader, local *net.TCPConn, dialer Dialer) error {
connections.Add(1)
defer local.Close()
defer connections.Done()

// SOCKS does not include a length in the header, so take
// a punt that each request will be readable in one go.
buf := make([]byte, 256)
n, err := local_reader.Read(buf)
if err != nil || n < 2 {
    //log.Printf("[%s] unable to read SOCKS header: %v", local.RemoteAddr(), err)
    return fmt.Errorf("[%s] unable to read SOCKS header: %v", local.RemoteAddr(), err)
}
buf = buf[:n]

.......
see http://golang.org/pkg/bufio/#Reader.Read
It calls Read at most once on the underlying Reader, hence n may be less than len(p).
if local_reader.Read(buf) return 4 bytes, it may fall, is it?

@yinqiwen
Copy link
Owner

Theoretically, it may fail. But in reality, the header part is sent by client at once after connected, Server have very little chance that read less data than such a few bytes.

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

No branches or pull requests

1 participant