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

fix: write called without restricting size #4

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions tinygo/v0/examples/reverse/wrapper_transport.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package main

import (
"io"

v0 "github.com/refraction-networking/watm/tinygo/v0"
v0net "github.com/refraction-networking/watm/tinygo/v0/net"
)

var dupBuf []byte = make([]byte, 16384) // 16k buffer for reversing

// type guard: ReverseWrappingTransport must implement [v0.WrappingTransport].
var _ v0.WrappingTransport = (*ReverseWrappingTransport)(nil)

Expand All @@ -24,29 +20,27 @@ type ReverseConn struct {
}

func (rc *ReverseConn) Read(b []byte) (n int, err error) {
n, err = rc.Conn.Read(dupBuf)
tmpBuf := make([]byte, len(b))
n, err = rc.Conn.Read(tmpBuf)
if err != nil {
return 0, err
}

if n > len(b) {
err = io.ErrShortBuffer
n = len(b)
}

// reverse all bytes read successfully so far
for i := 0; i < n; i++ {
b[i] = dupBuf[n-i-1]
b[i] = tmpBuf[n-i-1]
}

return n, err
}

func (rc *ReverseConn) Write(b []byte) (n int, err error) {
tmpBuf := make([]byte, len(b))

// reverse the bytes to be written
for i := 0; i < len(b); i++ {
dupBuf[i] = b[len(b)-i-1]
tmpBuf[i] = b[len(b)-i-1]
}

return rc.Conn.Write(dupBuf)
return rc.Conn.Write(tmpBuf[:len(b)])
}