Skip to content

Commit

Permalink
Fixed segfault when gouroboros is called without address or socket args.
Browse files Browse the repository at this point in the history
Changed os.RemoveAll to os.Remove as the former is dangerous.

Added proper help output for gouroboros command.
  • Loading branch information
rakshasa committed Feb 13, 2024
1 parent 5c8e061 commit 0e4560b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
48 changes: 30 additions & 18 deletions cmd/gouroboros/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"net"
"os"

"github.com/blinklabs-io/gouroboros"
ouroboros "github.com/blinklabs-io/gouroboros"
)

type globalFlags struct {
Expand Down Expand Up @@ -89,26 +89,38 @@ func main() {
f.networkMagic = int(network.NetworkMagic)
}

if len(f.flagset.Args()) > 0 {
switch f.flagset.Arg(0) {
case "chain-sync":
testChainSync(f)
case "local-tx-submission":
testLocalTxSubmission(f)
case "server":
testServer(f)
case "query":
testQuery(f)
case "mem-usage":
testMemUsage(f)
default:
fmt.Printf("Unknown subcommand: %s\n", f.flagset.Arg(0))
os.Exit(1)
subcommands := map[string]func(*globalFlags){
"chain-sync": testChainSync,
"local-tx-submission": testLocalTxSubmission,
"server": testServer,
"query": testQuery,
"mem-usage": testMemUsage,
}

if len(f.flagset.Args()) == 0 {
fmt.Printf("\n")
fmt.Printf("Usage of gouroboros:\n")
fmt.Printf("\n")
fmt.Printf("Commands:\n")

for k := range subcommands {
fmt.Printf(" %s\n", k)
}
} else {
fmt.Printf("You must specify a subcommand (chain-sync or local-tx-submission)\n")

fmt.Printf("\n")
fmt.Printf("Global flags:\n")
f.flagset.PrintDefaults()

os.Exit(1)
}

fn, ok := subcommands[f.flagset.Arg(0)]
if !ok {
fmt.Printf("Unknown subcommand: %s\n", f.flagset.Arg(0))
os.Exit(1)
}

fn(f)
}

func createClientConnection(f *globalFlags) net.Conn {
Expand Down
14 changes: 10 additions & 4 deletions cmd/gouroboros/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ package main
import (
"flag"
"fmt"
ouroboros "github.com/blinklabs-io/gouroboros"
"net"
"os"

ouroboros "github.com/blinklabs-io/gouroboros"
)

type serverFlags struct {
Expand All @@ -38,20 +39,25 @@ func newServerFlags() *serverFlags {
func createListenerSocket(f *globalFlags) (net.Listener, error) {
var err error
var listen net.Listener
if f.socket != "" {
if err := os.RemoveAll(f.socket); err != nil {

switch {
case f.socket != "":
if err := os.Remove(f.socket); err != nil {
return nil, fmt.Errorf("failed to remove existing socket: %s", err)
}
listen, err = net.Listen("unix", f.socket)
if err != nil {
return nil, fmt.Errorf("failed to open listening socket: %s", err)
}
} else if f.address != "" {
case f.address != "":
listen, err = net.Listen("tcp", f.address)
if err != nil {
return nil, fmt.Errorf("failed to open listening socket: %s", err)
}
default:
return nil, fmt.Errorf("no listening address or socket specified")
}

return listen, nil
}

Expand Down

0 comments on commit 0e4560b

Please sign in to comment.