From 82f3d55fa482926c40b245ffafc7b9ca27d28f7d Mon Sep 17 00:00:00 2001 From: Santhosh Manohar Date: Sun, 6 Mar 2016 10:03:03 -0800 Subject: [PATCH] Fix out of bound slice access in Delete() Signed-off-by: Santhosh Manohar --- libnetwork/etchosts/etchosts.go | 4 ++++ libnetwork/etchosts/etchosts_test.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) 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 {