Pull request: 3987 Fix nil pointer dereference

Merge in DNS/adguard-home from 3987-fix-nil-deref to master

Updates #3987.
Updates #2846.

Squashed commit of the following:

commit d653e09ce88a8b10b2a17fea1563c419895c714c
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Dec 23 20:08:51 2021 +0300

    all: log changes

commit c47a4eeacf76fa7df2d01af166dee9d52528ac58
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Dec 23 19:22:39 2021 +0300

    aghnet: fix windows tests

commit 9c91f14ccfe967ada3c00ddb86d673238e52c12d
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Dec 23 19:09:49 2021 +0300

    aghnet: imp code readability, docs

commit d3df15d1892e4ebfe7f8ea7144e39a0c712fce52
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Dec 23 18:47:28 2021 +0300

    aghnet: fix nil pointer dereference
This commit is contained in:
Eugene Burkov 2021-12-23 20:16:08 +03:00
parent d9df7c13be
commit 201ef10de6
5 changed files with 33 additions and 2 deletions

View file

@ -27,11 +27,16 @@ and this project adheres to
--> -->
- Go 1.17 support. v0.109.0 will require at least Go 1.18 to build. - Go 1.17 support. v0.109.0 will require at least Go 1.18 to build.
### Fixed
- Panic on port availability check during installation ([#3987]).
### Removed ### Removed
- Go 1.16 support. - Go 1.16 support.
[#3057]: https://github.com/AdguardTeam/AdGuardHome/issues/3057 [#3057]: https://github.com/AdguardTeam/AdGuardHome/issues/3057
[#3987]: https://github.com/AdguardTeam/AdGuardHome/issues/3987

View file

@ -187,7 +187,8 @@ func GetSubnet(ifaceName string) *net.IPNet {
return nil return nil
} }
// CheckPort checks if the port is available for binding. // CheckPort checks if the port is available for binding. network is expected
// to be one of "udp" and "tcp".
func CheckPort(network string, ip net.IP, port int) (err error) { func CheckPort(network string, ip net.IP, port int) (err error) {
var c io.Closer var c io.Closer
addr := netutil.IPPort{IP: ip, Port: port}.String() addr := netutil.IPPort{IP: ip, Port: port}.String()
@ -200,7 +201,11 @@ func CheckPort(network string, ip net.IP, port int) (err error) {
return nil return nil
} }
return errors.WithDeferred(err, closePortChecker(c)) if err != nil {
return err
}
return closePortChecker(c)
} }
// IsAddrInUse checks if err is about unsuccessful address binding. // IsAddrInUse checks if err is about unsuccessful address binding.

View file

@ -5,6 +5,8 @@ import (
"testing" "testing"
"github.com/AdguardTeam/AdGuardHome/internal/aghtest" "github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -69,3 +71,20 @@ func TestBroadcastFromIPNet(t *testing.T) {
}) })
} }
} }
func TestCheckPort(t *testing.T) {
l, err := net.Listen("tcp", "127.0.0.1:")
require.NoError(t, err)
testutil.CleanupAndRequireSuccess(t, l.Close)
ipp := netutil.IPPortFromAddr(l.Addr())
require.NotNil(t, ipp)
require.NotNil(t, ipp.IP)
require.NotZero(t, ipp.Port)
err = CheckPort("tcp", ipp.IP, ipp.Port)
target := &net.OpError{}
require.ErrorAs(t, err, &target)
assert.Equal(t, "listen", target.Op)
}

View file

@ -10,6 +10,7 @@ import (
"github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/errors"
) )
// closePortChecker closes c. c must be non-nil.
func closePortChecker(c io.Closer) (err error) { func closePortChecker(c io.Closer) (err error) {
return c.Close() return c.Close()
} }

View file

@ -25,6 +25,7 @@ func ifaceSetStaticIP(string) (err error) {
return aghos.Unsupported("setting static ip") return aghos.Unsupported("setting static ip")
} }
// closePortChecker closes c. c must be non-nil.
func closePortChecker(c io.Closer) (err error) { func closePortChecker(c io.Closer) (err error) {
if err = c.Close(); err != nil { if err = c.Close(); err != nil {
return err return err