Repository: syncthing/syncthing (language: go)

Issue:
lib/nat: panic: runtime error: index out of range
```
INFO: syncthing v1.27.5 "Gold Grasshopper" (go1.22.1 linux-amd64) debian@github.syncthing.net 2024-03-25 03:45:14 UTC [noupgrade]
Panic at 2024-04-08T14:59:35+02:00
panic: runtime error: index out of range [0] with length 0 [recovered]
	panic: runtime error: index out of range [0] with length 0

goroutine 77 [running]:
github.com/thejerf/suture/v4.(*Supervisor).runService.func2.2()
	github.com/thejerf/suture/v4@v4.0.2/supervisor.go:561 +0xb7
panic({0xf88620?, 0xc0004405a0?})
	runtime/panic.go:770 +0x132
github.com/syncthing/syncthing/lib/nat.(*Service).verifyExistingLocked(0xc00038e5b0, {0x1398080, 0xc000034b40}, 0xc0002bc480, 0xc00278af00, 0x1)
	github.com/syncthing/syncthing/lib/nat/service.go:261 +0xa85
github.com/syncthing/syncthing/lib/nat.(*Service).updateMapping(0xc00038e5b0, {0x1398080, 0xc000034b40}, 0xc0002bc480, 0xc00278af00, 0x1)
	github.com/syncthing/syncthing/lib/nat/service.go:216 +0x134
github.com/syncthing/syncthing/lib/nat.(*Service).process(0xc00038e5b0, {0x1398080, 0xc000034b40})
	github.com/syncthing/syncthing/lib/nat/service.go:149 +0x4d5
github.com/syncthing/syncthing/lib/nat.(*Service).Serve(0xc00038e5b0, {0x1398080, 0xc000034b40})
	github.com/syncthing/syncthing/lib/nat/service.go:96 +0x26f
github.com/thejerf/suture/v4.(*Supervisor).runService.func2()
	github.com/thejerf/suture/v4@v4.0.2/supervisor.go:565 +0xe6
created by github.com/thejerf/suture/v4.(*Supervisor).runService in goroutine 69
	github.com/thejerf/suture/v4@v4.0.2/supervisor.go:539 +0x18b
```

//@calmh


Discussion / hints:

Fix patch:
diff --git a/lib/nat/service.go b/lib/nat/service.go
index 8ff66a38076..d0644c31992 100644
--- a/lib/nat/service.go
+++ b/lib/nat/service.go
@@ -233,9 +233,8 @@ func (s *Service) verifyExistingLocked(ctx context.Context, mapping *Mapping, na
 		default:
 		}
 
-		// Delete addresses for NATDevice's that do not exist anymore
-		nat, ok := nats[id]
-		if !ok {
+		if nat, ok := nats[id]; !ok || len(extAddrs) == 0 {
+			// Delete addresses for NATDevice's that do not exist anymore
 			mapping.removeAddressLocked(id)
 			change = true
 			continue


Test patch: