Add some checks
authorMikael Berthe <mikael@lilotux.net>
Mon, 05 Sep 2016 23:42:09 +0200
changeset 2 d41c4f8fe066
parent 1 6a396d691a7d
child 3 8415b6bd06e9
Add some checks
takuzu.go
--- a/takuzu.go	Fri Sep 02 22:37:34 2016 +0200
+++ b/takuzu.go	Mon Sep 05 23:42:09 2016 +0200
@@ -40,12 +40,17 @@
 // NewFromString creates a new Takuzu board from a string definition
 func NewFromString(s string) (*Takuzu, error) {
 	l := len(s)
-	// TODO: validate chars ([.01OI])
+	if l < 4 {
+		return nil, errors.New("bad string length")
+	}
+
 	size := int(math.Sqrt(float64(l)))
 	if size*size != l {
 		return nil, errors.New("bad string length")
 	}
 
+	// TODO: validate chars ([.01OI])
+
 	i := 0
 	t := New(size)
 
@@ -117,11 +122,18 @@
 func BoardsMatch(t1, t2 *Takuzu, ignoreUndefined bool) (match bool, line, col int) {
 	match = true
 
+	if t1 == nil || t2 == nil {
+		line, col = -1, -1
+		match = false
+		return
+	}
+
 	if t1.Size != t2.Size {
 		line, col = -1, -1
 		match = false
 		return
 	}
+
 	for line = range t1.Board {
 		for col = range t1.Board[line] {
 			if !t1.Board[line][col].Defined || !t2.Board[line][col].Defined {