cherry-pick: 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-24 22:36:08 +03:00 committed by Ainar Garipov
parent d280151c18
commit 7528699fc2
5 changed files with 58 additions and 3 deletions

View File

@ -10,9 +10,38 @@ and this project adheres to
## [Unreleased] ## [Unreleased]
<!-- <!--
## [v0.107.1] - 2022-01-25 (APPROX.) ## [v0.108.0] - 2021-06-01 (APPROX.)
--> -->
### Added
- `windows/arm64` support ([#3057]).
### Deprecated
- Go 1.17 support. v0.109.0 will require at least Go 1.18 to build.
### Removed
- Go 1.16 support.
[#3057]: https://github.com/AdguardTeam/AdGuardHome/issues/3057
<!--
## [v0.107.1] - 2021-12-29 (APPROX.)
-->
## v0.107.1 - UNRELEASED
### Fixed
- Panic on port availability check during installation ([#3987]).
[#3987]: https://github.com/AdguardTeam/AdGuardHome/issues/3987
## [v0.107.0] - 2021-12-21 ## [v0.107.0] - 2021-12-21
### Added ### Added

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