more robust: proper bound checks;
authorwenzelm
Sun, 05 Mar 2023 13:42:10 +0100
changeset 77513 43bfb65ee9b3
parent 77512 9853251b958e
child 77514 acaa89cb977b
more robust: proper bound checks;
src/Pure/Tools/build_process.scala
--- a/src/Pure/Tools/build_process.scala	Sun Mar 05 12:52:04 2023 +0100
+++ b/src/Pure/Tools/build_process.scala	Sun Mar 05 13:42:10 2023 +0100
@@ -147,6 +147,11 @@
     type Pending = List[Entry]
     type Running = Map[String, Build_Job]
     type Results = Map[String, Result]
+
+    def inc_serial(serial: Long): Long = {
+      require(serial < java.lang.Long.MAX_VALUE, "serial overflow")
+      serial + 1
+    }
   }
 
   // dynamic state of various instances, distinguished by uuid
@@ -159,6 +164,13 @@
     running: State.Running = Map.empty,     // presently running jobs
     results: State.Results = Map.empty      // finished results
   ) {
+    require(serial >= 0, "serial underflow")
+    def inc_serial: State = copy(serial = State.inc_serial(serial))
+    def set_serial(i: Long): State = {
+      require(serial <= i, "non-monotonic change of serial")
+      copy(serial = i)
+    }
+
     def echo(progress: Progress, message_serial: Long, message: Progress.Message): State =
       if (message_serial > progress_seen) {
         progress.output(message)
@@ -564,10 +576,10 @@
           Host.Data.update_numa_index(db, hostname, state.numa_index))
 
       val serial0 = get_serial(db)
-      val serial = if (changed.exists(identity)) serial0 + 1 else serial0
+      val serial = if (changed.exists(identity)) State.inc_serial(serial0) else serial0
 
       set_serial(db, serial)
-      state.copy(serial = serial)
+      state.set_serial(serial)
     }
   }
 }
@@ -638,7 +650,7 @@
 
     override def output(message: Progress.Message): Unit =
       synchronized_database {
-        val state1 = _state.copy(serial = _state.serial + 1)
+        val state1 = _state.inc_serial
         for (db <- _database) {
           Build_Process.Data.write_progress(db, state1.serial, message, build_context.uuid)
           Build_Process.Data.set_serial(db, state1.serial)