Browse Source

Fix crash when removing an invalid zone file

Thanks to Alex Bligh

I think this closes #69 (Alex, please give me a test that fails if it doesn't)
Ask Bjørn Hansen 10 years ago
parent
commit
c9d0540288
2 changed files with 10 additions and 8 deletions
  1. 3 8
      zones.go
  2. 7 0
      zones_test.go

+ 3 - 8
zones.go

@@ -58,7 +58,7 @@ func zonesReadDir(dirName string, zones Zones) error {
 
 		if zone, ok := zones[zoneName]; !ok || file.ModTime().After(zone.LastRead) {
 			if ok {
-				log.Printf("Reloading %s\n", fileName)
+				logPrintf("Reloading %s\n", fileName)
 			} else {
 				logPrintf("Reading new file %s\n", fileName)
 			}
@@ -66,13 +66,8 @@ func zonesReadDir(dirName string, zones Zones) error {
 			//log.Println("FILE:", i, file, zoneName)
 			config, err := readZoneFile(zoneName, path.Join(dirName, fileName))
 			if config == nil || err != nil {
-				log.Println("Caught an error", err)
-				if config == nil {
-					config = new(Zone)
-				}
-				config.LastRead = file.ModTime()
-				zones[zoneName] = config
-				parseErr = err
+				parseErr = fmt.Errorf("Error reading zone '%s': %s", zoneName, err)
+				log.Println(parseErr.Error())
 				continue
 			}
 			config.LastRead = file.ModTime()

+ 7 - 0
zones_test.go

@@ -88,11 +88,18 @@ func (s *ConfigSuite) TestRemoveConfig(c *C) {
 		c.Fail()
 	}
 
+	err = ioutil.WriteFile(dir+"/invalid.example.org.json", []byte("not-json"), 0644)
+	if err != nil {
+		c.Log(err)
+		c.Fail()
+	}
+
 	zonesReadDir(dir, s.zones)
 	c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
 	c.Check(s.zones["test2.example.org"].Origin, Equals, "test2.example.org")
 
 	os.Remove(dir + "/test2.example.org.json")
+	os.Remove(dir + "/invalid.example.org.json")
 
 	zonesReadDir(dir, s.zones)
 	c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")