@amritk/nish-x86_64-linux 0.49.1 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
package/std/regex.ts CHANGED
@@ -288,9 +288,14 @@ const regexFixedAt = (table: string, at: i32): i32 => {
288
288
  }
289
289
 
290
290
  /** The member a `REGEX_FOLD_DATA` step leads to from `c`: the step is a zigzag-coded difference, doubled, plus one in a pair. */
291
- const regexFoldTarget = (c: i32, step: i32): i32 =>
292
- // Unsigned: a code point and a table's difference are below 2^21, so nothing wraps.
293
- toI32((step & 2) === 2 ? toU32(c) - toU32(step >>> 2) - 1 : toU32(c) + toU32(step >>> 2))
291
+ const regexFoldTarget = (c: i32, step: i32): i32 => {
292
+ // The table's code points are below 2^21 and its steps below 2^23 (2097152
293
+ // and 8388608); a pair outside that is no member, which -1 says.
294
+ if (c < 0 || c > 2097151 || step < 0 || step > 8388607) {
295
+ return -1
296
+ }
297
+ return (step & 2) === 2 ? c - (step >>> 2) - 1 : c + (step >>> 2)
298
+ }
294
299
 
295
300
  /** The code point `REGEX_FOLD_SPANS` holds at `at`. */
296
301
  const regexSpanAt = (at: i32): i32 => regexFixedAt(REGEX_FOLD_SPANS, at)
@@ -368,8 +373,10 @@ const regexFoldWordAt = (text: string, p: i32): boolean => {
368
373
  /** Where the value of `key` starts in `REGEX_PROPERTY_NAMES`, or -1 if it has none. */
369
374
  const regexPropertyEntry = (key: string): i32 => {
370
375
  const i: i32 = toI32(REGEX_PROPERTY_NAMES.indexOf(`|${key}=`))
371
- // Unsigned: the entry found ends inside the table, so the sum cannot wrap.
372
- return i < 0 ? -1 : toI32(toU32(i) + toU32(key.length) + 2)
376
+ const n: i32 = toI32(key.length)
377
+ // The entry found ends inside the table, far below 2^24 (16777216), so the
378
+ // limits only bound the sum.
379
+ return i < 0 || i > 16777216 || n > 16777216 ? -1 : i + n + 2
373
380
  }
374
381
 
375
382
  /** Whether every character of `s` is ASCII. */
@@ -409,14 +416,14 @@ const regexDecodeAt = (text: string, p: i32): i32 => {
409
416
  // Callers read inside the text; outside it there is no byte, which reads as
410
417
  // the invalid character a byte above 0xff would be.
411
418
  if (p < 0 || p >= len) {
412
- return toI32((toU32(REGEX_INVALID) + 0x100) * 8 + 1)
419
+ return (REGEX_INVALID + 0x100) * 8 + 1
413
420
  }
414
421
  const b0: i32 = toI32(text.charCodeAt(p))
415
422
  if (b0 < 0x80) {
416
423
  return b0 * 8 + 1
417
424
  }
418
- // Unsigned: below 0x110100 * 8, far from wrapping.
419
- const invalid: i32 = toI32((toU32(REGEX_INVALID) + toU32(b0 & 0xff)) * 8 + 1)
425
+ // Below 0x110100 * 8, far from 2^31.
426
+ const invalid: i32 = (REGEX_INVALID + (b0 & 0xff)) * 8 + 1
420
427
  if (b0 < 0xc2 || b0 > 0xf4) {
421
428
  return invalid
422
429
  }
@@ -610,17 +617,22 @@ class RegexParser {
610
617
  return -1
611
618
  }
612
619
 
613
- /**
614
- * Moves `pos` on `k` bytes. Unsigned: `pos` stays within a byte or two of
615
- * the pattern, whose length is far below 2^31, and `k` is a few bytes.
616
- */
620
+ /** Moves `pos` on `k` bytes, as `ahead` counts them. */
617
621
  private skip(k: i32): void {
618
622
  this.pos = this.ahead(k)
619
623
  }
620
624
 
621
- /** `pos` plus `k`, unsigned for the same reason as `skip`. */
625
+ /**
626
+ * `pos` plus `k`. `pos` stays within a byte or two of the pattern and `k` is
627
+ * a few bytes; past 2^30 (1073741824) either way the answer stays at
628
+ * `INT_MAX`, past the end of every pattern, so the sum never overflows.
629
+ */
622
630
  private ahead(k: i32): i32 {
623
- return toI32(toU32(this.pos) + toU32(k))
631
+ const p: i32 = this.pos
632
+ if (p < 0 || p > 1073741823 || k < 0 || k > 1073741823) {
633
+ return 2147483647
634
+ }
635
+ return p + k
624
636
  }
625
637
 
626
638
  /** The pattern's byte at `i`, or -1 past either end, which no byte test matches. */
@@ -712,13 +724,16 @@ class RegexParser {
712
724
  return this.node(NODE_CHAR, cp, cp)
713
725
  }
714
726
  const next: i32 = regexFoldTarget(cp, step)
727
+ if (next < 0) {
728
+ return this.node(NODE_CHAR, cp, cp)
729
+ }
715
730
  if ((step & 1) === 1) {
716
731
  return this.node(NODE_CHAR, cp, next)
717
732
  }
718
733
  this.tmpCount = 0
719
734
  this.addRange(cp, cp)
720
735
  let c: i32 = next
721
- while (c !== cp) {
736
+ while (c >= 0 && c !== cp) {
722
737
  this.addRange(c, c)
723
738
  c = regexFoldTarget(c, this.foldStepOf(c))
724
739
  }
@@ -729,22 +744,21 @@ class RegexParser {
729
744
  private tableNumber(table: string): i32 {
730
745
  const n: i32 = toI32(table.length)
731
746
  let p: i32 = this.tablePos
732
- // Unsigned: the generated numbers are below 2^21, so nothing wraps.
733
- let v: u32 = 0
734
- // A digit of 32 or more says another follows. The generator ends every
735
- // number inside its table, so the bound never stops the loop and the
736
- // partial number after it is never read.
737
- while (p >= 0 && p < n) {
747
+ let v: i32 = 0
748
+ // A digit of 32 or more says another follows. The generated numbers are
749
+ // below 2^21 (2097152) and the generator ends every number inside its
750
+ // table, so neither bound stops the loop early and nothing overflows.
751
+ while (p >= 0 && p < n && v >= 0 && v < 2097152) {
738
752
  const d: i32 = toI32(table.charCodeAt(p)) - 48
739
753
  p += 1
740
754
  if (d < 32) {
741
755
  this.tablePos = p
742
- return toI32(v * 32 + toU32(d))
756
+ return v * 32 + d
743
757
  }
744
- v = v * 32 + toU32(d) - 32
758
+ v = v * 32 + d - 32
745
759
  }
746
760
  this.tablePos = p
747
- return toI32(v)
761
+ return v
748
762
  }
749
763
 
750
764
  /**
@@ -773,11 +787,15 @@ class RegexParser {
773
787
  private foldStepOf(cp: i32): i32 {
774
788
  let c: i32 = this.foldSeek(cp)
775
789
  const end: i32 = toI32(REGEX_FOLD_DATA.length)
776
- while (c < cp && this.tablePos < end) {
790
+ // Code points and the gaps between them are below 2^21 (2097152).
791
+ while (c >= 0 && c < cp && c < 2097152 && this.tablePos < end) {
777
792
  this.tableNumber(REGEX_FOLD_DATA)
778
793
  if (this.tablePos < end) {
779
- // Unsigned: code points and their gaps are below 2^21.
780
- c = toI32(toU32(c) + toU32(this.tableNumber(REGEX_FOLD_DATA)))
794
+ const gap: i32 = this.tableNumber(REGEX_FOLD_DATA)
795
+ if (gap < 0 || gap > 2097151) {
796
+ return -1
797
+ }
798
+ c = c + gap
781
799
  }
782
800
  }
783
801
  return c === cp && this.tablePos < end ? this.tableNumber(REGEX_FOLD_DATA) : -1
@@ -785,9 +803,6 @@ class RegexParser {
785
803
 
786
804
  /** `alternative ('|' alternative)*`. @internal `regexCompile` starts the parse with it. */
787
805
  parseAlt(depth: i32): i32 {
788
- if (depth > REGEX_MAX_DEPTH) {
789
- return this.fail("groups nest too deeply", this.pos)
790
- }
791
806
  const first: i32 = this.parseSeq(depth)
792
807
  if (this.failed) {
793
808
  return -1
@@ -1047,19 +1062,21 @@ class RegexParser {
1047
1062
  }
1048
1063
  let index: i32 = 0
1049
1064
  if (capture) {
1050
- if (this.groups > REGEX_MAX_GROUPS) {
1065
+ index = this.groups
1066
+ if (index >= REGEX_MAX_GROUPS + 1) {
1051
1067
  return this.fail("too many groups", open)
1052
1068
  }
1053
- index = this.groups
1054
- // Unsigned: the test above stops the count past the limit.
1055
- this.groups = toI32(toU32(index) + 1)
1069
+ this.groups = index + 1
1056
1070
  this.names.push(name)
1057
1071
  if (name !== "") {
1058
1072
  this.hasNames = true
1059
1073
  }
1060
1074
  }
1061
- // Unsigned: `parseAlt` fails past `REGEX_MAX_DEPTH`, so nothing wraps.
1062
- const inner: i32 = this.parseAlt(toI32(toU32(depth) + 1))
1075
+ // The one place the nesting deepens, so the limit is tested here.
1076
+ if (depth >= REGEX_MAX_DEPTH) {
1077
+ return this.fail("groups nest too deeply", this.pos)
1078
+ }
1079
+ const inner: i32 = this.parseAlt(depth + 1)
1063
1080
  if (this.failed) {
1064
1081
  return -1
1065
1082
  }
@@ -1130,8 +1147,9 @@ class RegexParser {
1130
1147
  * backspace and `\-` a hyphen.
1131
1148
  */
1132
1149
  private parseCharEscape(inClass: boolean): i32 {
1133
- // The cursor is past the backslash, so this is at least 0.
1134
- const at: i32 = toI32(toU32(this.pos) - 1)
1150
+ // The cursor is past the backslash, so `here` is at least 1.
1151
+ const here: i32 = this.pos
1152
+ const at: i32 = here > 0 ? here - 1 : 0
1135
1153
  const c: i32 = this.byteAt(this.pos)
1136
1154
  this.skip(toI32(1))
1137
1155
  if (c === 110) {
@@ -1252,9 +1270,11 @@ class RegexParser {
1252
1270
  if (v < 0) {
1253
1271
  return this.fail("invalid \\u escape", at)
1254
1272
  }
1273
+ // A high surrogate, 0xd800 to 0xdbff, then a low one, 0xdc00 to 0xdfff,
1274
+ // tested in decimal so the bounds prover reads the pair's range.
1255
1275
  if (
1256
- v >= 0xd800 &&
1257
- v <= 0xdbff &&
1276
+ v >= 55296 &&
1277
+ v <= 56319 &&
1258
1278
  this.ahead(toI32(6)) <= this.len &&
1259
1279
  this.byteAt(this.pos) === 92 &&
1260
1280
  this.byteAt(this.ahead(toI32(1))) === 117
@@ -1262,8 +1282,8 @@ class RegexParser {
1262
1282
  const save: i32 = this.pos
1263
1283
  this.skip(toI32(2))
1264
1284
  const lo: i32 = this.parseHex(REGEX_HEX_UNIT)
1265
- if (lo >= 0xdc00 && lo <= 0xdfff) {
1266
- return toI32(((toU32(v) - 0xd800) << 10) + (toU32(lo) - 0xdc00) + 0x10000)
1285
+ if (lo >= 56320 && lo <= 57343) {
1286
+ return (v - 55296) * 1024 + (lo - 56320) + 65536
1267
1287
  }
1268
1288
  this.pos = save
1269
1289
  }
@@ -1278,13 +1298,13 @@ class RegexParser {
1278
1298
  if (n >= 0 && n < toI32(los.length) && n < toI32(his.length)) {
1279
1299
  los[n] = lo
1280
1300
  his[n] = hi
1301
+ this.tmpCount = n + 1
1281
1302
  } else {
1303
+ // Past the lists `n` is their length, so the push makes the count theirs.
1282
1304
  los.push(lo)
1283
1305
  his.push(hi)
1306
+ this.tmpCount = toI32(los.length)
1284
1307
  }
1285
- // Unsigned: each range has its slot in the lists, which hold far fewer
1286
- // than 2^31.
1287
- this.tmpCount = toI32(toU32(n) + 1)
1288
1308
  }
1289
1309
 
1290
1310
  /**
@@ -1294,8 +1314,8 @@ class RegexParser {
1294
1314
  */
1295
1315
  private addSet(letter: i32): void {
1296
1316
  const negate: boolean = letter === 68 || letter === 87 || letter === 83
1297
- // Unsigned: a capital letter's code is far from wrapping.
1298
- const lower: i32 = negate ? toI32(toU32(letter) + 32) : letter
1317
+ // ASCII's lower case sets bit 5, which `d`, `w` and `s` already have.
1318
+ const lower: i32 = letter | 32
1299
1319
  let set: i32 = SET_SPACE
1300
1320
  if (lower === 100) {
1301
1321
  set = SET_DIGIT
@@ -1346,8 +1366,8 @@ class RegexParser {
1346
1366
  r.push(0xfeff)
1347
1367
  r.push(0xfeff)
1348
1368
  }
1349
- // The pairs below are code points, so the unsigned steps between them
1350
- // never wrap.
1369
+ // The pairs above are code points, below 0x110000 (1114112), which keeps
1370
+ // the steps between them in range.
1351
1371
  let from: i32 = 0
1352
1372
  let i: i32 = 0
1353
1373
  while (i >= 0 && i < toI32(r.length)) {
@@ -1357,12 +1377,15 @@ class RegexParser {
1357
1377
  break
1358
1378
  }
1359
1379
  const hi: i32 = r[at]
1380
+ if (lo < 0 || hi < 0 || hi > 1114111) {
1381
+ break
1382
+ }
1360
1383
  if (!negate) {
1361
1384
  this.addRange(lo, hi)
1362
1385
  } else if (lo > from) {
1363
- this.addRange(from, toI32(toU32(lo) - 1))
1386
+ this.addRange(from, lo - 1)
1364
1387
  }
1365
- from = toI32(toU32(hi) + 1)
1388
+ from = hi + 1
1366
1389
  i = at + 1
1367
1390
  }
1368
1391
  if (!negate) {
@@ -1416,7 +1439,7 @@ class RegexParser {
1416
1439
  }
1417
1440
  } else {
1418
1441
  const name: string = regexSpan(this.src, start, eq)
1419
- const value: string = regexSpan(this.src, toI32(toU32(eq) + 1), i)
1442
+ const value: string = regexSpan(this.src, eq < i ? eq + 1 : i, i)
1420
1443
  const script: boolean = name === "Script" || name === "sc"
1421
1444
  const extensions: boolean = name === "Script_Extensions" || name === "scx"
1422
1445
  if (name === "General_Category" || name === "gc") {
@@ -1433,7 +1456,12 @@ class RegexParser {
1433
1456
  }
1434
1457
  // A script's entry is its Script list, a comma, then its Script_Extensions list.
1435
1458
  if (extensions) {
1436
- entry = toI32(toU32(REGEX_PROPERTY_NAMES.indexOf(",", entry)) + 1)
1459
+ const comma: i32 = toI32(REGEX_PROPERTY_NAMES.indexOf(",", entry))
1460
+ if (comma < 0 || comma > 1073741823) {
1461
+ this.fail("invalid property value", at)
1462
+ return
1463
+ }
1464
+ entry = comma + 1
1437
1465
  }
1438
1466
  }
1439
1467
  // An entry is the offset of its list in `REGEX_PROPERTY_DATA`, after a `~` for its complement.
@@ -1473,14 +1501,20 @@ class RegexParser {
1473
1501
  if (gaps && from <= toU32(REGEX_MAX_CP)) {
1474
1502
  this.addRange(toI32(from), REGEX_MAX_CP)
1475
1503
  }
1476
- this.countPropertyRanges(toI32(toU32(this.tmpCount) - toU32(before)), at)
1504
+ const now: i32 = this.tmpCount
1505
+ const added: i32 = now >= before && before >= 0 ? now - before : 0
1506
+ this.countPropertyRanges(added, at)
1477
1507
  }
1478
1508
 
1479
1509
  /** Count `n` more ranges against `REGEX_MAX_PROPERTY_RANGES`, failing at `at` past it. */
1480
1510
  private countPropertyRanges(n: i32, at: i32): void {
1481
- // Unsigned: the count fails the parse once past its limit, and `n` is
1482
- // the ranges one escape just added.
1483
- this.propertyRanges = toI32(toU32(this.propertyRanges) + toU32(n))
1511
+ const total: i32 = this.propertyRanges
1512
+ // Both terms are checked against the limit first, so the sum cannot overflow.
1513
+ if (n > 100000 || total < 0 || total > 100000) {
1514
+ this.fail("property escapes and case folding add more than 100000 ranges", at)
1515
+ return
1516
+ }
1517
+ this.propertyRanges = total + n
1484
1518
  if (this.propertyRanges > REGEX_MAX_PROPERTY_RANGES) {
1485
1519
  this.fail("property escapes and case folding add more than 100000 ranges", at)
1486
1520
  }
@@ -1659,8 +1693,7 @@ class RegexParser {
1659
1693
  // The tests on the lists' lengths below always hold: `from` and `end`
1660
1694
  // bound the run just pushed, and the run is the lists' tail.
1661
1695
  if (end - from <= REGEX_INSERTION_SORT) {
1662
- // Unsigned: `from` is a list's length, far below 2^31.
1663
- i = toI32(toU32(from) + 1)
1696
+ i = from < end ? from + 1 : end
1664
1697
  while (i >= 1 && i < end && i < toI32(lows.length) && i < toI32(highs.length)) {
1665
1698
  const lo: i32 = lows[i]
1666
1699
  const hi: i32 = highs[i]
@@ -1674,7 +1707,7 @@ class RegexParser {
1674
1707
  }
1675
1708
  q -= 1
1676
1709
  }
1677
- const m: i32 = toI32(toU32(q) + 1)
1710
+ const m: i32 = q + 1
1678
1711
  if (m >= 0 && m < toI32(lows.length) && m < toI32(highs.length)) {
1679
1712
  lows[m] = lo
1680
1713
  highs[m] = hi
@@ -1684,24 +1717,24 @@ class RegexParser {
1684
1717
  } else {
1685
1718
  this.mergeSort(from, end)
1686
1719
  }
1687
- // Merge in place; what merging leaves past `out` is dead. Unsigned
1688
- // steps: `out` and the ranges' code points are far below 2^31.
1720
+ // Merge in place; what merging leaves past `out` is dead.
1689
1721
  let out: i32 = from
1690
1722
  i = from
1691
1723
  while (i >= 0 && i < end && i < toI32(lows.length) && i < toI32(highs.length)) {
1692
1724
  const lo: i32 = lows[i]
1693
1725
  const hi: i32 = highs[i]
1694
- const last: i32 = toI32(toU32(out) - 1)
1695
- if (out > from && last >= 0 && last < toI32(highs.length) && lo <= toI32(toU32(highs[last]) + 1)) {
1696
- if (hi > highs[last]) {
1726
+ const last: i32 = out - 1
1727
+ const top: i32 = last >= 0 && last < toI32(highs.length) ? highs[last] : -1
1728
+ // A code point at the top of the range adjoins anything after it.
1729
+ if (out > from && last >= 0 && last < toI32(highs.length) && (top >= 1114111 || lo <= top + 1)) {
1730
+ if (hi > top) {
1697
1731
  highs[last] = hi
1698
1732
  }
1699
- } else {
1700
- if (out >= 0 && out < toI32(lows.length) && out < toI32(highs.length)) {
1701
- lows[out] = lo
1702
- highs[out] = hi
1703
- }
1704
- out = toI32(toU32(out) + 1)
1733
+ } else if (out >= 0 && out < toI32(lows.length) && out < toI32(highs.length)) {
1734
+ // `out` never passes `i`, so this always holds.
1735
+ lows[out] = lo
1736
+ highs[out] = hi
1737
+ out += 1
1705
1738
  }
1706
1739
  i += 1
1707
1740
  }
@@ -1791,13 +1824,20 @@ class RegexParser {
1791
1824
  width *= 2
1792
1825
  }
1793
1826
  i = 0
1794
- while (i < n && i < toI32(srcLo.length) && i < toI32(srcHi.length)) {
1795
- const at: i32 = toI32(toU32(from) + toU32(i))
1796
- if (at >= 0 && at < toI32(lows.length) && at < toI32(highs.length)) {
1797
- lows[at] = srcLo[i]
1798
- highs[at] = srcHi[i]
1799
- }
1827
+ let at: i32 = from
1828
+ while (
1829
+ i >= 0 &&
1830
+ i < n &&
1831
+ i < toI32(srcLo.length) &&
1832
+ i < toI32(srcHi.length) &&
1833
+ at >= 0 &&
1834
+ at < toI32(lows.length) &&
1835
+ at < toI32(highs.length)
1836
+ ) {
1837
+ lows[at] = srcLo[i]
1838
+ highs[at] = srcHi[i]
1800
1839
  i += 1
1840
+ at += 1
1801
1841
  }
1802
1842
  }
1803
1843
 
@@ -1822,10 +1862,11 @@ class RegexParser {
1822
1862
  if (!touches && regexFoldsIn(lo, highs[r0])) {
1823
1863
  touches = true
1824
1864
  }
1825
- if (covers && gap < lo && regexFoldsIn(gap, toI32(toU32(lo) - 1))) {
1865
+ if (covers && gap < lo && lo > 0 && regexFoldsIn(gap, lo - 1)) {
1826
1866
  covers = false
1827
1867
  }
1828
- gap = toI32(toU32(highs[r0]) + 1)
1868
+ const hi: i32 = highs[r0]
1869
+ gap = hi < REGEX_MAX_CP ? hi + 1 : REGEX_MAX_CP + 1
1829
1870
  r0 += 1
1830
1871
  }
1831
1872
  if (covers && gap <= REGEX_MAX_CP && regexFoldsIn(gap, REGEX_MAX_CP)) {
@@ -1846,7 +1887,7 @@ class RegexParser {
1846
1887
  const step: i32 = this.tableNumber(REGEX_FOLD_DATA)
1847
1888
  const at: i32 = this.tablePos
1848
1889
  let c: i32 = regexFoldTarget(f, step)
1849
- while (f >= lo && c !== f) {
1890
+ while (f >= lo && c >= 0 && c !== f) {
1850
1891
  if (!this.rangesHave(from, out, c)) {
1851
1892
  if (added === 0) {
1852
1893
  this.tmpCount = 0
@@ -1857,10 +1898,9 @@ class RegexParser {
1857
1898
  c = (step & 1) === 1 ? f : regexFoldTarget(c, this.foldStepOf(c))
1858
1899
  }
1859
1900
  this.tablePos = at
1860
- f =
1861
- at < end
1862
- ? toI32(toU32(f) + toU32(this.tableNumber(REGEX_FOLD_DATA)))
1863
- : toI32(toU32(REGEX_MAX_CP) + 1)
1901
+ const gapTo: i32 = at < end ? this.tableNumber(REGEX_FOLD_DATA) : -1
1902
+ // Table code points and gaps are below 2^21; anything else ends the walk.
1903
+ f = f >= 0 && f < 2097152 && gapTo >= 0 && gapTo < 2097152 ? f + gapTo : REGEX_MAX_CP + 1
1864
1904
  }
1865
1905
  r += 1
1866
1906
  }
@@ -1882,9 +1922,9 @@ class RegexParser {
1882
1922
  const highs: i32[] = this.clsHi
1883
1923
  let lo: i32 = from
1884
1924
  let hi: i32 = out
1885
- while (lo < hi) {
1886
- // Unsigned: both ends are list indexes, so their sum does not wrap.
1887
- const mid: i32 = toI32((toU32(lo) + toU32(hi)) >>> 1)
1925
+ // List indexes stay below 2^30, so the sum of two does not overflow.
1926
+ while (lo >= 0 && lo < 1073741824 && lo < hi && hi < 1073741824) {
1927
+ const mid: i32 = (lo + hi) >>> 1
1888
1928
  // `[from, out)` is inside the lists, so this always holds.
1889
1929
  if (mid < 0 || mid >= toI32(highs.length)) {
1890
1930
  return false
@@ -1932,7 +1972,7 @@ const regexNullable = (p: RegexParser, n: i32): boolean => {
1932
1972
  // the reverse program. A transition is one i32, 0 until it is computed. A
1933
1973
  // plain one is the next state's offset itself, which is never 0 because the
1934
1974
  // first row is never a state, so the hot loop's chain from one byte to the
1935
- // next is a load and an `|`. Any other is `-(next << 4 | flags)`, with
1975
+ // next is a load and an `|`. Any other is `~(next << 4 | flags)`, with
1936
1976
  // `DFA_MATCH` when a match ends before the character, `DFA_DEAD` when no
1937
1977
  // thread survives it and `DFA_IDLE` when the next state waits for a match to
1938
1978
  // start and a prefilter can find one. The hot loop follows a plain entry and
@@ -1951,8 +1991,7 @@ const DFA_INNER: i32 = 3
1951
1991
  const DFA_NONE: i32 = -1
1952
1992
  /** A search that built too many states for the bytes it read, and leaves the rest to the VM. */
1953
1993
  const DFA_GAVE_UP: i32 = -2
1954
- /** At most this many units are refined into byte classes, and this much work spent doing it. */
1955
- const DFA_MAX_UNITS: i32 = 1048576
1994
+ /** At most this much work is spent refining units into byte classes, of which `dfaInit` takes at most 1048576. */
1956
1995
  const DFA_MAX_WORK: i32 = 4194304
1957
1996
  /** At most 2^12 classes, so a state's row is at most 4096 transitions. */
1958
1997
  const DFA_MAX_SHIFT: i32 = 12
@@ -1965,7 +2004,6 @@ const DFA_HASH_MASK: i32 = 1048575
1965
2004
  /** A cache cleared more than this often must read 10 bytes per state it can hold, or the search gives up. */
1966
2005
  const DFA_FREE_FLUSHES: i32 = 3
1967
2006
  const DFA_BYTES_PER_STATE: i32 = 10
1968
- const DFA_EPOCH_MASK: i32 = 1073741823
1969
2007
  /** How far the inner search may read in vain before it gives way to the plain one. */
1970
2008
  const DFA_INNER_SLACK: f64 = 65536.0
1971
2009
 
@@ -2010,17 +2048,13 @@ const regexBytesText = (bytes: u8[]): string => {
2010
2048
  }
2011
2049
 
2012
2050
  /**
2013
- * The first slot of group `g`'s pair. Unsigned: the parser stops the
2014
- * groups at `REGEX_MAX_GROUPS`.
2051
+ * The first slot of group `g`'s pair, or 0 for a `g` no parse yields: the
2052
+ * parser stops the groups at `REGEX_MAX_GROUPS`.
2015
2053
  */
2016
- const regexSlot = (g: i32): i32 => toI32(2 * toU32(g))
2054
+ const regexSlot = (g: i32): i32 => (g >= 0 && g < 1073741823 ? 2 * g : 0)
2017
2055
 
2018
- /**
2019
- * Minus `e`, a negative transition entry. `~e` is `-e - 1`, which is not
2020
- * negative, so adding 1 in u32 cannot wrap, and it gives the same value
2021
- * under JavaScript, where a u32 sum does not wrap.
2022
- */
2023
- const regexNegated = (e: i32): i32 => toI32(toU32(~e) + 1)
2056
+ /** The second slot of group `g`'s pair, or 1 where `regexSlot` gives 0. */
2057
+ const regexSlotEnd = (g: i32): i32 => (g >= 0 && g < 1073741823 ? 2 * g + 1 : 1)
2024
2058
 
2025
2059
  /** `a[i]`, or `missing` outside `a`. */
2026
2060
  const regexItem = (a: i32[], i: i32, missing: i32): i32 => {
@@ -2071,31 +2105,58 @@ const regexPrefixAt = (text: string, s: i32, prefix: string): boolean => {
2071
2105
  return true
2072
2106
  }
2073
2107
 
2074
- /** Store `ns` slots of `src` into `dst` from `base`, or as many as fit. */
2075
- const regexStoreSlots = (dst: i32[], base: i32, src: i32[], ns: i32): void => {
2108
+ /**
2109
+ * Store `ns` slots of `src` into `dst` as row `pc`, or as many as fit. The
2110
+ * row's start is multiplied in `i64`, where two `i32` factors cannot
2111
+ * overflow; a row past `dst` stores nothing.
2112
+ */
2113
+ const regexStoreSlots = (dst: i32[], pc: i32, src: i32[], ns: i32): void => {
2114
+ const base: i64 = toI64(pc) * toI64(ns)
2115
+ if (pc < 0 || ns < 0 || base >= toI64(dst.length)) {
2116
+ return
2117
+ }
2076
2118
  let k: i32 = 0
2077
- while (k < ns && k < toI32(src.length)) {
2078
- // Unsigned: `base` and `k` are not negative, and `dst` bounds the sum.
2079
- const at: i32 = toI32(toU32(base) + toU32(k))
2080
- if (at < 0 || at >= toI32(dst.length)) {
2081
- return
2082
- }
2119
+ let at: i32 = toI32(base)
2120
+ while (k >= 0 && k < ns && k < toI32(src.length) && at >= 0 && at < toI32(dst.length)) {
2083
2121
  dst[at] = src[k]
2084
2122
  k += 1
2123
+ at += 1
2085
2124
  }
2086
2125
  }
2087
2126
 
2088
- /** Load `ns` slots of `src` from `base` into `dst`, or as many as there are. */
2089
- const regexLoadSlots = (dst: i32[], src: i32[], base: i32, ns: i32): void => {
2127
+ /**
2128
+ * Load `ns` slots of `src` from row `pc` into `dst`, or as many as there
2129
+ * are. The row's start is multiplied in `i64`, as in `regexStoreSlots`; a
2130
+ * row past `src` loads nothing.
2131
+ */
2132
+ const regexLoadSlots = (dst: i32[], src: i32[], pc: i32, ns: i32): void => {
2133
+ const base: i64 = toI64(pc) * toI64(ns)
2134
+ if (pc < 0 || ns < 0 || base >= toI64(src.length)) {
2135
+ return
2136
+ }
2090
2137
  let k: i32 = 0
2091
- while (k < ns && k < toI32(dst.length)) {
2092
- const at: i32 = toI32(toU32(base) + toU32(k))
2093
- if (at < 0 || at >= toI32(src.length)) {
2094
- return
2095
- }
2138
+ let at: i32 = toI32(base)
2139
+ while (k >= 0 && k < ns && k < toI32(dst.length) && at >= 0 && at < toI32(src.length)) {
2096
2140
  dst[k] = src[at]
2097
2141
  k += 1
2142
+ at += 1
2143
+ }
2144
+ }
2145
+
2146
+ /**
2147
+ * `a * b`, the size of a table a compile allocates, or -1 when a factor is
2148
+ * negative or the product passes `REGEX_MAX_SLOT_TABLE`. Multiplied in
2149
+ * `i64`, where two `i32` factors cannot overflow.
2150
+ */
2151
+ const regexTableSize = (a: i32, b: i32): i32 => {
2152
+ if (a < 0 || b < 0) {
2153
+ return -1
2098
2154
  }
2155
+ const wide: i64 = toI64(a) * toI64(b)
2156
+ if (wide > toI64(REGEX_MAX_SLOT_TABLE)) {
2157
+ return -1
2158
+ }
2159
+ return toI32(wide)
2099
2160
  }
2100
2161
 
2101
2162
  /**
@@ -2186,15 +2247,15 @@ const regexSortPoints = (a: i32[]): void => {
2186
2247
  // Each index is also tested against the length it reads, `n` for `a`:
2187
2248
  // the runs end inside both arrays, and that test is what proves it.
2188
2249
  while (k >= 0 && k < hi && k < toI32(tmp.length)) {
2189
- const right: boolean = j >= 0 && j < hi && j < n && (i >= mid || i < 0 || i >= n || a[j] < a[i])
2190
- const from: i32 = right ? j : i
2191
- if (from >= 0 && from < n) {
2192
- tmp[k] = a[from]
2193
- }
2194
- if (right) {
2195
- j = toI32(toU32(j) + 1)
2250
+ if (j >= 0 && j < hi && j < n && (i >= mid || i < 0 || i >= n || a[j] < a[i])) {
2251
+ tmp[k] = a[j]
2252
+ j += 1
2253
+ } else if (i >= 0 && i < n) {
2254
+ tmp[k] = a[i]
2255
+ i += 1
2196
2256
  } else {
2197
- i = toI32(toU32(i) + 1)
2257
+ // Never taken: the left run is not done while the right one is.
2258
+ break
2198
2259
  }
2199
2260
  k += 1
2200
2261
  }
@@ -2248,14 +2309,16 @@ const REGEX_LETTER_PER_MILLE: string = "9570626055555048463331222219181716151411
2248
2309
  /** How many of every thousand bytes of English text the `i`th letter of `REGEX_LETTER_ORDER` is. */
2249
2310
  const regexLetterPerMille = (i: i32): i32 => {
2250
2311
  const table: string = REGEX_LETTER_PER_MILLE
2251
- // Unsigned: an `i` of 2^30 or more wraps negative, which the test turns
2252
- // away. The table has a pair for every letter, so it never does.
2253
- const at: i32 = toI32(toU32(i) * 2)
2254
- if (i < 0 || at < 0 || at >= toI32(table.length) - 1) {
2312
+ // The table has a pair for every letter, so neither test turns one away.
2313
+ if (i < 0 || i > 1073741823) {
2314
+ return 0
2315
+ }
2316
+ const at: i32 = i * 2
2317
+ if (at >= toI32(table.length) - 1) {
2255
2318
  return 0
2256
2319
  }
2257
2320
  // Two decimal digits, so the unsigned sum stays below 100.
2258
- return toI32((toU32(table.charCodeAt(at)) - 48) * 10 + toU32(table.charCodeAt(at + 1)) - 48)
2321
+ return (toI32(table.charCodeAt(at)) - 48) * 10 + toI32(table.charCodeAt(at + 1)) - 48
2259
2322
  }
2260
2323
 
2261
2324
  /** How many of every thousand bytes of text are ASCII byte `b`, roughly: what a scan window is chosen by. */
@@ -2309,20 +2372,28 @@ const REGEX_SCAN_WIDTH: i32 = 3
2309
2372
  */
2310
2373
  const regexScanFrom = (text: string, masks: u8[], depth: i32, off: i32, width: i32, from: i32): i32 => {
2311
2374
  const length: i32 = toI32(text.length)
2375
+ // Outside the ranges every caller keeps to, nothing is scanned.
2376
+ if (depth < 1 || depth > 8 || off < 0 || off > 8 || width < 0 || width > 8) {
2377
+ return -1
2378
+ }
2312
2379
  let i: i32 = from
2313
2380
  while (i >= 0 && i < length && depth <= length - i) {
2314
2381
  let k: i32 = 0
2315
2382
  while (k < depth) {
2316
2383
  // The window's positions, then the ones before it, then the ones after.
2317
- // Unsigned, as every operand is below `REGEX_SCAN_DEPTH` and none wraps.
2318
- let j: u32 = toU32(k)
2384
+ let j: i32 = k
2319
2385
  if (k < width) {
2320
- j = toU32(off) + toU32(k)
2321
- } else if (toU32(k) < toU32(off) + toU32(width)) {
2322
- j = toU32(k) - toU32(width)
2386
+ j = off + k
2387
+ } else if (k < off + width) {
2388
+ j = k - width
2389
+ }
2390
+ // `j` is below `depth`, so `at` is inside the text; summed in `i64`
2391
+ // and tested, since the prover cannot tie `j` to `length - i`.
2392
+ const wide: i64 = toI64(i) + toI64(j)
2393
+ if (wide >= toI64(length)) {
2394
+ break
2323
2395
  }
2324
- // `j` is below `depth`, so `at` is inside the text; the test proves it.
2325
- const at: i32 = toI32(toU32(i) + j)
2396
+ const at: i32 = toI32(wide)
2326
2397
  if (at < 0 || at >= length) {
2327
2398
  break
2328
2399
  }
@@ -2330,7 +2401,11 @@ const regexScanFrom = (text: string, masks: u8[], depth: i32, off: i32, width: i
2330
2401
  if (b > 255) {
2331
2402
  b = 255
2332
2403
  }
2333
- const slot: i32 = toI32(toU32(depth) * 32 + (j << 8) + toU32(b))
2404
+ // Never taken: `j` is below 16 and a code unit is not negative.
2405
+ if (j < 0 || j > 15 || b < 0) {
2406
+ break
2407
+ }
2408
+ const slot: i32 = depth * 32 + j * 256 + b
2334
2409
  // Outside the table is never reached; the test is what proves `slot`.
2335
2410
  if (slot < 0 || slot >= toI32(masks.length) || masks[slot] === 0) {
2336
2411
  break
@@ -2409,14 +2484,15 @@ export class RegexMatch {
2409
2484
 
2410
2485
  /**
2411
2486
  * Group `k`'s start slot, or its end slot when `end` is set; -1 for a
2412
- * number with no group. The slot index `2 * k` is computed unsigned: a `k`
2413
- * of 2^30 or more wraps it negative, which the range test turns away as it
2414
- * does a negative `k`.
2487
+ * number with no group.
2415
2488
  */
2416
2489
  private slotOf(k: i32, end: boolean): i32 {
2417
2490
  const s: i32[] = this.slots
2418
- const at: i32 = toI32(toU32(k) * 2)
2419
- if (k < 0 || at < 0 || at >= toI32(s.length) - 1) {
2491
+ if (k < 0 || k > 1073741823) {
2492
+ return -1
2493
+ }
2494
+ const at: i32 = k * 2
2495
+ if (at >= toI32(s.length) - 1) {
2420
2496
  return -1
2421
2497
  }
2422
2498
  return end ? s[at + 1] : s[at]
@@ -2472,7 +2548,9 @@ export class RegexMatch {
2472
2548
 
2473
2549
  /**
2474
2550
  * A compiled pattern: its program, its classes, what the prefilters read and
2475
- * the VM's scratch memory. Made by `regexCompile`.
2551
+ * the VM's scratch memory. Made by `regexCompile`. A subject longer than
2552
+ * 2,146,435,071 bytes never matches: `test` and `exec` give false,
2553
+ * and `count` gives 0.
2476
2554
  */
2477
2555
  export class Regex {
2478
2556
  // Widest first, so the alignment rules insert no padding.
@@ -2787,11 +2865,14 @@ export class Regex {
2787
2865
 
2788
2866
  /**
2789
2867
  * Point split `s` on to the next instruction and to `other`, the next
2790
- * first when `first`. Unsigned: `s` is an instruction, and `emit` stops
2791
- * the program at `REGEX_MAX_INSTRUCTIONS`.
2868
+ * first when `first`. `s` is an instruction, so the test never returns.
2792
2869
  */
2793
2870
  private branch(s: i32, first: boolean, other: i32): void {
2794
- const next: i32 = toI32(toU32(s) + 1)
2871
+ const count: i32 = this.pc()
2872
+ if (s < 0 || s >= count) {
2873
+ return
2874
+ }
2875
+ const next: i32 = s + 1
2795
2876
  regexPut(this.ia, s, first ? next : other)
2796
2877
  regexPut(this.ib, s, first ? other : next)
2797
2878
  }
@@ -2861,7 +2942,7 @@ export class Regex {
2861
2942
  if (kind === NODE_GROUP) {
2862
2943
  this.emit(OP_SAVE, regexSlot(p.aOf(n)), REGEX_NIL)
2863
2944
  this.compileNode(p, p.kidOf(n), rev)
2864
- this.emit(OP_SAVE, toI32(toU32(regexSlot(p.aOf(n))) + 1), REGEX_NIL)
2945
+ this.emit(OP_SAVE, regexSlotEnd(p.aOf(n)), REGEX_NIL)
2865
2946
  return
2866
2947
  }
2867
2948
  if (kind === NODE_ALT) {
@@ -2969,9 +3050,11 @@ export class Regex {
2969
3050
  return
2970
3051
  }
2971
3052
  if (marks[n] < 0) {
2972
- marks[n] = this.width
2973
- // Unsigned: a slot per group end and per repeat node, far below 2^31.
2974
- this.width = toI32(toU32(this.width) + 1)
3053
+ const w: i32 = this.width
3054
+ marks[n] = w
3055
+ // A slot per group end and per repeat node. Past the slot table's
3056
+ // limit, `build` refuses the pattern whatever the count.
3057
+ this.width = w < REGEX_MAX_SLOT_TABLE ? w + 1 : w
2975
3058
  }
2976
3059
  const k: i32 = marks[n]
2977
3060
  let i: i32 = 0
@@ -3011,13 +3094,17 @@ export class Regex {
3011
3094
  */
3012
3095
  private compileMarked(p: RegexParser, n: i32, k: i32, rev: boolean): void {
3013
3096
  const level: i32 = this.checkDepth
3014
- // Unsigned: the depth is at most the pattern's nesting, which the parser
3015
- // bounds, and it goes back down only after it went up.
3097
+ // The depth is at most the pattern's nesting, which the parser bounds,
3098
+ // and it goes back down only after it went up: the test never holds.
3099
+ if (level < 0 || level > 1073741823) {
3100
+ this.tooBig = true
3101
+ return
3102
+ }
3016
3103
  if (level < REGEX_MAX_LEVELS && level >= this.levels) {
3017
- this.levels = toI32(toU32(level) + 1)
3104
+ this.levels = level + 1
3018
3105
  }
3019
3106
  this.emit(OP_MARK, k, level)
3020
- this.checkDepth = toI32(toU32(level) + 1)
3107
+ this.checkDepth = level + 1
3021
3108
  this.compileIteration(p, n, rev)
3022
3109
  this.checkDepth = level
3023
3110
  this.emit(OP_CHECK, k, REGEX_NIL)
@@ -3038,7 +3125,7 @@ export class Regex {
3038
3125
  const whole: i32 = regexSlot(REGEX_WHOLE)
3039
3126
  this.emit(OP_SAVE, whole, REGEX_NIL)
3040
3127
  this.compileNode(p, root, false)
3041
- this.emit(OP_SAVE, toI32(toU32(whole) + 1), REGEX_NIL)
3128
+ this.emit(OP_SAVE, regexSlotEnd(REGEX_WHOLE), REGEX_NIL)
3042
3129
  this.emit(OP_MATCH, REGEX_NIL, REGEX_NIL)
3043
3130
  if (this.tooBig) {
3044
3131
  return "the pattern compiles to more than 100000 instructions"
@@ -3058,20 +3145,22 @@ export class Regex {
3058
3145
  this.tooBig = false
3059
3146
  }
3060
3147
  const slots: i32 = this.width
3061
- // In f64, where neither product can wrap: for whole numbers,
3062
- // `count * slots > limit` holds exactly when `count > limit / slots`.
3063
- if (toF64(count) * toF64(slots) > toF64(REGEX_MAX_SLOT_TABLE)) {
3148
+ const table: i32 = regexTableSize(count, slots)
3149
+ if (table < 0) {
3064
3150
  return "too many groups for the pattern's size"
3065
3151
  }
3066
- // Unsigned: the test above bounds `count * slots`, and `states` is at
3067
- // most `1 << REGEX_MAX_LEVELS` beside a count `emit` bounds.
3068
- const table: i32 = toI32(toU32(count) * toU32(slots))
3069
3152
  this.cDense = regexZeros(count)
3070
3153
  this.cSlots = regexZeros(table)
3071
3154
  this.nDense = regexZeros(count)
3072
3155
  this.nSlots = regexZeros(table)
3073
3156
  const states: i32 = 1 << this.levels
3074
- this.visit = regexZeros(toI32(toU32(count) * toU32(states)))
3157
+ // At most `1 << REGEX_MAX_LEVELS` states beside a count `emit` bounds, so
3158
+ // this stays inside the limit.
3159
+ const visits: i32 = regexTableSize(count, states)
3160
+ if (visits < 0) {
3161
+ return "too many groups for the pattern's size"
3162
+ }
3163
+ this.visit = regexZeros(visits)
3075
3164
  this.caps = regexZeros(slots)
3076
3165
  this.mslots = regexZeros(slots)
3077
3166
  // Each instruction is visited once per closure and set of marks, and
@@ -3092,12 +3181,12 @@ export class Regex {
3092
3181
  }
3093
3182
  pc += 1
3094
3183
  }
3095
- if (toF64(frames) * toF64(states) > toF64(REGEX_MAX_SLOT_TABLE)) {
3184
+ // A `frames` past 2^31 reads as negative, and fails here too.
3185
+ const frameStates: i32 = regexTableSize(toI32(frames), states)
3186
+ if (frameStates < 0 || frameStates > 4194304) {
3096
3187
  return "too many groups for the pattern's size"
3097
3188
  }
3098
- // Unsigned: the test above keeps `frames * states` within the slot
3099
- // table's limit, and three times that far from wrapping.
3100
- this.stk = regexZeros(toI32(frames * toU32(states) * 3))
3189
+ this.stk = regexZeros(frameStates * 3)
3101
3190
  this.findPrefilters()
3102
3191
  if (this.revOk && !this.anchored && this.prefix === "") {
3103
3192
  this.findInner(p, root)
@@ -3131,12 +3220,12 @@ export class Regex {
3131
3220
  while (i >= 0 && i < toI32(kids.length)) {
3132
3221
  // Read before the calls below, which end what the loop's test proves.
3133
3222
  const first: i32 = kids[i]
3134
- // Unsigned: `end` counts up from `i` and stops at `count`.
3223
+ // `end` counts up from `i` and stops at `count`.
3135
3224
  let end: i32 = i
3136
3225
  while (end < count && this.isAsciiChar(p, regexItem(kids, end, REGEX_NONE))) {
3137
- end = toI32(toU32(end) + 1)
3226
+ end = end + 1
3138
3227
  }
3139
- const n: i32 = toI32(toU32(end) - toU32(i))
3228
+ const n: i32 = end - i
3140
3229
  if (n > 0) {
3141
3230
  const b: i32 = p.aOf(first)
3142
3231
  const score: i32 = regexByteScore(b)
@@ -3161,14 +3250,16 @@ export class Regex {
3161
3250
  }
3162
3251
  const bytes: u8[] = regexZeroBytes(bestLen)
3163
3252
  i = 0
3164
- while (i < toI32(bytes.length)) {
3165
- // Unsigned: the run at `best` lies within the kids.
3166
- bytes[i] = toU8(p.aOf(regexItem(kids, toI32(toU32(best) + toU32(i)), REGEX_NONE)))
3253
+ // The run at `best` lies within the kids, so this copies all of it.
3254
+ let at: i32 = best
3255
+ while (i >= 0 && i < toI32(bytes.length) && at >= 0 && at < toI32(kids.length)) {
3256
+ bytes[i] = toU8(p.aOf(kids[at]))
3167
3257
  i += 1
3258
+ at += 1
3168
3259
  }
3169
3260
  this.innerEntry = this.pc()
3170
3261
  // The run is never the first part, so `best` is at least 1.
3171
- i = toI32(toU32(best) - 1)
3262
+ i = best > 0 ? best - 1 : -1
3172
3263
  while (i >= 0 && i < toI32(kids.length)) {
3173
3264
  this.compileNode(p, kids[i], true)
3174
3265
  i -= 1
@@ -3251,8 +3342,8 @@ export class Regex {
3251
3342
  i += 1
3252
3343
  }
3253
3344
  this.rareAt = rare
3254
- // Unsigned: `rare` is a byte of the prefix.
3255
- this.rareStr = regexSpan(this.prefix, rare, toI32(toU32(rare) + 1))
3345
+ // `rare` is a byte of the prefix, so the test always holds.
3346
+ this.rareStr = regexSpan(this.prefix, rare, rare < n ? rare + 1 : n)
3256
3347
  }
3257
3348
  pc = 0
3258
3349
  while (pc >= 0 && pc < toI32(op.length) && op[pc] === OP_SAVE) {
@@ -3349,16 +3440,19 @@ export class Regex {
3349
3440
  ) {
3350
3441
  wide = true
3351
3442
  }
3352
- // Unsigned: `k` is a class, and its table is 128 bytes of `ascii`.
3443
+ // `k` is a class, and its table is 128 bytes of `ascii`; a `k`
3444
+ // the test turns away is one the tables lack, as above.
3353
3445
  const ascii: u8[] = this.clsAscii
3354
- const base: u32 = toU32(k) * 128
3355
- let c: i32 = 0
3356
- while (c >= 0 && c < toI32(set.length)) {
3357
- const idx: i32 = toI32(base + toU32(c))
3358
- if (idx >= 0 && idx < toI32(ascii.length) && ascii[idx] === 1) {
3359
- set[c] = 1
3446
+ if (k >= 0 && k < 16777216) {
3447
+ const base: i32 = k * 128
3448
+ let c: i32 = 0
3449
+ while (c >= 0 && c < 128 && c < toI32(set.length)) {
3450
+ const idx: i32 = base + c
3451
+ if (idx < toI32(ascii.length) && ascii[idx] === 1) {
3452
+ set[c] = 1
3453
+ }
3454
+ c += 1
3360
3455
  }
3361
- c += 1
3362
3456
  }
3363
3457
  next.push(at + 1)
3364
3458
  } else if (o === OP_ANY || o === OP_ANY_NL) {
@@ -3425,26 +3519,26 @@ export class Regex {
3425
3519
  frontier = next
3426
3520
  }
3427
3521
  // No position, or a first one that can be inside a character: no scan.
3428
- if (depth === 0 || (toI32(masks.length) > 16 && masks[16] !== 0)) {
3522
+ // The walk stops at `REGEX_SCAN_DEPTH`, 8, so the last test never holds.
3523
+ if (depth <= 0 || (toI32(masks.length) > 16 && masks[16] !== 0) || depth > 8) {
3429
3524
  return
3430
3525
  }
3431
3526
  this.scanDepth = depth
3432
3527
  // The 32-byte forms the vector window reads, then a membership table of
3433
3528
  // 256 bytes a position, which the walker and the kernel's other positions
3434
- // read a load a byte. Unsigned: `depth` is at most `REGEX_SCAN_DEPTH`.
3435
- const udepth: u32 = toU32(depth)
3436
- const vec: i32 = toI32(32 * udepth)
3437
- const both: u8[] = regexZeroBytes(toI32(288 * udepth))
3529
+ // read a load a byte.
3530
+ const vec: i32 = 32 * depth
3531
+ const both: u8[] = regexZeroBytes(288 * depth)
3438
3532
  let at: i32 = 0
3439
3533
  while (at >= 0 && at < vec && at < toI32(both.length) && at < toI32(masks.length)) {
3440
3534
  both[at] = masks[at]
3441
3535
  at += 1
3442
3536
  }
3443
3537
  at = 0
3444
- while (at >= 0 && at < toI32(256 * udepth)) {
3538
+ while (at >= 0 && at < 256 * depth && at < 2048) {
3445
3539
  const b: i32 = at & 255
3446
3540
  const row: i32 = ((at >> 8) << 5) | ((b >> 7) << 4) | (b & 15)
3447
- const slot: i32 = toI32(toU32(vec) + toU32(at))
3541
+ const slot: i32 = vec + at
3448
3542
  if (slot >= 0 && slot < toI32(both.length) && row >= 0 && row < toI32(masks.length)) {
3449
3543
  both[slot] = toU8((toI32(masks[row]) >> ((b >> 4) & 7)) & 1)
3450
3544
  }
@@ -3479,8 +3573,9 @@ export class Regex {
3479
3573
  off += 1
3480
3574
  }
3481
3575
  let j: i32 = 0
3482
- while (j < width) {
3483
- all = toI32(toU32(all) * 1000)
3576
+ // At most `REGEX_SCAN_WIDTH` factors, so the bound never stops it.
3577
+ while (j < width && all > 0 && all < 2147483) {
3578
+ all = all * 1000
3484
3579
  j += 1
3485
3580
  }
3486
3581
  if (width >= most || best < all / 256) {
@@ -3501,7 +3596,8 @@ export class Regex {
3501
3596
  */
3502
3597
  private prefixIsScan(): boolean {
3503
3598
  const n: i32 = toI32(this.prefix.length)
3504
- if (n === 0 || n > this.scanDepth) {
3599
+ // `scanDepth` is at most `REGEX_SCAN_DEPTH`, 8.
3600
+ if (n === 0 || n > this.scanDepth || n > 8) {
3505
3601
  return false
3506
3602
  }
3507
3603
  let j: i32 = 0
@@ -3514,8 +3610,7 @@ export class Regex {
3514
3610
  while (k < 32) {
3515
3611
  const bit: i32 = toI32(1) << (c >> 4)
3516
3612
  const want: i32 = k === (c & 15) ? bit : 0
3517
- // Unsigned: the prefix fits the scan's positions, tested above.
3518
- if (regexByteItem(this.posMasks, toI32(32 * toU32(j) + toU32(k))) !== want) {
3613
+ if (regexByteItem(this.posMasks, 32 * j + k) !== want) {
3519
3614
  return false
3520
3615
  }
3521
3616
  k += 1
@@ -3528,11 +3623,14 @@ export class Regex {
3528
3623
  /** Whether class `k` holds code point `cp`, which is at least 0. */
3529
3624
  private classHas(k: i32, cp: i32): boolean {
3530
3625
  if (cp < 128) {
3531
- // Unsigned: a class number is below the instruction limit, so its
3532
- // 128-byte row stays inside an i32.
3533
- const at: i32 = toI32(toU32(k) * 128 + toU32(cp))
3626
+ // A class number is below the instruction limit, so the test turns
3627
+ // away none the tables hold.
3628
+ if (k < 0 || k > 16777215 || cp < 0) {
3629
+ return false
3630
+ }
3631
+ const at: i32 = k * 128 + cp
3534
3632
  const ascii: u8[] = this.clsAscii
3535
- return at >= 0 && at < toI32(ascii.length) && ascii[at] === 1
3633
+ return at < toI32(ascii.length) && ascii[at] === 1
3536
3634
  }
3537
3635
  const from: i32[] = this.clsFrom
3538
3636
  const to: i32[] = this.clsTo
@@ -3648,12 +3746,18 @@ export class Regex {
3648
3746
  const levels: i32 = this.levels
3649
3747
  const states: i32 = 1 << levels
3650
3748
  const op: i32[] = this.op
3749
+ const nOp: i32 = toI32(op.length)
3651
3750
  const ia: i32[] = this.ia
3652
3751
  const ib: i32[] = this.ib
3653
3752
  const visit: i32[] = this.visit
3654
3753
  const caps: i32[] = this.caps
3655
3754
  const stk: i32[] = this.stk
3656
3755
  const gen: i32 = this.gen
3756
+ // `emit` stops the program at `REGEX_MAX_INSTRUCTIONS` and the marked
3757
+ // levels at `REGEX_MAX_LEVELS`, so this never turns a thread away.
3758
+ if (nOp > 100000 || states < 1 || states > 8) {
3759
+ return count
3760
+ }
3657
3761
  // `build` sizes the stack for every frame a closure can push, so the
3658
3762
  // tests on `top` below never turn a frame away.
3659
3763
  if (toI32(stk.length) < 3) {
@@ -3682,14 +3786,14 @@ export class Regex {
3682
3786
  while (true) {
3683
3787
  // Every jump the compiler emits lands inside the program, and every
3684
3788
  // instruction has both operands, so this test never ends a thread.
3685
- if (pc < 0 || pc >= toI32(op.length) || pc >= toI32(ia.length) || pc >= toI32(ib.length)) {
3789
+ if (pc < 0 || pc >= nOp || pc >= toI32(ia.length) || pc >= toI32(ib.length)) {
3686
3790
  break
3687
3791
  }
3688
3792
  const o: i32 = op[pc]
3689
- // Unsigned: `visit` holds `states` stamps for each pc, and `marked`
3690
- // is below `states`, so the slot is inside a table that fits an i32.
3691
- const row: u32 = toU32(pc) * toU32(states)
3692
- const at: i32 = toI32(o >= OP_SPLIT && o !== OP_MATCH ? row + toU32(marked) : row)
3793
+ // `visit` holds `states` stamps for each pc, and `marked` sets only
3794
+ // bits below `levels`, so the mask leaves it as it is.
3795
+ const row: i32 = pc * states
3796
+ const at: i32 = o >= OP_SPLIT && o !== OP_MATCH ? row + (marked & 7) : row
3693
3797
  if (at < 0 || at >= toI32(visit.length) || visit[at] === gen) {
3694
3798
  break
3695
3799
  }
@@ -3768,7 +3872,7 @@ export class Regex {
3768
3872
  dense[count] = pc
3769
3873
  count += 1
3770
3874
  }
3771
- regexStoreSlots(slots, toI32(toU32(pc) * toU32(ns)), caps, ns)
3875
+ regexStoreSlots(slots, pc, caps, ns)
3772
3876
  break
3773
3877
  }
3774
3878
  }
@@ -3790,23 +3894,22 @@ export class Regex {
3790
3894
  }
3791
3895
  const len: i32 = toI32(text.length)
3792
3896
  const n: i32 = toI32(prefix.length)
3793
- // Unsigned: `from` is in the text and `rareAt` inside the prefix.
3794
- let p: i32 = toI32(toU32(from) + toU32(rareAt))
3897
+ // The rare byte of a match at or after `from` is at or after `from` too:
3898
+ // a hit before `from + rareAt` is skipped rather than searched past.
3899
+ let p: i32 = from
3795
3900
  while (true) {
3796
3901
  const at: i32 = toI32(text.indexOf(this.rareStr, p))
3797
- if (at < 0) {
3902
+ if (at < 0 || at >= len) {
3798
3903
  return -1
3799
3904
  }
3800
- // `at` is at or after `p`, so `s` is at or after `from`.
3801
3905
  const s: i32 = at - rareAt
3802
- if (s < 0 || s > len - n) {
3906
+ if (s > len - n) {
3803
3907
  return -1
3804
3908
  }
3805
- if (regexPrefixAt(text, s, prefix)) {
3909
+ if (s >= from && regexPrefixAt(text, s, prefix)) {
3806
3910
  return s
3807
3911
  }
3808
- // `at` is a byte of the text, so one past it does not wrap.
3809
- p = toI32(toU32(at) + 1)
3912
+ p = at + 1
3810
3913
  }
3811
3914
  }
3812
3915
 
@@ -3830,8 +3933,10 @@ export class Regex {
3830
3933
  if (regexPrefixAt(text, s, this.prefix)) {
3831
3934
  return s
3832
3935
  }
3833
- // `s` is in the text, so one past it does not wrap.
3834
- p = toI32(toU32(s) + 1)
3936
+ if (s >= len) {
3937
+ return -1
3938
+ }
3939
+ p = s + 1
3835
3940
  }
3836
3941
  return -1
3837
3942
  }
@@ -3866,6 +3971,11 @@ export class Regex {
3866
3971
  private dfaInit(): void {
3867
3972
  this.dReady = true
3868
3973
  const total: i32 = this.pc()
3974
+ // `emit` stops the program at `REGEX_MAX_INSTRUCTIONS`, so this never
3975
+ // gives up.
3976
+ if (total < 0 || total > 1073741823) {
3977
+ return
3978
+ }
3869
3979
  const count: i32 = this.fwdCount
3870
3980
  const op: i32[] = this.op
3871
3981
  const ia: i32[] = this.ia
@@ -3903,20 +4013,21 @@ export class Regex {
3903
4013
  const points: i32[] = []
3904
4014
  const clsLo: i32[] = this.clsLo
3905
4015
  const clsHi: i32[] = this.clsHi
3906
- // Unsigned: a code point is at most U+10FFFF, so one past it fits.
4016
+ // A code point is at most U+10FFFF, so the tests on the top below hold
4017
+ // for every one.
3907
4018
  pc = 0
3908
4019
  while (pc < count && pc < toI32(op.length)) {
3909
4020
  const o: i32 = op[pc]
3910
4021
  const a: i32 = regexItem(ia, pc, REGEX_NONE)
3911
4022
  const b: i32 = regexItem(ib, pc, REGEX_NONE)
3912
4023
  if (o === OP_CHAR) {
3913
- if (a >= 128) {
4024
+ if (a >= 128 && a <= 1114111) {
3914
4025
  points.push(a)
3915
- points.push(toI32(toU32(a) + 1))
4026
+ points.push(a + 1)
3916
4027
  }
3917
- if (b >= 128 && b !== a) {
4028
+ if (b >= 128 && b <= 1114111 && b !== a) {
3918
4029
  points.push(b)
3919
- points.push(toI32(toU32(b) + 1))
4030
+ points.push(b + 1)
3920
4031
  }
3921
4032
  } else if (o === OP_CLASS) {
3922
4033
  const k: i32 = a
@@ -3928,9 +4039,9 @@ export class Regex {
3928
4039
  while (r >= 0 && r < to && r < toI32(clsHi.length) && r < toI32(clsLo.length)) {
3929
4040
  const hi: i32 = clsHi[r]
3930
4041
  const lo: i32 = clsLo[r]
3931
- if (hi >= 128) {
4042
+ if (hi >= 128 && hi <= 1114111) {
3932
4043
  points.push(lo > 128 ? lo : 128)
3933
- points.push(toI32(toU32(hi) + 1))
4044
+ points.push(hi + 1)
3934
4045
  }
3935
4046
  r += 1
3936
4047
  }
@@ -3961,12 +4072,12 @@ export class Regex {
3961
4072
  i += 1
3962
4073
  }
3963
4074
  const wide: i32 = toI32(lows.length)
3964
- // Unsigned: `wide` counts the boundaries, far below 2^31, and the test
3965
- // below bounds `units` for the products after it.
3966
- const units: i32 = toI32(129 + toU32(wide))
3967
- if (units > DFA_MAX_UNITS) {
4075
+ // `units` counts the ASCII bytes, the end of the text and the ranges, and
4076
+ // the tables below hold at most 1048576 of them.
4077
+ if (wide < 0 || wide > 1048447) {
3968
4078
  return
3969
4079
  }
4080
+ const units: i32 = 129 + wide
3970
4081
  // Each unit's representative: the ASCII bytes, the end of the text, then
3971
4082
  // the first code point of each range above ASCII.
3972
4083
  const rep: i32[] = []
@@ -3983,7 +4094,7 @@ export class Regex {
3983
4094
  // whether the predicate holds for it.
3984
4095
  const cls: i32[] = regexZeros(units)
3985
4096
  const hit: u8[] = regexZeroBytes(units)
3986
- const remap: i32[] = regexZeros(toI32(2 * toU32(units)))
4097
+ const remap: i32[] = regexZeros(2 * units)
3987
4098
  let n: i32 = 1
3988
4099
  let work: i32 = 0
3989
4100
  u = 0
@@ -4005,11 +4116,10 @@ export class Regex {
4005
4116
  const b: i32 = regexItem(ib, pc, REGEX_NONE)
4006
4117
  let use: boolean = false
4007
4118
  if (o === OP_CHAR) {
4008
- if (a < 128 && b < 128) {
4009
- // Unsigned: both are ASCII here.
4010
- const key: i32 = toI32(toU32(a) * 128 + toU32(b))
4011
- // Always true for a character: the test is what proves `key`.
4012
- if (key >= 0 && key < toI32(pairs.length)) {
4119
+ // A character is never negative, so the first two tests always hold.
4120
+ if (a >= 0 && b >= 0 && a < 128 && b < 128) {
4121
+ const key: i32 = a * 128 + b
4122
+ if (key < toI32(pairs.length)) {
4013
4123
  use = pairs[key] === 0
4014
4124
  pairs[key] = 1
4015
4125
  }
@@ -4043,8 +4153,12 @@ export class Regex {
4043
4153
  }
4044
4154
  }
4045
4155
  if (use) {
4046
- // Unsigned: the test below stops `work` at a bound far from wrapping.
4047
- work = toI32(toU32(work) + toU32(units))
4156
+ // The test after the sum stops `work` at `DFA_MAX_WORK`, so the one
4157
+ // before it never holds.
4158
+ if (work < 0 || work > 4194304) {
4159
+ return
4160
+ }
4161
+ work = work + units
4048
4162
  if (work > DFA_MAX_WORK) {
4049
4163
  return
4050
4164
  }
@@ -4077,10 +4191,10 @@ export class Regex {
4077
4191
  // One column past the classes for every byte above ASCII, whose entries
4078
4192
  // stay 0: the hot loop reads such a byte as a transition it cannot take
4079
4193
  // and leaves for the slow path, which decodes the character.
4080
- // Unsigned: `n` is at most `units`, so `shift` stops far below 31.
4194
+ // `n` is at most `units`, so `shift` stops far below 31.
4081
4195
  let shift: i32 = 0
4082
- while (1 << shift < toI32(toU32(n) + 1)) {
4083
- shift = toI32(toU32(shift) + 1)
4196
+ while (shift < 31 && 1 << shift <= n) {
4197
+ shift = shift + 1
4084
4198
  }
4085
4199
  if (shift > DFA_MAX_SHIFT) {
4086
4200
  return
@@ -4099,8 +4213,8 @@ export class Regex {
4099
4213
  this.dWide = wide
4100
4214
  const wcls: i32[] = regexZeros(wide)
4101
4215
  i = 0
4102
- while (i < toI32(wcls.length)) {
4103
- wcls[i] = regexItem(cls, toI32(129 + toU32(i)), 0)
4216
+ while (i < wide && i < toI32(wcls.length)) {
4217
+ wcls[i] = regexItem(cls, 129 + i, 0)
4104
4218
  i += 1
4105
4219
  }
4106
4220
  this.dWcls = wcls
@@ -4145,8 +4259,7 @@ export class Regex {
4145
4259
  this.dVisit = regexZeros(total)
4146
4260
  this.dLeaf = regexZeros(total)
4147
4261
  this.dBuf = regexZeros(total)
4148
- // Unsigned: `emit` bounds the program at `REGEX_MAX_INSTRUCTIONS`.
4149
- this.dStk = regexZeros(toI32(toU32(total) + 1))
4262
+ this.dStk = regexZeros(total + 1)
4150
4263
  this.dMax = DFA_MAX_TABLE >> shift < DFA_MAX_STATES ? DFA_MAX_TABLE >> shift : DFA_MAX_STATES
4151
4264
  this.dCap = this.dMax < DFA_FIRST_STATES ? this.dMax : DFA_FIRST_STATES
4152
4265
  this.dTrans = regexZeros(this.dCap << shift)
@@ -4154,7 +4267,7 @@ export class Regex {
4154
4267
  this.dLen = regexZeros(this.dCap)
4155
4268
  this.dFlags = regexZeros(this.dCap)
4156
4269
  this.dHash = regexZeros(this.dHashSize(this.dCap))
4157
- const pool: i32 = toI32(2 * toU32(total))
4270
+ const pool: i32 = 2 * total
4158
4271
  this.dPoolMax = pool > DFA_MIN_POOL ? pool : DFA_MIN_POOL
4159
4272
  this.dPool = regexZeros(DFA_FIRST_POOL)
4160
4273
  this.dPoolLen = 0
@@ -4173,24 +4286,35 @@ export class Regex {
4173
4286
  * classes in order of first appearance. Answers how many there are.
4174
4287
  */
4175
4288
  private dRefine(cls: i32[], hit: u8[], units: i32, n: i32, remap: i32[]): i32 {
4176
- // Unsigned: classes number at most the code units, far below 2^31, and
4177
- // `next` counts new ones.
4289
+ // Classes number at most the units, which `dfaInit` caps at 1048576, and
4290
+ // `next` counts new ones: the tests on that cap below never hold.
4291
+ if (n < 0 || n > 1048576) {
4292
+ return n
4293
+ }
4178
4294
  let k: i32 = 0
4179
- while (k < toI32(2 * toU32(n)) && k < toI32(remap.length)) {
4295
+ while (k < 2 * n && k < toI32(remap.length)) {
4180
4296
  remap[k] = -1
4181
4297
  k += 1
4182
4298
  }
4183
4299
  let next: i32 = 0
4184
4300
  let u: i32 = 0
4185
4301
  while (u < units && u < toI32(cls.length) && u < toI32(hit.length)) {
4186
- const key: i32 = toI32(toU32(cls[u]) * 2 + toU32(hit[u]))
4187
- // Always true: a class is below `n`. The test is what proves `key`.
4188
- if (key >= 0 && key < toI32(remap.length)) {
4189
- if (remap[key] < 0) {
4190
- remap[key] = next
4191
- next = toI32(toU32(next) + 1)
4302
+ const c: i32 = cls[u]
4303
+ // Always true: a class is below `n`. The tests are what prove `key`.
4304
+ if (c >= 0 && c <= 1048576) {
4305
+ const key: i32 = c * 2 + toI32(hit[u])
4306
+ if (key < toI32(remap.length)) {
4307
+ if (remap[key] < 0) {
4308
+ // Never taken: `next` counts the classes in use, at most
4309
+ // `units`. The test is what proves `next + 1`.
4310
+ if (next < 0 || next > 2097152) {
4311
+ return next
4312
+ }
4313
+ remap[key] = next
4314
+ next = next + 1
4315
+ }
4316
+ cls[u] = remap[key]
4192
4317
  }
4193
- cls[u] = remap[key]
4194
4318
  }
4195
4319
  u += 1
4196
4320
  }
@@ -4214,22 +4338,28 @@ export class Regex {
4214
4338
  // `dfaInit` makes at least one wide class and `dWide` entries in each
4215
4339
  // array, so the tests on `lo` and `mid` below always hold and the class
4216
4340
  // 0 answer is never reached.
4217
- let lo: i32 = 0
4218
- let hi: i32 = toI32(toU32(this.dWide) - 1)
4341
+ const wide: i32 = this.dWide
4342
+ if (wide <= 0) {
4343
+ return 0
4344
+ }
4345
+ // The search runs in u32 from start to end, as `regexFoldsIn`'s does:
4346
+ // `lo` and `hi` are indexes into the arrays, which `dfaInit` caps far
4347
+ // below 2^31, so `lo + hi + 1` is exact, and an unsigned index needs only
4348
+ // its upper test. `mid` is above `lo`, so `mid - 1` does not go below 0.
4349
+ let lo: u32 = 0
4350
+ let hi: u32 = toU32(wide - 1)
4219
4351
  while (lo < hi) {
4220
- // Both are indexes into the arrays, so the sum cannot wrap; `mid` is
4221
- // above `lo`, so `mid - 1` is not negative.
4222
- const mid: i32 = toI32((toU32(lo) + toU32(hi) + 1) >>> 1)
4223
- if (mid < 0 || mid >= toI32(wlo.length)) {
4352
+ const mid: u32 = (lo + hi + 1) >>> 1
4353
+ if (mid >= toU32(wlo.length)) {
4224
4354
  return 0
4225
4355
  }
4226
4356
  if (wlo[mid] <= cp) {
4227
4357
  lo = mid
4228
4358
  } else {
4229
- hi = toI32(toU32(mid) - 1)
4359
+ hi = mid - 1
4230
4360
  }
4231
4361
  }
4232
- if (lo < 0 || lo >= toI32(wcls.length)) {
4362
+ if (lo >= toU32(wcls.length)) {
4233
4363
  return 0
4234
4364
  }
4235
4365
  return wcls[lo]
@@ -4352,30 +4482,40 @@ export class Regex {
4352
4482
  id >= toI32(lens.length) ||
4353
4483
  id >= toI32(flagsOf.length) ||
4354
4484
  c < 0 ||
4355
- c >= toI32(nextK.length)
4485
+ c >= toI32(nextK.length) ||
4486
+ off < 0 ||
4487
+ off > 1073741823 ||
4488
+ c > 1048576
4356
4489
  ) {
4357
4490
  return 0
4358
4491
  }
4359
4492
  const start: i32 = starts[id]
4360
4493
  const len: i32 = lens[id]
4494
+ // A state's instructions are inside the pool, which holds far fewer
4495
+ // than 2^30.
4496
+ if (start < 0 || start > 1073741823 || len > 1073741823) {
4497
+ return 0
4498
+ }
4361
4499
  const flags: i32 = flagsOf[id]
4362
4500
  const restart: boolean = (flags & DFA_RESTART) !== 0
4363
4501
  const rev: boolean = (flags & DFA_REV) !== 0
4364
4502
  const ctx: i32 = (flags >> 1) & 3
4365
4503
  const nk: i32 = nextK[c]
4366
4504
  const epoch: i32 = this.dEpoch
4367
- if (this.dGen >= 2147483647) {
4505
+ // Reset when it reaches the i32 maximum, so it cannot wrap.
4506
+ const gen: i32 = this.dGen
4507
+ if (gen < 0 || gen >= 2147483647) {
4368
4508
  this.dVisit.fill(0)
4369
- this.dGen = 0
4509
+ this.dGen = 1
4510
+ } else {
4511
+ this.dGen = gen + 1
4370
4512
  }
4371
- // Reset just above when it reaches the i32 maximum, so it cannot wrap.
4372
- this.dGen = toI32(toU32(this.dGen) + 1)
4373
4513
  const pool: i32[] = this.dPool
4374
4514
  let n: i32 = 0
4375
4515
  let i: i32 = 0
4376
4516
  while (i < len) {
4377
- const at: i32 = toI32(toU32(start) + toU32(i))
4378
- if (at < 0 || at >= toI32(pool.length)) {
4517
+ const at: i32 = start + i
4518
+ if (at >= toI32(pool.length)) {
4379
4519
  return 0
4380
4520
  }
4381
4521
  n = this.dClose(pool[at], ctx, nk, n)
@@ -4444,7 +4584,7 @@ export class Regex {
4444
4584
  return 0
4445
4585
  }
4446
4586
  // `pc` is inside the program, so one more cannot wrap.
4447
- buf[nb] = toI32(toU32(pc) + 1)
4587
+ buf[nb] = pc + 1
4448
4588
  nb += 1
4449
4589
  }
4450
4590
  }
@@ -4479,14 +4619,15 @@ export class Regex {
4479
4619
  if (to !== 0 && entry === ((to << 4) | DFA_KNOWN)) {
4480
4620
  entry = to
4481
4621
  } else {
4482
- // A state shifted left by 4 with its flags: negated in u32, where it
4483
- // cannot overflow.
4484
- entry = toI32(0 - toU32(entry))
4622
+ // A state below `DFA_MAX_STATES` shifted left by 4 with its flags is
4623
+ // never negative, so this never gives the search up. `~` keeps the
4624
+ // decoding free of a sign test: `~entry` is at most -1, never 0.
4625
+ entry = entry >= 0 ? ~entry : 0
4485
4626
  }
4486
4627
  // The table is a cache: a slot outside it would only go unremembered.
4487
4628
  const trans: i32[] = this.dTrans
4488
- const at: i32 = toI32(toU32(off) + toU32(c))
4489
- if (epoch === this.dEpoch && at >= 0 && at < toI32(trans.length)) {
4629
+ const at: i32 = off + c
4630
+ if (epoch === this.dEpoch && at < toI32(trans.length)) {
4490
4631
  trans[at] = entry
4491
4632
  }
4492
4633
  return entry
@@ -4496,11 +4637,15 @@ export class Regex {
4496
4637
  private dHashOf(src: i32[], at: i32, n: i32, flags: i32): i32 {
4497
4638
  // In u32 the low bits the mask keeps are those of the i32 sum.
4498
4639
  let h: u32 = toU32(flags)
4640
+ // Callers hash a state's slots, which are inside `src`: this test and the
4641
+ // one in the loop never hold.
4642
+ if (at < 0 || at > 1073741823 || n > 1073741823) {
4643
+ return 0
4644
+ }
4499
4645
  let i: i32 = 0
4500
4646
  while (i < n) {
4501
- // Callers hash a state's slots, which are inside `src`.
4502
- const k: i32 = toI32(toU32(at) + toU32(i))
4503
- if (k < 0 || k >= toI32(src.length)) {
4647
+ const k: i32 = at + i
4648
+ if (k >= toI32(src.length)) {
4504
4649
  break
4505
4650
  }
4506
4651
  h = (h * 31 + toU32(src[k])) & toU32(DFA_HASH_MASK)
@@ -4524,14 +4669,15 @@ export class Regex {
4524
4669
  const flagsOf: i32[] = this.dFlags
4525
4670
  const lens: i32[] = this.dLen
4526
4671
  const starts: i32[] = this.dStart
4527
- if (n < 0 || n > toI32(buf.length)) {
4672
+ if (n < 0 || n > toI32(buf.length) || n > 1073741823) {
4528
4673
  return -1
4529
4674
  }
4530
4675
  const mask: i32 = toI32(hash.length) - 1
4531
4676
  let slot: i32 = this.dHashOf(buf, toI32(0), n, flags) & mask
4532
4677
  while (slot >= 0 && slot < toI32(hash.length) && hash[slot] !== 0) {
4533
4678
  // Slots hold an id plus one.
4534
- const id: i32 = toI32(toU32(hash[slot]) - 1)
4679
+ const stored: i32 = hash[slot]
4680
+ const id: i32 = stored > 0 ? stored - 1 : -1
4535
4681
  if (
4536
4682
  id >= 0 &&
4537
4683
  id < toI32(flagsOf.length) &&
@@ -4543,9 +4689,9 @@ export class Regex {
4543
4689
  const start: i32 = starts[id]
4544
4690
  const pool: i32[] = this.dPool
4545
4691
  let k: i32 = 0
4546
- while (k < n && k < toI32(buf.length)) {
4547
- const q: i32 = toI32(toU32(start) + toU32(k))
4548
- if (q < 0 || q >= toI32(pool.length) || pool[q] !== buf[k]) {
4692
+ while (k < n && k < toI32(buf.length) && start >= 0 && start <= 1073741823) {
4693
+ const q: i32 = start + k
4694
+ if (q >= toI32(pool.length) || pool[q] !== buf[k]) {
4549
4695
  break
4550
4696
  }
4551
4697
  k += 1
@@ -4604,7 +4750,8 @@ export class Regex {
4604
4750
  id >= toI32(lens.length) ||
4605
4751
  id >= toI32(flagsOf.length) ||
4606
4752
  base < 0 ||
4607
- toU32(base) + toU32(n) > toU32(pool.length)
4753
+ base > 1073741823 ||
4754
+ base + n > toI32(pool.length)
4608
4755
  ) {
4609
4756
  return -1
4610
4757
  }
@@ -4615,32 +4762,36 @@ export class Regex {
4615
4762
  let k: i32 = 0
4616
4763
  while (k < n && k < toI32(buf.length)) {
4617
4764
  // Inside the pool: checked above.
4618
- const q: i32 = toI32(toU32(base) + toU32(k))
4765
+ const q: i32 = base + k
4619
4766
  if (q < 0 || q >= toI32(pool.length)) {
4620
4767
  break
4621
4768
  }
4622
4769
  pool[q] = buf[k]
4623
4770
  k += 1
4624
4771
  }
4625
- this.dPoolLen = toI32(toU32(base) + toU32(n))
4772
+ this.dPoolLen = base + n
4626
4773
  const off: i32 = id << this.dShift
4627
4774
  const row: i32 = 1 << this.dShift
4628
- // A state's row ends inside the table, which holds `dCap` rows.
4629
- this.dTrans.fill(0, off, toI32(toU32(off) + toU32(row)))
4775
+ // A state's row ends inside the table, which holds `dCap` rows of at
4776
+ // most `2 ** DFA_MAX_SHIFT` entries.
4777
+ if (off < 0 || off > 1073741823 || row < 0 || row > 4096) {
4778
+ return -1
4779
+ }
4780
+ this.dTrans.fill(0, off, off + row)
4630
4781
  // In `hash`, checked after the probe; the calls since then cost the proof.
4631
4782
  if (slot >= 0 && slot < toI32(hash.length)) {
4632
- hash[slot] = toI32(toU32(id) + 1)
4783
+ hash[slot] = id + 1
4633
4784
  }
4634
4785
  return off
4635
4786
  }
4636
4787
 
4637
4788
  /** Double the state table, up to `dMax` states. */
4638
4789
  private dGrow(): void {
4639
- // The cache holds at most `dMax` states, far below 2^30: doubling it
4640
- // cannot wrap.
4641
- let cap: i32 = toI32(toU32(this.dCap) * 2)
4642
- if (cap > this.dMax) {
4643
- cap = this.dMax
4790
+ // The cache holds at most `dMax` states, far below 2^30.
4791
+ const have: i32 = this.dCap
4792
+ let cap: i32 = this.dMax
4793
+ if (have >= 0 && have <= 1073741823 && have * 2 < cap) {
4794
+ cap = have * 2
4644
4795
  }
4645
4796
  const used: i32 = this.dCount << this.dShift
4646
4797
  const trans: i32[] = regexZeros(cap << this.dShift)
@@ -4703,7 +4854,9 @@ export class Regex {
4703
4854
  // Only whether the count passed `DFA_FREE_FLUSHES` is read, so it stops
4704
4855
  // one past it and cannot wrap.
4705
4856
  const flushes: i32 = this.dFlushes
4706
- this.dFlushes = flushes > DFA_FREE_FLUSHES ? flushes : toI32(toU32(flushes) + 1)
4857
+ if (flushes >= 0 && flushes <= DFA_FREE_FLUSHES && flushes < 1073741823) {
4858
+ this.dFlushes = flushes + 1
4859
+ }
4707
4860
  const moved: f64 = Math.abs(toF64(p) - toF64(this.dPos))
4708
4861
  if (this.dFlushes > DFA_FREE_FLUSHES && moved < toF64(DFA_BYTES_PER_STATE) * toF64(this.dMax)) {
4709
4862
  return false
@@ -4714,21 +4867,27 @@ export class Regex {
4714
4867
  this.dHash.fill(0)
4715
4868
  this.dStartOff.fill(0)
4716
4869
  this.dPrefOff = 0
4717
- // Masked, so wrapping past 2^32 is harmless.
4718
- this.dEpoch = toI32((toU32(this.dEpoch) + 1) & toU32(DFA_EPOCH_MASK))
4870
+ // It counts modulo 2^30, back to 0 past 2^30 - 1.
4871
+ const epoch: i32 = this.dEpoch
4872
+ this.dEpoch = epoch >= 0 && epoch < 1073741823 ? epoch + 1 : 0
4719
4873
  return true
4720
4874
  }
4721
4875
 
4722
4876
  /** The start state for `mode` after a character of kind `ctx`, or -1. */
4723
4877
  private dStartAt(mode: i32, ctx: i32, p: i32): i32 {
4724
- // Modes and contexts are small constants, so the slot cannot wrap, and
4725
- // `dStartOff` has one per pair and `dBuf` room for a state: the -1 for a
4726
- // slot outside either, which hands the search to the NFA, is never
4727
- // answered.
4728
- const k: i32 = toI32(toU32(mode) * toU32(CTX_SLOTS) + toU32(ctx))
4878
+ // Modes and contexts are small constants, and `dStartOff` has one slot
4879
+ // per pair and `dBuf` room for a state: the -1 for a slot outside either,
4880
+ // which hands the search to the NFA, is never answered. In u32 a negative
4881
+ // mode or context is far above 15, so one test each bounds the slot.
4882
+ const m: u32 = toU32(mode)
4883
+ const x: u32 = toU32(ctx)
4884
+ if (m > 15 || x > 15) {
4885
+ return -1
4886
+ }
4887
+ const k: u32 = m * toU32(CTX_SLOTS) + x
4729
4888
  const startOff: i32[] = this.dStartOff
4730
4889
  const buf: i32[] = this.dBuf
4731
- if (k < 0 || k >= toI32(startOff.length) || toI32(buf.length) < 1) {
4890
+ if (k >= toU32(startOff.length) || toI32(buf.length) < 1) {
4732
4891
  return -1
4733
4892
  }
4734
4893
  const cached: i32 = startOff[k]
@@ -4750,9 +4909,9 @@ export class Regex {
4750
4909
  const off: i32 = this.dIntern(n, flags, p)
4751
4910
  // `dIntern` can flush the cache, which clears `dStartOff` in place.
4752
4911
  const after: i32[] = this.dStartOff
4753
- if (off >= 0 && k < toI32(after.length)) {
4754
- // A state's offset is inside the transition table: one more cannot wrap.
4755
- after[k] = toI32(toU32(off) + 1)
4912
+ if (off >= 0 && off < 1073741823 && k < toU32(after.length)) {
4913
+ // A state's offset is inside the transition table, far below the test.
4914
+ after[k] = off + 1
4756
4915
  }
4757
4916
  return off
4758
4917
  }
@@ -4779,15 +4938,20 @@ export class Regex {
4779
4938
  }
4780
4939
  const c: i32 = cls[b]
4781
4940
  const trans: i32[] = this.dTrans
4782
- const at: i32 = toI32(toU32(off) + toU32(c))
4783
- if (at < 0 || at >= toI32(trans.length)) {
4941
+ if (off > 1073741823 || c < 0 || c > 1073741823) {
4942
+ off = -1
4943
+ break
4944
+ }
4945
+ const at: i32 = off + c
4946
+ if (at >= toI32(trans.length)) {
4784
4947
  off = -1
4785
4948
  break
4786
4949
  }
4787
4950
  let e: i32 = trans[at]
4788
4951
  if (e === 0) {
4789
- // The prefix is in the text at `p`, so `p + k` cannot wrap.
4790
- e = this.dStep(off, c, toI32(toU32(p) + toU32(k)))
4952
+ // The prefix is in the text at `p`, and `dfaForward` turns away a
4953
+ // text of 2146435072 bytes or more, so `p + k` stays in an i32.
4954
+ e = this.dStep(off, c, p >= 0 && p <= 2146435071 && k <= 1048576 ? p + k : p)
4791
4955
  }
4792
4956
  off = e > 0 ? e : -1
4793
4957
  k += 1
@@ -4796,7 +4960,9 @@ export class Regex {
4796
4960
  this.dPrefFast = false
4797
4961
  return -1
4798
4962
  }
4799
- this.dPrefOff = toI32(toU32(off) + 1)
4963
+ if (off < 1073741823) {
4964
+ this.dPrefOff = off + 1
4965
+ }
4800
4966
  return off
4801
4967
  }
4802
4968
 
@@ -4838,9 +5004,10 @@ export class Regex {
4838
5004
  kind = CTX_LINE
4839
5005
  } else if (isRegexWordByte(b)) {
4840
5006
  kind = CTX_WORD
4841
- } else if (b === 0xe2 && regexByteAt(text, p + 1) === 0x80) {
4842
- // `b` was read, so `p` is in the text and `p + 2` cannot wrap.
4843
- const b2: i32 = regexByteAt(text, toI32(toU32(p) + 2))
5007
+ } else if (b === 0xe2 && p < 2146435071 && regexByteAt(text, p + 1) === 0x80) {
5008
+ // `b` was read, so `p` is in the text, which `run` keeps shorter
5009
+ // than the test.
5010
+ const b2: i32 = regexByteAt(text, p + 2)
4844
5011
  if (b2 === 0xa8 || b2 === 0xa9) {
4845
5012
  kind = CTX_LINE
4846
5013
  }
@@ -4866,22 +5033,27 @@ export class Regex {
4866
5033
  */
4867
5034
  private dfaForward(text: string, from: i32, first: boolean, pinned: boolean): i32 {
4868
5035
  const len: i32 = toI32(text.length)
5036
+ // `run` turns away a longer text or prefix, so every step below, past a
5037
+ // character or the prefix, stays in an i32.
5038
+ const n: i32 = toI32(this.prefix.length)
5039
+ if (len > 2146435071 || n > 1048576) {
5040
+ return DFA_GAVE_UP
5041
+ }
4869
5042
  const mode: i32 = this.anchored || pinned ? DFA_ANCHORED : DFA_UNANCHORED
4870
5043
  let p: i32 = from
4871
5044
  this.dStop = len
4872
5045
  let off: i32 = -1
4873
5046
  if (this.dSkip && !pinned) {
4874
5047
  p = this.dSkipTo(text, p)
4875
- if (p < 0) {
5048
+ if (p < 0 || p > len) {
4876
5049
  return DFA_NONE
4877
5050
  }
4878
5051
  if (this.dPrefFast) {
4879
- // The prefix is at `p`: start past it. `p` and the prefix are in
4880
- // the text, so the sum cannot wrap.
5052
+ // The prefix is at `p`: start past it.
4881
5053
  const prefOff: i32 = this.dPrefOff
4882
5054
  off = prefOff > 0 ? prefOff - 1 : this.dAfterPrefix(p)
4883
5055
  if (off >= 0) {
4884
- p = toI32(toU32(p) + toU32(this.prefix.length))
5056
+ p = p + n
4885
5057
  }
4886
5058
  }
4887
5059
  }
@@ -4917,8 +5089,8 @@ export class Regex {
4917
5089
  off = e
4918
5090
  p += 1
4919
5091
  } else {
4920
- // A negative entry is minus a state and its flags.
4921
- const neg: i32 = regexNegated(e)
5092
+ // A negative entry is the complement of a state and its flags.
5093
+ const neg: i32 = ~e
4922
5094
  if (e === 0 || (neg & (DFA_DEAD | DFA_IDLE)) !== 0) {
4923
5095
  break
4924
5096
  }
@@ -4946,10 +5118,9 @@ export class Regex {
4946
5118
  return DFA_GAVE_UP
4947
5119
  }
4948
5120
  }
4949
- // States and classes are both non-negative, and a state's row holds
4950
- // every class, so the slot is in the table; were it not, giving up
4951
- // hands the search to the NFA.
4952
- const at: i32 = toI32(toU32(off) + toU32(c))
5121
+ // A row starts at a multiple of `1 << dShift` and every class is below
5122
+ // that, so `|` is the sum, as in the hot loop, and cannot overflow.
5123
+ const at: i32 = off | c
4953
5124
  if (at < 0 || at >= toI32(trans.length)) {
4954
5125
  return DFA_GAVE_UP
4955
5126
  }
@@ -4969,10 +5140,10 @@ export class Regex {
4969
5140
  return end
4970
5141
  }
4971
5142
  off = e
4972
- p = toI32(toU32(p) + toU32(w))
5143
+ p = p + w
4973
5144
  continue
4974
5145
  }
4975
- e = regexNegated(e)
5146
+ e = ~e
4976
5147
  if ((e & DFA_MATCH) !== 0) {
4977
5148
  if (first) {
4978
5149
  this.dStop = p
@@ -4985,13 +5156,13 @@ export class Regex {
4985
5156
  return end
4986
5157
  }
4987
5158
  off = e >> 4
4988
- p = toI32(toU32(p) + toU32(w))
5159
+ p = p + w
4989
5160
  if ((e & DFA_IDLE) !== 0) {
4990
5161
  // No thread is alive and no match has been seen: skip to where the
4991
5162
  // next one can start. The state reached is the start state there
4992
5163
  // when the skip stays put or no assertion reads the context.
4993
5164
  const q: i32 = this.dSkipTo(text, p)
4994
- if (q < 0) {
5165
+ if (q < 0 || q > len) {
4995
5166
  return end
4996
5167
  }
4997
5168
  if (q !== p && !this.dCtxFree) {
@@ -5004,9 +5175,9 @@ export class Regex {
5004
5175
  p = q
5005
5176
  const prefOff: i32 = this.dPrefOff
5006
5177
  if (prefOff > 0 && this.dPrefFast) {
5007
- // The prefix is at `q`, inside the text: the sum cannot wrap.
5178
+ // The prefix is at `q`: start past it.
5008
5179
  off = prefOff - 1
5009
- p = toI32(toU32(q) + toU32(this.prefix.length))
5180
+ p = q + n
5010
5181
  }
5011
5182
  }
5012
5183
  }
@@ -5036,7 +5207,10 @@ export class Regex {
5036
5207
  // `q` is the byte before `p`. `p` only falls from `e`, so `q` stays
5037
5208
  // below `len` and above -2: the tests on it stand in for the bounds
5038
5209
  // check its read needs, and `q - 1` and `q + 1` cannot wrap.
5039
- let q: i32 = toI32(toU32(p) - 1)
5210
+ if (p < 0) {
5211
+ return DFA_GAVE_UP
5212
+ }
5213
+ let q: i32 = p - 1
5040
5214
  while (q >= from && q >= 0 && q < len) {
5041
5215
  const b: i32 = toI32(text.charCodeAt(q))
5042
5216
  // A byte or a slot outside its table gives up, as in `dfaForward`.
@@ -5052,8 +5226,8 @@ export class Regex {
5052
5226
  off = t
5053
5227
  q -= 1
5054
5228
  } else {
5055
- // Minus a state and its flags, as in `dfaForward`.
5056
- const neg: i32 = regexNegated(t)
5229
+ // The complement of a state and its flags, as in `dfaForward`.
5230
+ const neg: i32 = ~t
5057
5231
  if (t === 0 || (neg & DFA_DEAD) !== 0) {
5058
5232
  break
5059
5233
  }
@@ -5064,9 +5238,15 @@ export class Regex {
5064
5238
  q -= 1
5065
5239
  }
5066
5240
  }
5067
- // `q` is -1 at the least, where `p` is 0: in u32 -1 would wrap, which
5068
- // JavaScript's numbers do not.
5069
- p = q < 0 ? 0 : toI32(toU32(q) + 1)
5241
+ // `q` is -1 at the least, where `p` is 0, and below `len`.
5242
+ let here: i32 = len
5243
+ if (q < len) {
5244
+ here = q < 0 ? 0 : q + 1
5245
+ }
5246
+ if (here < 0) {
5247
+ return DFA_GAVE_UP
5248
+ }
5249
+ p = here
5070
5250
  // The character before `p`. At `from` it is read only for its kind,
5071
5251
  // so it is decoded from the start of the text.
5072
5252
  let c: i32 = this.dEot
@@ -5082,8 +5262,8 @@ export class Regex {
5082
5262
  let cp: i32 = REGEX_INVALID + b
5083
5263
  let k: i32 = 2
5084
5264
  while (k <= 4) {
5085
- // Below zero, `at` wraps to a negative value under `lim`.
5086
- const at: i32 = toI32(toU32(p) - toU32(k))
5265
+ // `p` is at least 0 and `k` at most 4.
5266
+ const at: i32 = here - (k & 7)
5087
5267
  if (at < lim) {
5088
5268
  break
5089
5269
  }
@@ -5104,8 +5284,8 @@ export class Regex {
5104
5284
  c = this.dWideClass(cp)
5105
5285
  }
5106
5286
  }
5107
- // As in `dfaForward`: the slot is in the table.
5108
- const at: i32 = toI32(toU32(off) + toU32(c))
5287
+ // As in `dfaForward`: `|` is the sum and cannot overflow.
5288
+ const at: i32 = off | c
5109
5289
  if (at < 0 || at >= toI32(trans.length)) {
5110
5290
  return DFA_GAVE_UP
5111
5291
  }
@@ -5120,22 +5300,22 @@ export class Regex {
5120
5300
  // Past here `p` is above `from`, and the `w` bytes before it were
5121
5301
  // decoded at or after `from`, so `p - w` cannot wrap.
5122
5302
  if (t > 0) {
5123
- if (p <= from) {
5303
+ if (p <= from || p <= 0) {
5124
5304
  return s
5125
5305
  }
5126
5306
  off = t
5127
- p = toI32(toU32(p) - toU32(w))
5307
+ p = p - (w & 7)
5128
5308
  continue
5129
5309
  }
5130
- t = regexNegated(t)
5310
+ t = ~t
5131
5311
  if ((t & DFA_MATCH) !== 0) {
5132
5312
  s = p
5133
5313
  }
5134
- if ((t & DFA_DEAD) !== 0 || p <= from) {
5314
+ if ((t & DFA_DEAD) !== 0 || p <= from || p <= 0) {
5135
5315
  return s
5136
5316
  }
5137
5317
  off = t >> 4
5138
- p = toI32(toU32(p) - toU32(w))
5318
+ p = p - (w & 7)
5139
5319
  }
5140
5320
  }
5141
5321
 
@@ -5159,7 +5339,7 @@ export class Regex {
5159
5339
  let lower: i32 = from
5160
5340
  while (this.innerLit !== "") {
5161
5341
  const i: i32 = toI32(text.indexOf(this.innerLit, lower))
5162
- if (i < 0) {
5342
+ if (i < 0 || i >= toI32(text.length)) {
5163
5343
  return DFA_NONE
5164
5344
  }
5165
5345
  this.innerGain += toF64(i) + 1.0 - toF64(lower)
@@ -5179,7 +5359,7 @@ export class Regex {
5179
5359
  }
5180
5360
  }
5181
5361
  // `i` is a byte of the text, so the next one cannot wrap.
5182
- lower = toI32(toU32(i) + 1)
5362
+ lower = i + 1
5183
5363
  }
5184
5364
  // No match starts before `lower`.
5185
5365
  const e: i32 = this.dfaForward(text, lower, first, false)
@@ -5198,17 +5378,22 @@ export class Regex {
5198
5378
  */
5199
5379
  private run(text: string, from: i32, want: i32): boolean {
5200
5380
  const len: i32 = toI32(text.length)
5201
- if (from < 0 || from > len) {
5381
+ // A text longer than 2146435071 bytes (2^31 - 2^20 - 1) never matches:
5382
+ // every position past a character, or past the literal prefix, then
5383
+ // fits an i32. `findPrefilters` keeps the prefix far shorter than
5384
+ // 1048576 bytes. `vm`, `dfaForward` and `count` repeat the test.
5385
+ const n: i32 = toI32(this.prefix.length)
5386
+ if (from < 0 || from > len || len > 2146435071 || n > 1048576) {
5202
5387
  return false
5203
5388
  }
5204
5389
  if (this.literal) {
5205
5390
  const at: i32 = this.findPrefix(text, from)
5206
- if (at < 0) {
5391
+ if (at < 0 || at > len) {
5207
5392
  return false
5208
5393
  }
5209
5394
  if (want >= REGEX_WANT_BOUNDS) {
5210
5395
  // The prefix found at `at` ends inside the text.
5211
- this.setBounds(at, toI32(toU32(at) + toU32(this.prefix.length)))
5396
+ this.setBounds(at, at + n)
5212
5397
  }
5213
5398
  return true
5214
5399
  }
@@ -5277,6 +5462,11 @@ export class Regex {
5277
5462
  private vm(text: string, from: i32, want: i32, pinned: boolean): boolean {
5278
5463
  const ns: i32 = want > 0 && this.width > this.groupSlots() ? this.width : want
5279
5464
  const len: i32 = toI32(text.length)
5465
+ // `run` turns away a longer text, so a step past a character stays in
5466
+ // an i32.
5467
+ if (len > 2146435071) {
5468
+ return false
5469
+ }
5280
5470
  const anchored: boolean = this.anchored || pinned
5281
5471
  const hasPrefix: boolean = this.prefix !== ""
5282
5472
  const hasScan: boolean = this.scanSel
@@ -5337,7 +5527,7 @@ export class Regex {
5337
5527
  break
5338
5528
  }
5339
5529
  // `p` is inside the text and a character is at most 4 bytes.
5340
- p = toI32(toU32(p) + toU32(regexWidthAt(text, p)))
5530
+ p = p + (regexWidthAt(text, p) & 7)
5341
5531
  continue
5342
5532
  }
5343
5533
  let cp: i32 = -1
@@ -5369,10 +5559,13 @@ export class Regex {
5369
5559
  hit = cp === ia[pc] || cp === ib[pc]
5370
5560
  } else if (o === OP_CLASS) {
5371
5561
  if (cp >= 0 && cp < 128) {
5372
- // Unsigned: a class number is below the instruction limit, so its
5373
- // 128-byte row stays inside an i32.
5374
- const at: i32 = toI32(toU32(ia[pc]) * 128 + toU32(cp))
5375
- hit = at >= 0 && at < toI32(clsAscii.length) && clsAscii[at] === 1
5562
+ // A class number is below the instruction limit, so the test on
5563
+ // `k` turns away none the tables hold.
5564
+ const k: i32 = ia[pc]
5565
+ if (k >= 0 && k < 16777216) {
5566
+ const at: i32 = k * 128 + cp
5567
+ hit = at < toI32(clsAscii.length) && clsAscii[at] === 1
5568
+ }
5376
5569
  } else {
5377
5570
  hit = cp >= 0 && this.classHas(ia[pc], cp)
5378
5571
  }
@@ -5384,14 +5577,13 @@ export class Regex {
5384
5577
  if (ns === 0) {
5385
5578
  return true
5386
5579
  }
5387
- // Unsigned: the slot table holds `ns` slots for every pc.
5388
- regexLoadSlots(mslots, cSlots, toI32(toU32(pc) * toU32(ns)), ns)
5580
+ regexLoadSlots(mslots, cSlots, pc, ns)
5389
5581
  matched = true
5390
5582
  // Every thread after this one has lower priority.
5391
5583
  break
5392
5584
  }
5393
5585
  if (hit) {
5394
- regexLoadSlots(caps, cSlots, toI32(toU32(pc) * toU32(ns)), ns)
5586
+ regexLoadSlots(caps, cSlots, pc, ns)
5395
5587
  // A hit read a character at `p`, inside the text; `pc` is below
5396
5588
  // the program's length.
5397
5589
  this.nCount = this.addThread(
@@ -5400,7 +5592,7 @@ export class Regex {
5400
5592
  this.nCount,
5401
5593
  pc + 1,
5402
5594
  text,
5403
- toI32(toU32(p) + toU32(w)),
5595
+ p < len ? p + w : len,
5404
5596
  ns
5405
5597
  )
5406
5598
  }
@@ -5416,7 +5608,7 @@ export class Regex {
5416
5608
  break
5417
5609
  }
5418
5610
  // `p` is inside the text and `w` the width of its character.
5419
- p = toI32(toU32(p) + toU32(w))
5611
+ p = p + w
5420
5612
  }
5421
5613
  return matched
5422
5614
  }
@@ -5518,15 +5710,20 @@ export class Regex {
5518
5710
  let n: u32 = 0
5519
5711
  let pos: i32 = 0
5520
5712
  if (this.literal && this.prefix !== "") {
5521
- const n0: u32 = toU32(this.prefix.length)
5713
+ const n0: i32 = toI32(this.prefix.length)
5714
+ // `run` finds no match in a longer text, and `findPrefilters` keeps
5715
+ // the prefix far shorter.
5716
+ if (len > 2146435071 || n0 > 1048576) {
5717
+ return 0
5718
+ }
5522
5719
  while (true) {
5523
5720
  const at: i32 = this.findPrefix(text, pos)
5524
- if (at < 0) {
5721
+ if (at < 0 || at > len) {
5525
5722
  return toI32(n)
5526
5723
  }
5527
5724
  n += 1
5528
- // The prefix found at `at` ends inside the text; unsigned, nothing wraps.
5529
- pos = toI32(toU32(at) + n0)
5725
+ // The prefix found at `at` ends inside the text.
5726
+ pos = at + n0
5530
5727
  }
5531
5728
  }
5532
5729
  if (!this.dReady) {
@@ -5637,7 +5834,7 @@ export class Regex {
5637
5834
 
5638
5835
  /** Two slots a group; `REGEX_MAX_GROUPS` keeps the product far from wrapping. */
5639
5836
  private groupSlots(): i32 {
5640
- return toI32(toU32(this.groups) * 2)
5837
+ return regexSlot(this.groups)
5641
5838
  }
5642
5839
 
5643
5840
  /**
@@ -5655,11 +5852,15 @@ export class Regex {
5655
5852
 
5656
5853
  /**
5657
5854
  * Where the search resumes after an empty match at `e`, which is inside
5658
- * the text: one character on. Unsigned, as a character's width is at
5659
- * most 4 and the sum stays at or below the text's length.
5855
+ * the text: one character on. `run` finds no match in a text of
5856
+ * 2146435072 bytes or more, so the 2147483647 past any text for an `e`
5857
+ * outside is never answered.
5660
5858
  */
5661
5859
  private pastEmpty(text: string, e: i32): i32 {
5662
- return toI32(toU32(e) + toU32(regexWidthAt(text, e)))
5860
+ if (e < 0 || e > 2146435071) {
5861
+ return 2147483647
5862
+ }
5863
+ return e + (regexWidthAt(text, e) & 7)
5663
5864
  }
5664
5865
 
5665
5866
  /**
@@ -5675,12 +5876,14 @@ export class Regex {
5675
5876
 
5676
5877
  /** Group `k` of the match in `mslots`, or "". */
5677
5878
  private slotText(text: string, ns: i32, k: i32): string {
5678
- // `2 * k` unsigned: a `k` of 2^30 or more wraps it negative, which the
5679
- // test turns away as it does a negative `k`; an even `at` plus one
5680
- // cannot wrap.
5681
- const at: i32 = toI32(toU32(k) * 2)
5682
- const end: i32 = toI32(toU32(at) + 1)
5683
- if (k < 0 || at < 0 || end >= ns) {
5879
+ // No group number reaches the test on `k`, which keeps `2 * k + 1` in
5880
+ // an i32.
5881
+ if (k < 0 || k > 1073741822) {
5882
+ return ""
5883
+ }
5884
+ const at: i32 = k * 2
5885
+ const end: i32 = at + 1
5886
+ if (end >= ns) {
5684
5887
  return ""
5685
5888
  }
5686
5889
  const from: i32 = this.slotOf(at)
@@ -5694,50 +5897,59 @@ export class Regex {
5694
5897
  private expand(rep: string, text: string, ns: i32, parts: string[]): void {
5695
5898
  const n: i32 = toI32(rep.length)
5696
5899
  // Group 0 is always there, so this is not negative.
5697
- const groupsAfterZero: i32 = toI32(toU32(this.groups) - 1)
5900
+ const groups: i32 = this.groups
5901
+ const groupsAfterZero: i32 = groups > 0 ? groups - 1 : 0
5698
5902
  let lit: i32 = 0
5699
5903
  let i: i32 = toI32(rep.indexOf("$"))
5700
5904
  while (i >= 0 && i < n - 1) {
5701
5905
  const c: i32 = toI32(rep.charCodeAt(i + 1))
5702
- let used: i32 = 0
5906
+ // Where the reference ends, or -1 when `$` is not one. Each end is
5907
+ // tested against `n`, so none goes past the template.
5908
+ const two: i32 = i + 2
5909
+ let next: i32 = -1
5703
5910
  let value: string = ""
5704
5911
  if (c === 36) {
5705
5912
  value = "$"
5706
- used = 2
5913
+ next = two
5707
5914
  } else if (c === 38) {
5708
5915
  value = this.slotText(text, ns, REGEX_WHOLE)
5709
- used = 2
5916
+ next = two
5710
5917
  } else if (c === 96) {
5711
5918
  value = text.substring(0, this.slotOf(toI32(0)))
5712
- used = 2
5919
+ next = two
5713
5920
  } else if (c === 39) {
5714
5921
  value = text.substring(this.slotOf(toI32(1)), toI32(text.length))
5715
- used = 2
5922
+ next = two
5716
5923
  } else if (c >= 48 && c <= 57) {
5717
5924
  const one: i32 = c - 48
5718
- const d: i32 = i + 2 < n ? toI32(rep.charCodeAt(i + 2)) : -1
5719
- if (d >= 48 && d <= 57 && one * 10 + (d - 48) >= 1 && one * 10 + (d - 48) <= groupsAfterZero) {
5925
+ const d: i32 = two < n ? toI32(rep.charCodeAt(two)) : -1
5926
+ if (
5927
+ two < n &&
5928
+ d >= 48 &&
5929
+ d <= 57 &&
5930
+ one * 10 + (d - 48) >= 1 &&
5931
+ one * 10 + (d - 48) <= groupsAfterZero
5932
+ ) {
5720
5933
  value = this.slotText(text, ns, one * 10 + (d - 48))
5721
- used = 3
5934
+ next = two + 1
5722
5935
  } else if (one >= 1 && one <= groupsAfterZero) {
5723
5936
  value = this.slotText(text, ns, one)
5724
- used = 2
5937
+ next = two
5725
5938
  }
5726
5939
  } else if (c === 60 && this.hasNames) {
5727
- const close: i32 = toI32(rep.indexOf(">", i + 2))
5728
- if (close >= 0) {
5729
- // `close` is at or after `i + 2`, so the unsigned width cannot wrap.
5730
- value = this.namedText(text, ns, rep, i + 2, close)
5731
- used = toI32(toU32(close) + 1 - toU32(i))
5940
+ const close: i32 = toI32(rep.indexOf(">", two))
5941
+ if (close >= 0 && close < n) {
5942
+ value = this.namedText(text, ns, rep, two, close)
5943
+ next = close + 1
5732
5944
  }
5733
5945
  }
5734
- if (used === 0) {
5946
+ if (next < 0) {
5735
5947
  i = toI32(rep.indexOf("$", i + 1))
5736
5948
  continue
5737
5949
  }
5738
5950
  parts.push(regexSpan(rep, lit, i))
5739
5951
  parts.push(value)
5740
- lit = toI32(toU32(i) + toU32(used))
5952
+ lit = next
5741
5953
  i = toI32(rep.indexOf("$", lit))
5742
5954
  }
5743
5955
  parts.push(regexSpan(rep, lit, n))