go2p icon indicating copy to clipboard operation
go2p copied to clipboard

panic: runtime error: slice bounds out of range

Open florianjs opened this issue 5 years ago • 2 comments
trafficstars

Hi :) I have an issue when I try to send a message from a server (Terminal 1) to more than one client (Terminal 2 and 3)

Problem Description:

panic: runtime error: slice bounds out of range [:535024639] with capacity 359

goroutine 36 [running]:
github.com/v-braun/go2p.middlewareHeadersImpl(0xc00040c000, 0xc00040c0a0, 0xc000050080, 0x1, 0x1, 0x0)
        C:/Users/argau/go/src/github.com/v-braun/go2p/middleware_headers.go:57 +0x45b
github.com/v-braun/go2p.(*Pipe).process(0xc00040c0a0, 0xc000050080, 0x4, 0x4)   
        C:/Users/argau/go/src/github.com/v-braun/go2p/pipe.go:63 +0x3b0
github.com/v-braun/go2p.(*Peer).processPipe(0xc00040c000, 0xc000050080, 0x1)    
        C:/Users/argau/go/src/github.com/v-braun/go2p/peer.go:71 +0x93
github.com/v-braun/go2p.(*Peer).start.func3()
        C:/Users/argau/go/src/github.com/v-braun/go2p/peer.go:48 +0x1a7
github.com/v-braun/awaiter.(*awaiter).Go.func1(0xc00040e0e0, 0xc00040e020)      
        C:/Users/argau/go/src/github.com/v-braun/awaiter/awaiter.go:51 +0x59    
created by github.com/v-braun/awaiter.(*awaiter).Go
        C:/Users/argau/go/src/github.com/v-braun/awaiter/awaiter.go:49 +0x6a    
exit status 2
package main

import (
	"bufio"
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/fatih/color"
	"github.com/sirupsen/logrus"
	"github.com/v-braun/go2p"
	prefixed "github.com/x-cray/logrus-prefixed-formatter"
)

func main() {
	logrus.SetFormatter(new(prefixed.TextFormatter))
	logrus.SetOutput(os.Stdout)
	logrus.SetLevel(logrus.DebugLevel)

	/* 	localHost := "localhost:7071"
	 */
	localAddr := flag.String("laddr", "localhost:7071", "local ip address")
	flag.Parse()

	cyan := color.New(color.FgCyan).SprintFunc()
	blue := color.New(color.FgHiBlue).SprintFunc()
	green := color.New(color.FgHiGreen).SprintFunc()
	white := color.New(color.FgHiWhite).SprintFunc()
	peerName := color.New(color.BgBlue, color.FgHiWhite).SprintFunc()

	/* 	if localHost == "localhost:7071" {
	   		peerName = color.New(color.BgHiMagenta, color.FgHiWhite).SprintFunc()
	   	} else if localHost == "localhost:7072" {
	   		peerName = color.New(color.BgHiYellow, color.FgHiWhite).SprintFunc()
	   	} */

	net := go2p.NewNetworkConnectionTCP(*localAddr, &map[string]func(peer *go2p.Peer, msg *go2p.Message){
		"msg": func(peer *go2p.Peer, msg *go2p.Message) {
			fmt.Println(fmt.Sprintf("%s %s", peerName(peer.RemoteAddress()+" > "), msg.PayloadGetString()))
		},
	})

	err := net.Start()
	if err != nil {
		panic(err)
	}

	net.OnPeer(func(p *go2p.Peer) {
		fmt.Printf("%s %s\n", cyan("new peer:"), green(p.RemoteAddress()))
	})

	defer net.Stop()

	fmt.Println(cyan(`
local server started!
press:
	`))

	fmt.Printf("%s %s\n", blue("[q][ENTER]"), white("to exit"))
	fmt.Printf("%s %s\n", blue("[c {ip address : port}][ENTER]"), white("to connect to another peer"))
	fmt.Printf("%s %s\n", blue("[any message][ENTER]"), white("to send a message to all peers"))

	reader := bufio.NewReader(os.Stdin)
	for {
		text, _ := reader.ReadString('\n')
		text = strings.TrimSpace(text)
		if text == "q" {
			return
		} else if strings.HasPrefix(text, "c ") {
			text = strings.TrimPrefix(text, "c ")
			net.ConnectTo("tcp", text)
		} else {
			net.SendBroadcast(go2p.NewMessageRoutedFromString("msg", text))
		}
	}
}

First Terminal go run main.go Second Terminal go run main.go -laddr=localhost:7072 c localhost:7071 Third Terminal go run main.go -laddr=localhost:7073 c localhost:7071

First Terminal Send A Message

Second or Third Terminal will panic

Environment:

  • Windows 10
  • Latest Verison of Go2p

florianjs avatar Apr 13 '20 13:04 florianjs

Server :

[0017] DEBUG middleware_log: tcp:127.0.0.1:59529 out-
> (358 bytes) - local endpoint: tcp:127.0.0.1:7071

Client :

[0016] DEBUG pipe: execute middleware msg-len
m=300 name=log op=Receive pos=3
[0016] DEBUG middleware_log: tcp:127.0.0.1:7071 <--in
 (300 bytes) - local endpoint: tcp:127.0.0.1:59224
[0016] DEBUG pipe: execute middleware msg-len
m=300 name=Crypt op=Receive pos=2
[0016] DEBUG pipe: execute middleware msg-len
m=643 name=log op=Receive pos=3
[0016] DEBUG middleware_log: tcp:127.0.0.1:7071 <--in
 (643 bytes) - local endpoint: tcp:127.0.0.1:59224
[0016] DEBUG pipe: execute middleware msg-len
m=643 name=Crypt op=Receive pos=2
[0016] DEBUG pipe: execute middleware msg-len
m=359 name=headers op=Receive pos=1
panic: runtime error: slice bounds out of range [:2133614430] with capacity 358

I don't know where this [:2133614430] came from?

header := full[8 : 8+headerSize] 

line 57 middleware_headers I guess ?

florianjs avatar Apr 13 '20 15:04 florianjs

Very detailed information and logs! Thanks a lot I will check this issue asap.

v-braun avatar Sep 21 '20 09:09 v-braun