ferama / rospo

🐸 Simple, reliable, persistent ssh tunnels with embedded ssh server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

user.Current may return err in some cases on Windows

ceclin opened this issue · comments

commented
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x40 pc=0xae5ea2]

goroutine 1 [running]:
github.com/ferama/rospo/cmd.init.1()
        github.com/ferama/rospo/cmd/grabpubkey.go:15 +0x62

golang/go#37348

I think it is better to use os.Getwd or empty string as a fallback.

You are right, thanks for reporting. The grabpubkey subcommand is not the only one that suffer from this issue. rospo needs better error handling for this scenario

commented

Haha, in fact, this error will be triggered in sshc subcommand first. I fixed that and found not only sshc, so I opened this issue.

As a temporary fix, I adopted the following fallback strategy for home dir:

  1. user.Current()
  2. os.LookupEnv("HOME")
  3. os.Getwd()
  4. default to "/"

And the following fallback strategy for username:

  1. user.Current()
  2. os.LookupEnv("USER")
  3. default to "root"

This may serve as a reference for you.

By the way, it is interesting that I encountered a similar situation on linux, but I didn't get into why. (That's why I use root as username fallback).

Would you like to contribute with a pull request? We could define a couple of functions into the utils/helpers.go file that return homeDir and username. Then we should use those function everywhere the user.Current() is actually used

commented

I was too busy the past few days.
A pull request is created just now. Review please.