Skip to content

panic: runtime error: slice bounds out of range #2

@florianjs

Description

@florianjs

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions