summaryrefslogtreecommitdiff
path: root/libnetwork/etchosts
diff options
context:
space:
mode:
authorSanthosh Manohar <santhosh@docker.com>2016-03-06 10:03:03 -0800
committerSanthosh Manohar <santhosh@docker.com>2016-03-06 10:03:03 -0800
commit82f3d55fa482926c40b245ffafc7b9ca27d28f7d (patch)
tree909542f0201215eb00d7cd67cf5120c42f6ed6d5 /libnetwork/etchosts
parentd734bc580745f065f5608bb6256dfbb8d7312738 (diff)
downloaddocker-82f3d55fa482926c40b245ffafc7b9ca27d28f7d.tar.gz
Fix out of bound slice access in Delete()
Signed-off-by: Santhosh Manohar <santhosh@docker.com>
Diffstat (limited to 'libnetwork/etchosts')
-rw-r--r--libnetwork/etchosts/etchosts.go4
-rw-r--r--libnetwork/etchosts/etchosts_test.go23
2 files changed, 27 insertions, 0 deletions
diff --git a/libnetwork/etchosts/etchosts.go b/libnetwork/etchosts/etchosts.go
index 99ea5dec77..4526532593 100644
--- a/libnetwork/etchosts/etchosts.go
+++ b/libnetwork/etchosts/etchosts.go
@@ -159,6 +159,10 @@ func Delete(path string, recs []Record) error {
loop:
for s.Scan() {
b := s.Bytes()
+ if len(b) == 0 {
+ continue
+ }
+
if b[0] == '#' {
buf.Write(b)
buf.Write(eol)
diff --git a/libnetwork/etchosts/etchosts_test.go b/libnetwork/etchosts/etchosts_test.go
index f897096da5..9d1663d322 100644
--- a/libnetwork/etchosts/etchosts_test.go
+++ b/libnetwork/etchosts/etchosts_test.go
@@ -331,6 +331,29 @@ func TestDeleteEmpty(t *testing.T) {
}
}
+func TestDeleteNewline(t *testing.T) {
+ file, err := ioutil.TempFile("", "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.Remove(file.Name())
+
+ b := []byte("\n")
+ if _, err := file.Write(b); err != nil {
+ t.Fatal(err)
+ }
+
+ rec := []Record{
+ {
+ Hosts: "prefix",
+ IP: "2.2.2.2",
+ },
+ }
+ if err := Delete(file.Name(), rec); err != nil {
+ t.Fatal(err)
+ }
+}
+
func TestDelete(t *testing.T) {
file, err := ioutil.TempFile("", "")
if err != nil {