From 7f69142aa01260bca812250c96a6a566a80e557a Mon Sep 17 00:00:00 2001 From: Rob Murray Date: Fri, 1 Mar 2024 16:52:29 +0000 Subject: [PATCH] resolv.conf comments have '#' or ';' in the first column When a '#' or ';' appears anywhere else, it's not a comment marker. Signed-off-by: Rob Murray --- libnetwork/internal/resolvconf/resolvconf.go | 14 ++------ libnetwork/resolvconf/resolvconf_unix_test.go | 35 +++++++++++-------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/libnetwork/internal/resolvconf/resolvconf.go b/libnetwork/internal/resolvconf/resolvconf.go index 6e49c37927..5e9278c48a 100644 --- a/libnetwork/internal/resolvconf/resolvconf.go +++ b/libnetwork/internal/resolvconf/resolvconf.go @@ -452,18 +452,8 @@ func UserModified(rcPath, rcHashPath string) (bool, error) { func (rc *ResolvConf) processLine(line string) { fields := strings.Fields(line) - // Strip comments. - // TODO(robmry) - ignore comment chars except in column 0. - // This preserves old behaviour, but it's wrong. For example, resolvers - // will honour the option in line "options # ndots:0" (and ignore the - // "#" as an unknown option). - for i, s := range fields { - if s[0] == '#' || s[0] == ';' { - fields = fields[:i] - break - } - } - if len(fields) == 0 { + // Strip blank lines and comments. + if len(fields) == 0 || fields[0][0] == '#' || fields[0][0] == ';' { return } diff --git a/libnetwork/resolvconf/resolvconf_unix_test.go b/libnetwork/resolvconf/resolvconf_unix_test.go index 6fcbe34381..86ccbf4106 100644 --- a/libnetwork/resolvconf/resolvconf_unix_test.go +++ b/libnetwork/resolvconf/resolvconf_unix_test.go @@ -149,16 +149,16 @@ func TestGetSearchDomains(t *testing.T) { result: []string{"example.com"}, }, { - input: `search example.com # ignored`, - result: []string{"example.com"}, + input: `search example.com # notignored`, + result: []string{"example.com", "#", "notignored"}, }, { input: ` search example.com `, result: []string{"example.com"}, }, { - input: ` search example.com # ignored`, - result: []string{"example.com"}, + input: ` search example.com # notignored`, + result: []string{"example.com", "#", "notignored"}, }, { input: `search foo.example.com example.com`, @@ -169,8 +169,8 @@ func TestGetSearchDomains(t *testing.T) { result: []string{"foo.example.com", "example.com"}, }, { - input: ` search foo.example.com example.com # ignored`, - result: []string{"foo.example.com", "example.com"}, + input: ` search foo.example.com example.com # notignored`, + result: []string{"foo.example.com", "example.com", "#", "notignored"}, }, { input: `nameserver 1.2.3.4 @@ -212,6 +212,9 @@ func TestGetOptions(t *testing.T) { { input: `# ignored`, }, + { + input: `; ignored`, + }, { input: `nameserver 1.2.3.4`, }, @@ -220,32 +223,36 @@ func TestGetOptions(t *testing.T) { result: []string{"opt1"}, }, { - input: `options opt1 # ignored`, - result: []string{"opt1"}, + input: `options opt1 # notignored`, + result: []string{"opt1", "#", "notignored"}, + }, + { + input: `options opt1 ; notignored`, + result: []string{"opt1", ";", "notignored"}, }, { input: ` options opt1 `, result: []string{"opt1"}, }, { - input: ` options opt1 # ignored`, - result: []string{"opt1"}, + input: ` options opt1 # notignored`, + result: []string{"opt1", "#", "notignored"}, }, { input: `options opt1 opt2 opt3`, result: []string{"opt1", "opt2", "opt3"}, }, { - input: `options opt1 opt2 opt3 # ignored`, - result: []string{"opt1", "opt2", "opt3"}, + input: `options opt1 opt2 opt3 # notignored`, + result: []string{"opt1", "opt2", "opt3", "#", "notignored"}, }, { input: ` options opt1 opt2 opt3 `, result: []string{"opt1", "opt2", "opt3"}, }, { - input: ` options opt1 opt2 opt3 # ignored`, - result: []string{"opt1", "opt2", "opt3"}, + input: ` options opt1 opt2 opt3 # notignored`, + result: []string{"opt1", "opt2", "opt3", "#", "notignored"}, }, { input: `nameserver 1.2.3.4