limit buffer usage

Signed-off-by: Vasiliy Tolstov <v.tolstov@selfip.ru>
This commit is contained in:
Василий Толстов 2017-06-18 21:01:01 +03:00
parent 1413d28267
commit c8905c0ea9
4 changed files with 10 additions and 14 deletions

View File

@ -244,16 +244,11 @@ func (enc *TightPngEncoding) Read(c Conn, rect *Rectangle) error {
cmp := enc.TightCC.Compression cmp := enc.TightCC.Compression
switch cmp { switch cmp {
case TightCompressionPNG: case TightCompressionPNG:
buf := bytes.NewBuffer(nil)
l, err := readTightLength(c) l, err := readTightLength(c)
if err != nil { if err != nil {
return err return err
} }
_, err = io.CopyN(buf, c, int64(l)) enc.Image, err = png.Decode(io.LimitReader(c, int64(l)))
if err != nil {
return err
}
enc.Image, err = png.Decode(buf)
if err != nil { if err != nil {
return err return err
} }

View File

@ -4,11 +4,17 @@ import (
"context" "context"
"log" "log"
"net" "net"
"net/http"
_ "net/http/pprof"
vnc "github.com/vtolstov/go-vnc" vnc "github.com/vtolstov/go-vnc"
) )
func main() { func main() {
go func() {
log.Println(http.ListenAndServe(":6060", nil))
}()
ln, err := net.Listen("tcp", ":5900") ln, err := net.Listen("tcp", ":5900")
if err != nil { if err != nil {
log.Fatalf("Error listen. %v", err) log.Fatalf("Error listen. %v", err)

View File

@ -114,6 +114,7 @@ func colorsToImage(x, y, width, height uint16, colors []Color) *image.RGBA64 {
// Marshal implements the Marshaler interface. // Marshal implements the Marshaler interface.
func (r *Rectangle) Write(c Conn) error { func (r *Rectangle) Write(c Conn) error {
var err error var err error
if err = binary.Write(c, binary.BigEndian, r.X); err != nil { if err = binary.Write(c, binary.BigEndian, r.X); err != nil {
return err return err
@ -131,10 +132,7 @@ func (r *Rectangle) Write(c Conn) error {
return err return err
} }
if err := r.Enc.Write(c, r); err != nil { return r.Enc.Write(c, r)
return err
}
return c.Flush()
} }
func (r *Rectangle) Read(c Conn) error { func (r *Rectangle) Read(c Conn) error {

View File

@ -416,10 +416,7 @@ func (*Bell) Read(c Conn) (ServerMessage, error) {
} }
func (msg *Bell) Write(c Conn) error { func (msg *Bell) Write(c Conn) error {
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil { return binary.Write(c, binary.BigEndian, msg.Type())
return err
}
return c.Flush()
} }
type SetColorMapEntries struct { type SetColorMapEntries struct {