Fix magic SSL reason codes.

SSL reason codes corresponding to alerts have special values. Teach
make_errors.go that values above 1000 are reserved (otherwise it will assign
new values in that namespace). Also fix all the existing reason codes which
corresponded to alerts.

Change-Id: Ieabdf8fd59f4802938616934e1d84e659227cf84
Reviewed-on: https://boringssl-review.googlesource.com/1212
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/util/make_errors.go b/util/make_errors.go
index 20b3e2d..f770e3d 100644
--- a/util/make_errors.go
+++ b/util/make_errors.go
@@ -28,6 +28,11 @@
 	"unicode"
 )
 
+// ssl.h reserves values 1000 and above for error codes corresponding to
+// alerts. If automatically assigned reason codes exceed this value, this script
+// will error. This must be kept in sync with SSL_AD_REASON_OFFSET in ssl.h.
+const reservedReasonCode = 1000
+
 var resetFlag *bool = flag.Bool("reset", false, "If true, ignore current assignments and reassign from scratch")
 
 func makeErrors(reset bool) error {
@@ -59,7 +64,14 @@
 	if reset {
 		err = nil
 		functions = make(map[string]int)
-		reasons = make(map[string]int)
+		// Retain any reason codes above reservedReasonCode.
+		newReasons := make(map[string]int)
+		for key, value := range reasons {
+			if value >= reservedReasonCode {
+				newReasons[key] = value
+			}
+		}
+		reasons = newReasons
 	}
 
 	if err != nil {
@@ -87,8 +99,8 @@
 		}
 	}
 
-	assignNewValues(functions)
-	assignNewValues(reasons)
+	assignNewValues(functions, -1)
+	assignNewValues(reasons, reservedReasonCode)
 
 	headerFile, err = os.Open(headerPath)
 	if err != nil {
@@ -294,10 +306,13 @@
 	}
 }
 
-func assignNewValues(assignments map[string]int) {
+func assignNewValues(assignments map[string]int, reserved int) {
 	max := 99
 
 	for _, value := range assignments {
+		if reserved >= 0 && value >= reserved {
+			continue
+		}
 		if value > max {
 			max = value
 		}
@@ -307,6 +322,11 @@
 
 	for key, value := range assignments {
 		if value == -1 {
+			if reserved >= 0 && max >= reserved {
+				// If this happens, try passing
+				// -reset. Otherwise bump up reservedReasonCode.
+				panic("Automatically-assigned values exceeded limit!")
+			}
 			assignments[key] = max
 			max++
 		}