Skip to content

Commit 4fbfa1d

Browse files
committed
Fix possible NPEs
1 parent a77069a commit 4fbfa1d

File tree

14 files changed

+99
-35
lines changed

14 files changed

+99
-35
lines changed

pkg/config/loader.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ func (e *Env) Read() (Kv, error) {
8888
mp := make(Kv)
8989
for _, k := range keys {
9090
parts := strings.SplitN(k, "=", 2)
91+
if parts == nil {
92+
continue
93+
}
9194
n := strings.ToLower(strings.TrimPrefix(parts[0], string(*e)))
9295
if n == "" {
9396
continue
@@ -102,7 +105,9 @@ func (e *Env) Read() (Kv, error) {
102105
} else {
103106
key = strings.Replace(n[:x+1], "_", ".", -1) + n[x+2:]
104107
}
105-
mp[key] = parts[1]
108+
if len(parts) > 1 {
109+
mp[key] = parts[1]
110+
}
106111
}
107112
return maps.Unflatten(mp, "."), nil
108113
}

pkg/coordinator/userhandlers.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,16 @@ func (u *User) handleGetWorkerList(debug bool, info HasServerInfo) {
127127
if debug {
128128
response.Servers = servers
129129
} else {
130-
// not sure if []byte to string always reversible :/
131130
unique := map[string]*api.Server{}
132131
for _, s := range servers {
133132
mid := s.Machine
134133
if _, ok := unique[mid]; !ok {
135134
unique[mid] = &api.Server{Addr: s.Addr, PingURL: s.PingURL, Id: s.Id, InGroup: true}
136135
}
137-
unique[mid].Replicas++
136+
v := unique[mid]
137+
if v != nil {
138+
v.Replicas++
139+
}
138140
}
139141
for _, v := range unique {
140142
response.Servers = append(response.Servers, *v)

pkg/encoder/encoder.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func NewVideoEncoder(w, h, dw, dh int, scale float64, conf config.Video, log *lo
6262
if err != nil {
6363
return nil, err
6464
}
65+
if enc == nil {
66+
return nil, fmt.Errorf("no encoder")
67+
}
6568

6669
return &Video{codec: enc, y: yuv.NewYuvConv(w, h, scale), log: log}, nil
6770
}
@@ -86,6 +89,10 @@ func (v *Video) Info() string {
8689
}
8790

8891
func (v *Video) SetPixFormat(f uint32) {
92+
if v == nil {
93+
return
94+
}
95+
8996
switch f {
9097
case 1:
9198
v.pf = yuv.PixFmt(yuv.FourccArgb)
@@ -98,22 +105,37 @@ func (v *Video) SetPixFormat(f uint32) {
98105

99106
// SetRot sets the de-rotation angle of the frames.
100107
func (v *Video) SetRot(a uint) {
108+
if v == nil {
109+
return
110+
}
111+
101112
if a > 0 {
102113
v.rot = (a + 180) % 360
103114
}
104115
}
105116

106117
// SetFlip tells the encoder to flip the frames vertically.
107-
func (v *Video) SetFlip(b bool) { v.codec.SetFlip(b) }
118+
func (v *Video) SetFlip(b bool) {
119+
if v == nil {
120+
return
121+
}
122+
v.codec.SetFlip(b)
123+
}
108124

109125
func (v *Video) Stop() {
126+
if v == nil {
127+
return
128+
}
129+
110130
if v.stopped.Swap(true) {
111131
return
112132
}
113133
v.rot = 0
114134

115135
defer func() { v.codec = nil }()
116136
if err := v.codec.Shutdown(); err != nil {
117-
v.log.Error().Err(err).Msg("failed to close the encoder")
137+
if v.log != nil {
138+
v.log.Error().Err(err).Msg("failed to close the encoder")
139+
}
118140
}
119141
}

pkg/encoder/h264/x264_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ func TestH264Encode(t *testing.T) {
66
h264, err := NewEncoder(120, 120, 0, nil)
77
if err != nil {
88
t.Error(err)
9+
return
910
}
1011
data := make([]byte, 120*120*1.5)
1112
h264.LoadBuf(data)
@@ -20,6 +21,7 @@ func Benchmark(b *testing.B) {
2021
h264, err := NewEncoder(w, h, 0, nil)
2122
if err != nil {
2223
b.Error(err)
24+
return
2325
}
2426
data := make([]byte, int(float64(w)*float64(h)*1.5))
2527
for i := 0; i < b.N; i++ {

pkg/network/address.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package network
22

33
import (
44
"errors"
5+
"net"
56
"strconv"
67
"strings"
78
)
@@ -12,15 +13,17 @@ func (a *Address) Port() (int, error) {
1213
if len(string(*a)) == 0 {
1314
return 0, errors.New("no address")
1415
}
15-
parts := strings.Split(string(*a), ":")
16-
var port string
17-
if len(parts) == 1 {
18-
port = parts[0]
19-
} else {
20-
port = parts[len(parts)-1]
16+
addr := replaceAllExceptLast(string(*a), ":", "_")
17+
_, port, err := net.SplitHostPort(addr)
18+
if err != nil {
19+
return 0, err
2120
}
2221
if val, err := strconv.Atoi(port); err == nil {
2322
return val, nil
2423
}
2524
return 0, errors.New("port is not a number")
2625
}
26+
27+
func replaceAllExceptLast(s, c, x string) string {
28+
return strings.Replace(s, c, x, strings.Count(s, c)-1)
29+
}

pkg/network/httpx/server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ func (s *Server) run() {
120120
s.log.Debug().Msgf("Starting %s server on %s", protocol, s.Addr)
121121

122122
if s.opts.Https && s.opts.HttpsRedirect {
123-
rdr, err := s.redirection()
124-
if err != nil {
123+
if rdr, err := s.redirection(); err == nil {
124+
s.redirect = rdr
125+
go s.redirect.Run()
126+
} else {
125127
s.log.Error().Err(err).Msg("couldn't init redirection server")
126128
}
127-
s.redirect = rdr
128-
go s.redirect.Run()
129129
}
130130

131131
var err error
@@ -165,6 +165,7 @@ func (s *Server) redirection() (*Server, error) {
165165
address = s.opts.HttpsDomain
166166
}
167167
addr := buildAddress(address, s.opts.Zone, *s.listener)
168+
s.log.Info().Str("addr", addr).Msg("Start HTTPS redirect server")
168169

169170
srv, err := NewServer(s.opts.HttpsRedirectAddress, func(serv *Server) Handler {
170171
h := NewServeMux("")
@@ -186,7 +187,6 @@ func (s *Server) redirection() (*Server, error) {
186187
},
187188
WithLogger(s.log),
188189
)
189-
s.log.Info().Str("addr", addr).Msg("Start HTTPS redirect server")
190190
return srv, err
191191
}
192192

pkg/worker/caged/libretro/caged.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func (c *Caged) ReloadFrontend() {
3838
frontend, err := NewFrontend(c.conf.Emulator, c.log)
3939
if err != nil {
4040
c.log.Fatal().Err(err).Send()
41+
return
4142
}
4243
c.Emulator = frontend
4344
c.base = frontend
@@ -47,6 +48,9 @@ func (c *Caged) ReloadFrontend() {
4748
func (c *Caged) VideoChangeCb(fn func()) { c.base.SetVideoChangeCb(fn) }
4849

4950
func (c *Caged) Load(game games.GameMetadata, path string) error {
51+
if c.Emulator == nil {
52+
return nil
53+
}
5054
c.Emulator.LoadCore(game.System)
5155
if err := c.Emulator.LoadGame(game.FullPath(path)); err != nil {
5256
return err

pkg/worker/caged/libretro/frontend.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ func (f *Frontend) Shutdown() {
217217

218218
func (f *Frontend) linkNano(nano *nanoarch.Nanoarch) {
219219
f.nano = nano
220+
if nano == nil {
221+
return
222+
}
220223
f.nano.WaitReady() // start only when nano is available
221224

222225
f.nano.OnKeyPress = f.input.isKeyPressed
@@ -225,7 +228,11 @@ func (f *Frontend) linkNano(nano *nanoarch.Nanoarch) {
225228
f.nano.OnAudio = f.handleAudio
226229
}
227230

228-
func (f *Frontend) SetVideoChangeCb(fn func()) { f.nano.OnSystemAvInfo = fn }
231+
func (f *Frontend) SetVideoChangeCb(fn func()) {
232+
if f.nano != nil {
233+
f.nano.OnSystemAvInfo = fn
234+
}
235+
}
229236

230237
func (f *Frontend) Start() {
231238
f.log.Debug().Msgf("frontend start")

pkg/worker/caged/libretro/graphics/opengl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const (
5050

5151
var (
5252
opt = offscreenSetup{}
53-
buf []byte
53+
buf = make([]byte, 1024*1024)
5454
)
5555

5656
func initContext(getProcAddr func(name string) unsafe.Pointer) {

pkg/worker/caged/libretro/manager/grab.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ func (d GrabDownloader) Request(dest string, urls ...Download) (ok []string, noo
4747
r := resp.Request
4848
if err := resp.Err(); err != nil {
4949
d.log.Error().Err(err).Msgf("download [%s] %s has failed: %v", r.Label, r.URL(), err)
50-
if resp.HTTPResponse.StatusCode == 404 {
50+
if resp.HTTPResponse != nil && resp.HTTPResponse.StatusCode == 404 {
5151
nook = append(nook, resp.Request.Label)
5252
}
5353
} else {
54-
d.log.Info().Msgf("Downloaded [%v] [%s] -> %s", resp.HTTPResponse.Status, r.Label, resp.Filename)
54+
status := ""
55+
if resp.HTTPResponse != nil {
56+
status = resp.HTTPResponse.Status
57+
}
58+
d.log.Info().Msgf("Downloaded [%v] [%s] -> %s", status, r.Label, resp.Filename)
5559
ok = append(ok, resp.Filename)
5660
}
5761
}

0 commit comments

Comments
 (0)