nish 0.44.0 → 0.46.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 (119) hide show
  1. package/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/docs/INSTALL.md +3 -1
  4. package/package.json +5 -5
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -80,7 +80,6 @@ import {
80
80
  DNS_ERR_QUESTION,
81
81
  DNS_ERR_RCODE,
82
82
  DNS_MAX_ANSWERS,
83
- DNS_MAX_LABEL_BYTES,
84
83
  DNS_MAX_NAME_BYTES,
85
84
  DNS_NOERROR,
86
85
  DNS_NXDOMAIN,
@@ -144,6 +143,10 @@ const DNS_Q_DONE: i32 = 3
144
143
  const DNS_L_FREE: i32 = 0
145
144
  const DNS_L_RUNNING: i32 = 1
146
145
  const DNS_L_DONE: i32 = 2
146
+ // One query's addresses and one lookup's, in bytes: module constants fold
147
+ // where a product of the imported ones written inline would not.
148
+ const DNS_RESOLVER_ANSWER_BYTES: i32 = DNS_MAX_ANSWERS * DNS_ADDRESS_BYTES
149
+ const DNS_RESOLVER_LOOKUP_BYTES: i32 = DNS_RESOLVE_MAX_ADDRESSES * DNS_ADDRESS_BYTES
147
150
 
148
151
  /**
149
152
  * A stub resolver of `maxLookups` concurrent lookups over `config`'s servers,
@@ -301,7 +304,10 @@ export class DnsResolver {
301
304
  this.config = config
302
305
  this.hosts = hosts
303
306
  this.maxLookups = maxLookups
304
- const n: i32 = maxLookups * 2
307
+ // The guard above holds `maxLookups` to 1 to 64, so both conversions are
308
+ // exact; as `u16`s the array lengths below are known to be non-negative.
309
+ const lookups: u16 = toU16(maxLookups)
310
+ const n: u16 = toU16(toI32(lookups) * 2)
305
311
  this.queries = []
306
312
  this.names = []
307
313
  this.starts = []
@@ -312,19 +318,19 @@ export class DnsResolver {
312
318
  this.lookupAddresses = []
313
319
  this.lookupTtls = []
314
320
  let q: i32 = 0
315
- while (q < n) {
321
+ while (q < toI32(n)) {
316
322
  this.queries.push(new Array<u8>(DNS_QUERY_MAX_BYTES))
317
323
  this.names.push(new Array<u8>(DNS_MAX_NAME_BYTES))
318
324
  this.starts.push(new Array<u8>(DNS_MAX_NAME_BYTES))
319
325
  this.streams.push(new Array<u8>(DNS_TCP_MAX_REPLY))
320
- this.answers.push(new Array<u8>(DNS_MAX_ANSWERS * DNS_ADDRESS_BYTES))
326
+ this.answers.push(new Array<u8>(DNS_RESOLVER_ANSWER_BYTES))
321
327
  this.answerTtls.push(new Array<i32>(DNS_MAX_ANSWERS))
322
328
  q += 1
323
329
  }
324
330
  let l: i32 = 0
325
- while (l < maxLookups) {
331
+ while (l < toI32(lookups)) {
326
332
  this.bases.push(new Array<u8>(DNS_MAX_NAME_BYTES))
327
- this.lookupAddresses.push(new Array<u8>(DNS_RESOLVE_MAX_ADDRESSES * DNS_ADDRESS_BYTES))
333
+ this.lookupAddresses.push(new Array<u8>(DNS_RESOLVER_LOOKUP_BYTES))
328
334
  this.lookupTtls.push(new Array<i32>(DNS_RESOLVE_MAX_ADDRESSES))
329
335
  l += 1
330
336
  }
@@ -341,16 +347,16 @@ export class DnsResolver {
341
347
  this.deadline = new Array<i64>(n)
342
348
  this.streamHave = new Array<i32>(n)
343
349
  this.streamWant = new Array<i32>(n)
344
- this.baseLength = new Array<i32>(maxLookups)
350
+ this.baseLength = new Array<i32>(lookups)
345
351
  this.nxdomain = new Array<boolean>(n)
346
- this.absolute = new Array<boolean>(maxLookups)
347
- this.candidate = new Array<i32>(maxLookups)
348
- this.candidates = new Array<i32>(maxLookups)
349
- this.fallback = new Array<i32>(maxLookups)
350
- this.dots = new Array<i32>(maxLookups)
351
- this.lookupState = new Array<i32>(maxLookups)
352
- this.lookupResult = new Array<i32>(maxLookups)
353
- this.lookupPort = new Array<i32>(maxLookups)
352
+ this.absolute = new Array<boolean>(lookups)
353
+ this.candidate = new Array<i32>(lookups)
354
+ this.candidates = new Array<i32>(lookups)
355
+ this.fallback = new Array<i32>(lookups)
356
+ this.dots = new Array<i32>(lookups)
357
+ this.lookupState = new Array<i32>(lookups)
358
+ this.lookupResult = new Array<i32>(lookups)
359
+ this.lookupPort = new Array<i32>(lookups)
354
360
  }
355
361
 
356
362
  /**
@@ -378,67 +384,116 @@ export class DnsResolver {
378
384
  if (port < 0 || port > 0xffff) {
379
385
  panic(`DnsResolver.start: the port ${port} is not 16 bits`)
380
386
  }
387
+ const lookupState: i32[] = this.lookupState
381
388
  let l: i32 = 0
382
- while (l < this.maxLookups && this.lookupState[l] !== DNS_L_FREE) {
389
+ while (l < this.maxLookups && l < toI32(lookupState.length) && lookupState[l] !== DNS_L_FREE) {
383
390
  l += 1
384
391
  }
385
- if (l === this.maxLookups) {
392
+ // There are `maxLookups` slots, at most 64, so `l > 63` never holds; it
393
+ // bounds `l * 2` for the panic prover. `result`, `cancel` and `settle`
394
+ // take a slot below both bounds, so `start` hands out no other.
395
+ if (l >= this.maxLookups || l >= toI32(lookupState.length) || l > 63) {
386
396
  return DNS_RESOLVE_BUSY
387
397
  }
388
- this.state[l * 2] = DNS_Q_IDLE
389
- this.state[l * 2 + 1] = DNS_Q_IDLE
390
- const at: u8[] = this.lookupAddresses[l]
391
- const ttls: i32[] = this.lookupTtls[l]
398
+ const a: i32 = l * 2
399
+ const state: i32[] = this.state
400
+ const answers: u8[][] = this.answers
401
+ const lookupAddresses: u8[][] = this.lookupAddresses
402
+ const lookupTtls: i32[][] = this.lookupTtls
403
+ const bases: u8[][] = this.bases
404
+ // Each per-query array holds two entries per slot and each per-lookup one
405
+ // holds one, so no part of this holds.
406
+ if (
407
+ a >= toI32(state.length) - 1 ||
408
+ a >= toI32(answers.length) - 1 ||
409
+ l >= toI32(lookupAddresses.length) ||
410
+ l >= toI32(lookupTtls.length) ||
411
+ l >= toI32(bases.length)
412
+ ) {
413
+ return DNS_RESOLVE_BUSY
414
+ }
415
+ state[a] = DNS_Q_IDLE
416
+ state[a + 1] = DNS_Q_IDLE
417
+ const at: u8[] = lookupAddresses[l]
418
+ const ttls: i32[] = lookupTtls[l]
419
+ const base: u8[] = bases[l]
420
+ const four: u8[] = answers[a]
421
+ const six: u8[] = answers[a + 1]
392
422
  if (dnsResolverIsLocalhost(name)) {
393
423
  dnsResolverLoopback(at, port)
394
- ttls[0] = 0
395
- ttls[1] = 0
424
+ dnsResolverZeroTtls(ttls, 2)
396
425
  return dnsResolverFinishNow(this, l, 2)
397
426
  }
398
- if (dnsResolverLiteral(this.scratch, name, at, port)) {
399
- ttls[0] = 0
427
+ const scratch: u8[] = this.scratch
428
+ if (dnsResolverLiteral(scratch, name, at, port)) {
429
+ dnsResolverZeroTtls(ttls, 1)
400
430
  return dnsResolverFinishNow(this, l, 1)
401
431
  }
402
- const built: i32 = dnsBuildQuery(this.scratch, 0, name, DNS_TYPE_A)
432
+ const built: i32 = dnsBuildQuery(scratch, 0, name, DNS_TYPE_A)
403
433
  if (built < 0) {
404
434
  return built
405
435
  }
406
436
  const hosts: DnsHosts | null = this.hosts
407
437
  if (hosts !== null) {
408
- const v4: i32 = dnsHostsFind(hosts, name, DNS_TYPE_A, port, this.answers[l * 2], DNS_MAX_ANSWERS)
409
- const v6: i32 = dnsHostsFind(hosts, name, DNS_TYPE_AAAA, port, this.answers[l * 2 + 1], DNS_MAX_ANSWERS)
410
- if (v4 + v6 > 0) {
438
+ const v4: i32 = dnsHostsFind(hosts, name, DNS_TYPE_A, port, four, DNS_MAX_ANSWERS)
439
+ const v6: i32 = dnsHostsFind(hosts, name, DNS_TYPE_AAAA, port, six, DNS_MAX_ANSWERS)
440
+ // dnsHostsFind answers 0 to its `max`, DNS_MAX_ANSWERS (8).
441
+ if (v4 >= 0 && v4 <= 8 && v6 >= 0 && v6 <= 8 && v4 + v6 > 0) {
411
442
  dnsResolverGather(this, l, v4, v6, false)
412
443
  return dnsResolverFinishNow(this, l, v4 + v6)
413
444
  }
414
445
  }
415
446
  // The wire name is the query's bytes after the header, less the root byte
416
447
  // and the question's type and class.
417
- const base: u8[] = this.bases[l]
418
448
  const baseLength: i32 = built - 12 - 5
419
449
  let i: i32 = 0
420
450
  let dots: i32 = 0
421
- while (i < baseLength && i < toI32(base.length)) {
422
- base[i] = this.scratch[12 + i]
451
+ while (i < baseLength && i < toI32(base.length) && i < toI32(scratch.length) - 12) {
452
+ base[i] = scratch[12 + i]
423
453
  i += 1
424
454
  }
425
- // Every length byte past the first is a dot of the name.
455
+ // Every length byte past the first is a dot of the name. A wire name is
456
+ // at most 255 bytes, which `base` holds, so `p < 256` and `dots < 255`
457
+ // never stop the walk; they bound it for the panic prover.
426
458
  let p: i32 = 0
427
- while (p >= 0 && p < baseLength && p < toI32(base.length)) {
459
+ while (p >= 0 && p < baseLength && p < toI32(base.length) && p < 256) {
428
460
  p += toI32(base[p]) + 1
429
- if (p < baseLength) {
461
+ if (p < baseLength && dots < 255) {
430
462
  dots += 1
431
463
  }
432
464
  }
433
- this.baseLength[l] = baseLength
434
- this.absolute[l] = toI32(name.charCodeAt(toI32(name.length) - 1)) === 46
435
- this.dots[l] = dots
436
- this.candidate[l] = 0
437
- this.fallback[l] = 0
438
- this.candidates[l] = this.absolute[l] ? 1 : this.config.searchCount + 1
439
- this.lookupPort[l] = port
440
- this.lookupState[l] = DNS_L_RUNNING
441
- this.lookupResult[l] = DNS_RESOLVE_PENDING
465
+ const nameLength: i32 = toI32(name.length)
466
+ const absolute: boolean = nameLength > 0 && toI32(name.charCodeAt(nameLength - 1)) === 46
467
+ const baseLengths: i32[] = this.baseLength
468
+ const absolutes: boolean[] = this.absolute
469
+ const dotCounts: i32[] = this.dots
470
+ const candidate: i32[] = this.candidate
471
+ const fallback: i32[] = this.fallback
472
+ const candidates: i32[] = this.candidates
473
+ const lookupPort: i32[] = this.lookupPort
474
+ const lookupResult: i32[] = this.lookupResult
475
+ if (
476
+ l >= toI32(baseLengths.length) ||
477
+ l >= toI32(absolutes.length) ||
478
+ l >= toI32(dotCounts.length) ||
479
+ l >= toI32(candidate.length) ||
480
+ l >= toI32(fallback.length) ||
481
+ l >= toI32(candidates.length) ||
482
+ l >= toI32(lookupPort.length) ||
483
+ l >= toI32(lookupState.length) ||
484
+ l >= toI32(lookupResult.length)
485
+ ) {
486
+ return DNS_RESOLVE_BUSY
487
+ }
488
+ baseLengths[l] = baseLength
489
+ absolutes[l] = absolute
490
+ dotCounts[l] = dots
491
+ candidate[l] = 0
492
+ fallback[l] = 0
493
+ candidates[l] = absolute ? 1 : dnsResolverCount(this.config.searchCount)
494
+ lookupPort[l] = port
495
+ lookupState[l] = DNS_L_RUNNING
496
+ lookupResult[l] = DNS_RESOLVE_PENDING
442
497
  if (!dnsResolverNextCandidate(this, l, nowNanos)) {
443
498
  dnsResolverFinish(this, l, DNS_RESOLVE_NOT_FOUND)
444
499
  }
@@ -483,44 +538,56 @@ export class DnsResolver {
483
538
  ) {
484
539
  panic("DnsResolver.nextOutgoing: out, to or meta is too short")
485
540
  }
486
- const n: i32 = this.maxLookups * 2
487
- let k: i32 = 0
488
- while (k < n) {
489
- const q: i32 = (this.cursor + k) % n
490
- const kind: i32 = this.pending[q]
491
- if (kind !== 0) {
492
- this.pending[q] = 0
493
- this.cursor = (q + 1) % n
494
- meta[0] = kind
495
- meta[1] = 0
496
- if (kind === DNS_CLOSE) {
497
- return q
498
- }
499
- const query: u8[] = this.queries[q]
500
- setU16BE(query, 0, toU16(id))
501
- const length: i32 = this.queryLength[q]
502
- const skip: i32 = kind === DNS_SEND_TCP ? 2 : 0
503
- if (skip === 2) {
504
- setU16BE(out, 0, toU16(length))
505
- }
506
- let i: i32 = 0
507
- while (i < length && i < toI32(query.length)) {
508
- out[skip + i] = query[i]
509
- i += 1
510
- }
511
- meta[1] = skip + length
512
- const from: i32 = this.server[q] * DNS_ADDRESS_BYTES
513
- i = 0
514
- while (i < DNS_ADDRESS_BYTES && i < toI32(to.length)) {
515
- to[i] = this.config.servers[from + i]
516
- i += 1
517
- }
518
- this.sent = dnsResolverCount(this.sent)
519
- return q
520
- }
521
- k += 1
541
+ // Queries take turns: the first owed one from the cursor on, then from 0.
542
+ const pending: i32[] = this.pending
543
+ const n: i32 = toI32(pending.length)
544
+ const cursor: i32 = this.cursor
545
+ let q: i32 = dnsResolverFirstPending(pending, cursor, n)
546
+ if (q < 0) {
547
+ q = dnsResolverFirstPending(pending, 0, cursor)
548
+ }
549
+ const queries: u8[][] = this.queries
550
+ const queryLength: i32[] = this.queryLength
551
+ const server: i32[] = this.server
552
+ // Every per-query array has the same length, so only `q < 0` (nothing
553
+ // owed) takes this.
554
+ if (
555
+ q < 0 ||
556
+ q >= n ||
557
+ q >= toI32(queries.length) ||
558
+ q >= toI32(queryLength.length) ||
559
+ q >= toI32(server.length)
560
+ ) {
561
+ return -1
522
562
  }
523
- return -1
563
+ const kind: i32 = pending[q]
564
+ pending[q] = 0
565
+ this.cursor = q + 1 < n ? q + 1 : 0
566
+ meta[0] = kind
567
+ meta[1] = 0
568
+ if (kind === DNS_CLOSE) {
569
+ return q
570
+ }
571
+ const query: u8[] = queries[q]
572
+ setU16BE(query, 0, toU16(id))
573
+ const length: i32 = queryLength[q]
574
+ const skip: i32 = kind === DNS_SEND_TCP ? 2 : 0
575
+ if (skip === 2) {
576
+ setU16BE(out, 0, toU16(length))
577
+ }
578
+ // A query's length is at most its buffer's, and `out` holds the longest
579
+ // with its two-byte prefix, so `o` ends at `skip + length`.
580
+ let i: i32 = 0
581
+ let o: i32 = skip
582
+ while (i < length && i < toI32(query.length) && o >= 0 && o < toI32(out.length)) {
583
+ out[o] = query[i]
584
+ i += 1
585
+ o += 1
586
+ }
587
+ meta[1] = o
588
+ dnsResolverServerAddress(this.config.servers, server[q], to)
589
+ this.sent = dnsResolverCount(this.sent)
590
+ return q
524
591
  }
525
592
 
526
593
  /**
@@ -546,27 +613,34 @@ export class DnsResolver {
546
613
  if (len < 0 || len > toI32(buf.length) || toI32(from.length) < DNS_ADDRESS_BYTES) {
547
614
  panic("DnsResolver.receive: len is outside buf, or from is shorter than 18 bytes")
548
615
  }
549
- if (this.state[q] !== DNS_Q_UDP) {
616
+ const state: i32[] = this.state
617
+ const server: i32[] = this.server
618
+ const queries: u8[][] = this.queries
619
+ const queryLength: i32[] = this.queryLength
620
+ const lookupPort: i32[] = this.lookupPort
621
+ const l: i32 = q / 2
622
+ // The check above holds `q` to a query, and every per-query array (and
623
+ // per-lookup one, by `q / 2`) is that long, so this never returns.
624
+ if (
625
+ q < 0 ||
626
+ q >= toI32(state.length) ||
627
+ q >= toI32(server.length) ||
628
+ q >= toI32(queries.length) ||
629
+ q >= toI32(queryLength.length) ||
630
+ l < 0 ||
631
+ l >= toI32(lookupPort.length)
632
+ ) {
633
+ return
634
+ }
635
+ if (state[q] !== DNS_Q_UDP) {
550
636
  this.droppedMalformed = dnsResolverCount(this.droppedMalformed)
551
637
  return
552
638
  }
553
- const at: i32 = this.server[q] * DNS_ADDRESS_BYTES
554
- let i: i32 = 0
555
- while (i < DNS_ADDRESS_BYTES && i < toI32(from.length)) {
556
- if (from[i] !== this.config.servers[at + i]) {
557
- this.droppedSource = dnsResolverCount(this.droppedSource)
558
- return
559
- }
560
- i += 1
639
+ if (!dnsResolverSameServer(this.config.servers, server[q], from)) {
640
+ this.droppedSource = dnsResolverCount(this.droppedSource)
641
+ return
561
642
  }
562
- const status: i32 = dnsParseReply(
563
- buf,
564
- len,
565
- this.queries[q],
566
- this.queryLength[q],
567
- this.lookupPort[q / 2],
568
- this.reply
569
- )
643
+ const status: i32 = dnsParseReply(buf, len, queries[q], queryLength[q], lookupPort[l], this.reply)
570
644
  if (status === DNS_ERR_ID) {
571
645
  this.droppedId = dnsResolverCount(this.droppedId)
572
646
  } else if (status === DNS_ERR_QUESTION) {
@@ -603,37 +677,63 @@ export class DnsResolver {
603
677
  if (len < 0 || len > toI32(buf.length)) {
604
678
  panic("DnsResolver.receiveStream: len is outside buf")
605
679
  }
606
- if (this.state[q] !== DNS_Q_TCP) {
680
+ const state: i32[] = this.state
681
+ const streams: u8[][] = this.streams
682
+ const streamHave: i32[] = this.streamHave
683
+ const streamWant: i32[] = this.streamWant
684
+ const queries: u8[][] = this.queries
685
+ const queryLength: i32[] = this.queryLength
686
+ const lookupPort: i32[] = this.lookupPort
687
+ const l: i32 = q / 2
688
+ // The check above holds `q` to a query, and every per-query array (and
689
+ // per-lookup one, by `q / 2`) is that long, so this never returns.
690
+ if (
691
+ q < 0 ||
692
+ q >= toI32(state.length) ||
693
+ q >= toI32(streams.length) ||
694
+ q >= toI32(streamHave.length) ||
695
+ q >= toI32(streamWant.length) ||
696
+ q >= toI32(queries.length) ||
697
+ q >= toI32(queryLength.length) ||
698
+ l < 0 ||
699
+ l >= toI32(lookupPort.length)
700
+ ) {
701
+ return
702
+ }
703
+ if (state[q] !== DNS_Q_TCP) {
607
704
  return
608
705
  }
609
- const stream: u8[] = this.streams[q]
706
+ const stream: u8[] = streams[q]
707
+ let have: i32 = streamHave[q]
708
+ let want: i32 = streamWant[q]
610
709
  let i: i32 = 0
611
- while (i < len) {
612
- const have: i32 = this.streamHave[q]
710
+ while (i < len && i < toI32(buf.length)) {
613
711
  if (have < 2) {
614
- this.streamWant[q] = (this.streamWant[q] << 8) | toI32(buf[i])
615
- if (have === 1 && (this.streamWant[q] < 12 || this.streamWant[q] > DNS_TCP_MAX_REPLY)) {
712
+ want = (want << 8) | toI32(buf[i])
713
+ if (have === 1 && (want < 12 || want > DNS_TCP_MAX_REPLY)) {
714
+ streamHave[q] = have
715
+ streamWant[q] = want
616
716
  dnsResolverServerFailed(this, q, nowNanos)
617
717
  return
618
718
  }
619
- } else if (have - 2 < this.streamWant[q]) {
620
- stream[have - 2] = buf[i]
719
+ } else {
720
+ // `want` passed the check above, so the stream, DNS_TCP_MAX_REPLY
721
+ // bytes, holds every byte below it.
722
+ const at: i32 = have - 2
723
+ if (at < want && at < toI32(stream.length)) {
724
+ stream[at] = buf[i]
725
+ }
621
726
  }
622
- this.streamHave[q] = have + 1
727
+ // Held at the i32 maximum: past the reply's length a byte only counts.
728
+ have = dnsResolverCount(have)
623
729
  i += 1
624
730
  }
625
- const want: i32 = this.streamWant[q]
626
- if (this.streamHave[q] < 2 || this.streamHave[q] - 2 < want) {
731
+ streamHave[q] = have
732
+ streamWant[q] = want
733
+ if (have < 2 || have - 2 < want) {
627
734
  return
628
735
  }
629
- const status: i32 = dnsParseReply(
630
- stream,
631
- want,
632
- this.queries[q],
633
- this.queryLength[q],
634
- this.lookupPort[q / 2],
635
- this.reply
636
- )
736
+ const status: i32 = dnsParseReply(stream, want, queries[q], queryLength[q], lookupPort[l], this.reply)
637
737
  if (status < 0 || this.reply.truncated) {
638
738
  dnsResolverServerFailed(this, q, nowNanos)
639
739
  } else {
@@ -654,7 +754,9 @@ export class DnsResolver {
654
754
  */
655
755
  fail(q: i32, nowNanos: i64): void {
656
756
  dnsResolverCheckQuery(this, q, "fail")
657
- if (this.state[q] === DNS_Q_UDP || this.state[q] === DNS_Q_TCP) {
757
+ const state: i32[] = this.state
758
+ // The check above holds `q` to a query, so `q` is always in `state`.
759
+ if (q >= 0 && q < toI32(state.length) && (state[q] === DNS_Q_UDP || state[q] === DNS_Q_TCP)) {
658
760
  dnsResolverServerFailed(this, q, nowNanos)
659
761
  }
660
762
  }
@@ -667,10 +769,11 @@ export class DnsResolver {
667
769
  * @param nowNanos The caller's monotonic clock now, in nanoseconds.
668
770
  */
669
771
  tick(nowNanos: i64): void {
670
- const n: i32 = this.maxLookups * 2
772
+ const state: i32[] = this.state
773
+ const deadline: i64[] = this.deadline
671
774
  let q: i32 = 0
672
- while (q < n) {
673
- if ((this.state[q] === DNS_Q_UDP || this.state[q] === DNS_Q_TCP) && this.deadline[q] <= nowNanos) {
775
+ while (q < toI32(state.length) && q < toI32(deadline.length)) {
776
+ if ((state[q] === DNS_Q_UDP || state[q] === DNS_Q_TCP) && deadline[q] <= nowNanos) {
674
777
  this.timeouts = dnsResolverCount(this.timeouts)
675
778
  dnsResolverServerFailed(this, q, nowNanos)
676
779
  }
@@ -686,15 +789,13 @@ export class DnsResolver {
686
789
  * -1 when no try is in flight.
687
790
  */
688
791
  nextDeadlineNanos(): i64 {
689
- const n: i32 = this.maxLookups * 2
792
+ const state: i32[] = this.state
793
+ const deadline: i64[] = this.deadline
690
794
  let soonest: i64 = -1
691
795
  let q: i32 = 0
692
- while (q < n) {
693
- if (
694
- (this.state[q] === DNS_Q_UDP || this.state[q] === DNS_Q_TCP) &&
695
- (soonest < 0 || this.deadline[q] < soonest)
696
- ) {
697
- soonest = this.deadline[q]
796
+ while (q < toI32(state.length) && q < toI32(deadline.length)) {
797
+ if ((state[q] === DNS_Q_UDP || state[q] === DNS_Q_TCP) && (soonest < 0 || deadline[q] < soonest)) {
798
+ soonest = deadline[q]
698
799
  }
699
800
  q += 1
700
801
  }
@@ -721,7 +822,16 @@ export class DnsResolver {
721
822
  * `DNS_RESOLVE_NO_ANSWER` or `DNS_RESOLVE_CNAME`.
722
823
  */
723
824
  result(lookup: i32, out: u8[], ttls: i32[]): i32 {
724
- if (lookup < 0 || lookup >= this.maxLookups || this.lookupState[lookup] === DNS_L_FREE) {
825
+ const lookupState: i32[] = this.lookupState
826
+ // `lookupState` holds `maxLookups` slots, at most 64, so the last two
827
+ // tests add nothing; they bound `lookup` for the panic prover.
828
+ if (
829
+ lookup < 0 ||
830
+ lookup >= this.maxLookups ||
831
+ lookup >= toI32(lookupState.length) ||
832
+ lookup > 63 ||
833
+ lookupState[lookup] === DNS_L_FREE
834
+ ) {
725
835
  panic(`DnsResolver.result: ${lookup} is not a lookup in progress`)
726
836
  }
727
837
  if (
@@ -730,21 +840,39 @@ export class DnsResolver {
730
840
  ) {
731
841
  panic("DnsResolver.result: out or ttls has no room for DNS_RESOLVE_MAX_ADDRESSES")
732
842
  }
733
- if (this.lookupState[lookup] !== DNS_L_DONE) {
843
+ if (lookupState[lookup] !== DNS_L_DONE) {
734
844
  return DNS_RESOLVE_PENDING
735
845
  }
736
- const count: i32 = this.lookupResult[lookup]
846
+ const lookupResult: i32[] = this.lookupResult
847
+ const lookupAddresses: u8[][] = this.lookupAddresses
848
+ const lookupTtls: i32[][] = this.lookupTtls
849
+ // Every per-lookup array holds `maxLookups` slots, so this never returns.
850
+ if (
851
+ lookup >= toI32(lookupResult.length) ||
852
+ lookup >= toI32(lookupAddresses.length) ||
853
+ lookup >= toI32(lookupTtls.length)
854
+ ) {
855
+ return DNS_RESOLVE_PENDING
856
+ }
857
+ const count: i32 = lookupResult[lookup]
858
+ const addresses: u8[] = lookupAddresses[lookup]
859
+ const addressTtls: i32[] = lookupTtls[lookup]
860
+ // A count is at most DNS_RESOLVE_MAX_ADDRESSES (16); a code copies nothing.
861
+ let bytes: i32 = 0
862
+ if (count > 0 && count <= 16) {
863
+ bytes = count * DNS_ADDRESS_BYTES
864
+ }
737
865
  let i: i32 = 0
738
- while (i < count * DNS_ADDRESS_BYTES && i < toI32(out.length)) {
739
- out[i] = this.lookupAddresses[lookup][i]
866
+ while (i < bytes && i < toI32(out.length) && i < toI32(addresses.length)) {
867
+ out[i] = addresses[i]
740
868
  i += 1
741
869
  }
742
870
  i = 0
743
- while (i < count && i < toI32(ttls.length)) {
744
- ttls[i] = this.lookupTtls[lookup][i]
871
+ while (i < count && i < toI32(ttls.length) && i < toI32(addressTtls.length)) {
872
+ ttls[i] = addressTtls[i]
745
873
  i += 1
746
874
  }
747
- this.lookupState[lookup] = DNS_L_FREE
875
+ lookupState[lookup] = DNS_L_FREE
748
876
  return count
749
877
  }
750
878
 
@@ -757,12 +885,21 @@ export class DnsResolver {
757
885
  * @param lookup The slot `start` answered.
758
886
  */
759
887
  cancel(lookup: i32): void {
760
- if (lookup < 0 || lookup >= this.maxLookups || this.lookupState[lookup] === DNS_L_FREE) {
888
+ const lookupState: i32[] = this.lookupState
889
+ // `lookupState` holds `maxLookups` slots, at most 64, so the last two
890
+ // tests add nothing; they bound `lookup` for the panic prover.
891
+ if (
892
+ lookup < 0 ||
893
+ lookup >= this.maxLookups ||
894
+ lookup >= toI32(lookupState.length) ||
895
+ lookup > 63 ||
896
+ lookupState[lookup] === DNS_L_FREE
897
+ ) {
761
898
  panic(`DnsResolver.cancel: ${lookup} is not a lookup in progress`)
762
899
  }
763
900
  dnsResolverStop(this, lookup * 2)
764
901
  dnsResolverStop(this, lookup * 2 + 1)
765
- this.lookupState[lookup] = DNS_L_FREE
902
+ lookupState[lookup] = DNS_L_FREE
766
903
  }
767
904
 
768
905
  /**
@@ -779,20 +916,35 @@ export class DnsResolver {
779
916
  * to read; true for one that had finished already.
780
917
  */
781
918
  settle(lookup: i32): boolean {
782
- if (lookup < 0 || lookup >= this.maxLookups || this.lookupState[lookup] === DNS_L_FREE) {
919
+ const lookupState: i32[] = this.lookupState
920
+ // `lookupState` holds `maxLookups` slots, at most 64, so the last two
921
+ // tests add nothing; they bound `lookup` for the panic prover.
922
+ if (
923
+ lookup < 0 ||
924
+ lookup >= this.maxLookups ||
925
+ lookup >= toI32(lookupState.length) ||
926
+ lookup > 63 ||
927
+ lookupState[lookup] === DNS_L_FREE
928
+ ) {
783
929
  panic(`DnsResolver.settle: ${lookup} is not a lookup in progress`)
784
930
  }
785
- if (this.lookupState[lookup] === DNS_L_DONE) {
931
+ if (lookupState[lookup] === DNS_L_DONE) {
786
932
  return true
787
933
  }
788
934
  const a: i32 = lookup * 2
935
+ const state: i32[] = this.state
936
+ const outcome: i32[] = this.outcome
937
+ // Each lookup has two queries in both, so this never returns.
938
+ if (a >= toI32(state.length) - 1 || a >= toI32(outcome.length) - 1) {
939
+ return false
940
+ }
789
941
  let open: i32 = -1
790
- if (this.state[a] === DNS_Q_DONE && this.outcome[a] > 0) {
942
+ if (state[a] === DNS_Q_DONE && outcome[a] > 0) {
791
943
  open = a + 1
792
- } else if (this.state[a + 1] === DNS_Q_DONE && this.outcome[a + 1] > 0) {
944
+ } else if (state[a + 1] === DNS_Q_DONE && outcome[a + 1] > 0) {
793
945
  open = a
794
946
  }
795
- if (open < 0 || this.state[open] === DNS_Q_DONE) {
947
+ if (open < 0 || open >= toI32(state.length) || state[open] === DNS_Q_DONE) {
796
948
  return false
797
949
  }
798
950
  // The other has addresses, so this finishes the lookup and never reaches a next name, which alone reads the clock.
@@ -807,11 +959,12 @@ export class DnsResolver {
807
959
  * 0 to `maxLookups`.
808
960
  */
809
961
  active(): i32 {
962
+ const lookupState: i32[] = this.lookupState
810
963
  let count: i32 = 0
811
964
  let l: i32 = 0
812
- while (l < this.maxLookups) {
813
- if (this.lookupState[l] !== DNS_L_FREE) {
814
- count += 1
965
+ while (l < toI32(lookupState.length)) {
966
+ if (lookupState[l] !== DNS_L_FREE) {
967
+ count = dnsResolverCount(count)
815
968
  }
816
969
  l += 1
817
970
  }
@@ -821,7 +974,8 @@ export class DnsResolver {
821
974
 
822
975
  /** Panics unless `q` names one of `r`'s queries. */
823
976
  const dnsResolverCheckQuery = (r: DnsResolver, q: i32, who: string): void => {
824
- if (q < 0 || q >= r.maxLookups * 2) {
977
+ // `q / 2 >= maxLookups` is `q >= 2 * maxLookups` without the product.
978
+ if (q < 0 || q / 2 >= r.maxLookups) {
825
979
  panic(`DnsResolver.${who}: ${q} is not a query`)
826
980
  }
827
981
  }
@@ -843,9 +997,14 @@ const dnsResolverIsLocalhost = (name: string): boolean => {
843
997
  if (n === 10 || (n > 10 && toI32(name.charCodeAt(n - 11)) === 46)) {
844
998
  return false
845
999
  }
1000
+ // `n` is at least 9 and at most the name's length, so `at` stays in it.
1001
+ let at: i32 = n - 9
846
1002
  let i: i32 = 0
847
1003
  while (i < 9 && i < toI32(word.length)) {
848
- let b: i32 = toI32(name.charCodeAt(n - 9 + i))
1004
+ if (at < 0 || at >= toI32(name.length)) {
1005
+ return false
1006
+ }
1007
+ let b: i32 = toI32(name.charCodeAt(at))
849
1008
  if (b >= 65 && b <= 90) {
850
1009
  b += 32
851
1010
  }
@@ -853,12 +1012,18 @@ const dnsResolverIsLocalhost = (name: string): boolean => {
853
1012
  return false
854
1013
  }
855
1014
  i += 1
1015
+ at += 1
856
1016
  }
857
1017
  return true
858
1018
  }
859
1019
 
860
1020
  /** Writes 127.0.0.1 then ::1, both with `port`, to `out`. */
861
1021
  const dnsResolverLoopback = (out: u8[], port: i32): void => {
1022
+ // `out` is a lookup's answer, DNS_RESOLVE_MAX_ADDRESSES addresses long, so
1023
+ // it always holds the two (36 bytes).
1024
+ if (toI32(out.length) < 36) {
1025
+ return
1026
+ }
862
1027
  let i: i32 = 0
863
1028
  while (i < 2 * DNS_ADDRESS_BYTES && i < toI32(out.length)) {
864
1029
  out[i] = toU8(0)
@@ -894,28 +1059,109 @@ const dnsResolverLiteral = (scratch: u8[], name: string, out: u8[], port: i32):
894
1059
  return dnsParseAddress(scratch, 0, n, out, 0, port)
895
1060
  }
896
1061
 
1062
+ /**
1063
+ * The first query from `from` up to `to` with an action owed, or -1 when there
1064
+ * is none.
1065
+ */
1066
+ const dnsResolverFirstPending = (pending: i32[], from: i32, to: i32): i32 => {
1067
+ let q: i32 = from < 0 ? 0 : from
1068
+ while (q >= 0 && q < to && q < toI32(pending.length)) {
1069
+ if (pending[q] !== 0) {
1070
+ return q
1071
+ }
1072
+ q += 1
1073
+ }
1074
+ return -1
1075
+ }
1076
+
1077
+ /**
1078
+ * Copies server `s`'s 18-byte address from `servers` to `to`. A resolver's
1079
+ * server is always one `servers` holds, at most `DNS_MAX_SERVERS` (3), so the
1080
+ * `s > 2` that would copy nothing never holds; it bounds the offset.
1081
+ */
1082
+ const dnsResolverServerAddress = (servers: u8[], s: i32, to: u8[]): void => {
1083
+ if (s < 0 || s > 2) {
1084
+ return
1085
+ }
1086
+ let at: i32 = s * DNS_ADDRESS_BYTES
1087
+ let i: i32 = 0
1088
+ while (i < DNS_ADDRESS_BYTES && i < toI32(to.length) && at < toI32(servers.length)) {
1089
+ to[i] = servers[at]
1090
+ i += 1
1091
+ at += 1
1092
+ }
1093
+ }
1094
+
1095
+ /**
1096
+ * Whether `from` is server `s`'s 18-byte address in `servers`. A resolver's
1097
+ * server is always one `servers` holds, at most `DNS_MAX_SERVERS` (3), so
1098
+ * `s > 2` never holds; it bounds the offset.
1099
+ */
1100
+ const dnsResolverSameServer = (servers: u8[], s: i32, from: u8[]): boolean => {
1101
+ if (s < 0 || s > 2) {
1102
+ return false
1103
+ }
1104
+ let at: i32 = s * DNS_ADDRESS_BYTES
1105
+ let i: i32 = 0
1106
+ while (i < DNS_ADDRESS_BYTES && i < toI32(from.length)) {
1107
+ if (at < 0 || at >= toI32(servers.length) || from[i] !== servers[at]) {
1108
+ return false
1109
+ }
1110
+ i += 1
1111
+ at += 1
1112
+ }
1113
+ return true
1114
+ }
1115
+
1116
+ /** Zeroes the first `count` TTLs of `ttls`: an answer that needed no packet. */
1117
+ const dnsResolverZeroTtls = (ttls: i32[], count: i32): void => {
1118
+ let i: i32 = 0
1119
+ while (i < count && i < toI32(ttls.length)) {
1120
+ ttls[i] = 0
1121
+ i += 1
1122
+ }
1123
+ }
1124
+
897
1125
  /** Finishes lookup `l` before any query was sent, with `count` addresses already in place. */
898
1126
  const dnsResolverFinishNow = (r: DnsResolver, l: i32, count: i32): i32 => {
899
- r.lookupState[l] = DNS_L_DONE
900
- r.lookupResult[l] = count
1127
+ dnsResolverSetResult(r, l, count)
901
1128
  return l
902
1129
  }
903
1130
 
904
1131
  /** Ends lookup `l` with `result`, and owes the driver a close for each of its queries still holding a socket. */
905
1132
  const dnsResolverFinish = (r: DnsResolver, l: i32, result: i32): void => {
906
- dnsResolverStop(r, l * 2)
907
- dnsResolverStop(r, l * 2 + 1)
908
- r.lookupState[l] = DNS_L_DONE
909
- r.lookupResult[l] = result
1133
+ // A lookup is below maxLookups, at most 64, so `l > 63` never holds; it
1134
+ // bounds `l * 2`.
1135
+ if (l >= 0 && l <= 63) {
1136
+ dnsResolverStop(r, l * 2)
1137
+ dnsResolverStop(r, l * 2 + 1)
1138
+ }
1139
+ dnsResolverSetResult(r, l, result)
1140
+ }
1141
+
1142
+ /** Marks lookup `l` done with `result`; every lookup is in both arrays. */
1143
+ const dnsResolverSetResult = (r: DnsResolver, l: i32, result: i32): void => {
1144
+ const lookupState: i32[] = r.lookupState
1145
+ const lookupResult: i32[] = r.lookupResult
1146
+ if (l >= 0 && l < toI32(lookupState.length) && l < toI32(lookupResult.length)) {
1147
+ lookupState[l] = DNS_L_DONE
1148
+ lookupResult[l] = result
1149
+ }
910
1150
  }
911
1151
 
912
1152
  /** Stops query `q`, and owes the driver a close if a socket may be open for it. */
913
1153
  const dnsResolverStop = (r: DnsResolver, q: i32): void => {
914
- if (r.state[q] === DNS_Q_UDP || r.state[q] === DNS_Q_TCP || r.pending[q] !== 0) {
915
- r.pending[q] = DNS_CLOSE
1154
+ const state: i32[] = r.state
1155
+ const pending: i32[] = r.pending
1156
+ // Every query is in both, so this never returns.
1157
+ if (q < 0 || q >= toI32(state.length) || q >= toI32(pending.length)) {
1158
+ return
1159
+ }
1160
+ if (state[q] === DNS_Q_UDP || state[q] === DNS_Q_TCP || pending[q] !== 0) {
1161
+ pending[q] = DNS_CLOSE
916
1162
  }
917
- if (r.state[q] !== DNS_Q_IDLE) {
918
- r.state[q] = DNS_Q_DONE
1163
+ if (state[q] !== DNS_Q_IDLE) {
1164
+ state[q] = DNS_Q_DONE
919
1165
  }
920
1166
  }
921
1167
 
@@ -927,27 +1173,56 @@ const dnsResolverStop = (r: DnsResolver, q: i32): void => {
927
1173
  * the file holds.
928
1174
  */
929
1175
  const dnsResolverGather = (r: DnsResolver, l: i32, v4: i32, v6: i32, fromServer: boolean): void => {
930
- const out: u8[] = r.lookupAddresses[l]
931
- const ttls: i32[] = r.lookupTtls[l]
1176
+ const lookupAddresses: u8[][] = r.lookupAddresses
1177
+ const lookupTtls: i32[][] = r.lookupTtls
1178
+ const answers: u8[][] = r.answers
1179
+ const answerTtls: i32[][] = r.answerTtls
1180
+ // A lookup is below maxLookups, at most 64, and has two queries in each
1181
+ // per-query array, so this never returns.
1182
+ if (
1183
+ l < 0 ||
1184
+ l > 63 ||
1185
+ l >= toI32(lookupAddresses.length) ||
1186
+ l >= toI32(lookupTtls.length) ||
1187
+ l * 2 >= toI32(answers.length) - 1 ||
1188
+ l * 2 >= toI32(answerTtls.length) - 1
1189
+ ) {
1190
+ return
1191
+ }
1192
+ const out: u8[] = lookupAddresses[l]
1193
+ const ttls: i32[] = lookupTtls[l]
932
1194
  let k: i32 = 0
933
1195
  let half: i32 = 0
934
1196
  while (half < 2) {
935
1197
  const q: i32 = l * 2 + half
1198
+ if (q >= toI32(answers.length) || q >= toI32(answerTtls.length)) {
1199
+ return
1200
+ }
1201
+ const answer: u8[] = answers[q]
1202
+ const answerTtl: i32[] = answerTtls[q]
936
1203
  const count: i32 = half === 0 ? v4 : v6
937
1204
  let least: i32 = 0
938
1205
  let j: i32 = 0
939
- while (fromServer && j < count) {
940
- const t: i32 = r.answerTtls[q][j]
1206
+ while (fromServer && j < count && j < toI32(answerTtl.length)) {
1207
+ const t: i32 = answerTtl[j]
941
1208
  if (j === 0 || t < least) {
942
1209
  least = t
943
1210
  }
944
1211
  j += 1
945
1212
  }
1213
+ // A query holds at most DNS_MAX_ANSWERS (8) addresses and a lookup
1214
+ // DNS_RESOLVE_MAX_ADDRESSES (16), the lengths of `answer` and `out`, so
1215
+ // the caps only bound the offsets.
946
1216
  let i: i32 = 0
947
- while (i < count && k >= 0 && k < toI32(ttls.length)) {
1217
+ while (i < count && i < 8 && k >= 0 && k < toI32(ttls.length) && k < 16) {
948
1218
  let b: i32 = 0
949
- while (b < DNS_ADDRESS_BYTES) {
950
- out[k * DNS_ADDRESS_BYTES + b] = r.answers[q][i * DNS_ADDRESS_BYTES + b]
1219
+ // 18 is DNS_ADDRESS_BYTES, written out so it bounds the offsets.
1220
+ while (b < 18) {
1221
+ const to: i32 = k * DNS_ADDRESS_BYTES + b
1222
+ const from: i32 = i * DNS_ADDRESS_BYTES + b
1223
+ if (to < toI32(out.length) && from < toI32(answer.length)) {
1224
+ out[to] = answer[from]
1225
+ }
951
1226
  b += 1
952
1227
  }
953
1228
  ttls[k] = least
@@ -967,30 +1242,57 @@ const dnsResolverGather = (r: DnsResolver, l: i32, v4: i32, v6: i32, fromServer:
967
1242
  * other name is each suffix and then itself.
968
1243
  */
969
1244
  const dnsResolverNextCandidate = (r: DnsResolver, l: i32, nowNanos: i64): boolean => {
970
- while (r.candidate[l] < r.candidates[l]) {
971
- const c: i32 = r.candidate[l]
1245
+ const candidate: i32[] = r.candidate
1246
+ const candidates: i32[] = r.candidates
1247
+ const absolute: boolean[] = r.absolute
1248
+ const dots: i32[] = r.dots
1249
+ const names: u8[][] = r.names
1250
+ // A lookup is below maxLookups, at most 64, with its two queries in
1251
+ // `names`, so this never returns.
1252
+ if (
1253
+ l < 0 ||
1254
+ l > 63 ||
1255
+ l >= toI32(candidate.length) ||
1256
+ l >= toI32(candidates.length) ||
1257
+ l >= toI32(absolute.length) ||
1258
+ l >= toI32(dots.length) ||
1259
+ l * 2 >= toI32(names.length) - 1
1260
+ ) {
1261
+ return false
1262
+ }
1263
+ const a: i32 = l * 2
1264
+ while (candidate[l] < candidates[l]) {
1265
+ const c: i32 = candidate[l]
972
1266
  const search: i32 = r.config.searchCount
973
1267
  let suffix: i32 = -1
974
- if (!r.absolute[l]) {
975
- if (r.dots[l] >= r.config.ndots) {
976
- suffix = c - 1
1268
+ if (!absolute[l]) {
1269
+ if (dots[l] >= r.config.ndots) {
1270
+ // Any negative suffix is no suffix, so `c` 0 or below gives -1.
1271
+ if (c > 0) {
1272
+ suffix = c - 1
1273
+ }
977
1274
  } else {
978
1275
  suffix = c < search ? c : -1
979
1276
  }
980
1277
  }
981
- const a: i32 = l * 2
982
- const length: i32 = dnsResolverCandidate(r, l, suffix, r.names[a])
1278
+ // As above: never taken.
1279
+ if (a >= toI32(names.length) - 1) {
1280
+ return false
1281
+ }
1282
+ const first: u8[] = names[a]
1283
+ const length: i32 = dnsResolverCandidate(r, l, suffix, first)
983
1284
  if (length > 0) {
984
1285
  dnsResolverBegin(r, a, length, DNS_TYPE_A, nowNanos)
1286
+ const second: u8[] = names[a + 1]
985
1287
  let i: i32 = 0
986
- while (i < length) {
987
- r.names[a + 1][i] = r.names[a][i]
1288
+ while (i < length && i < toI32(second.length) && i < toI32(first.length)) {
1289
+ second[i] = first[i]
988
1290
  i += 1
989
1291
  }
990
1292
  dnsResolverBegin(r, a + 1, length, DNS_TYPE_AAAA, nowNanos)
991
1293
  return true
992
1294
  }
993
- r.candidate[l] = c + 1
1295
+ candidate[l] = dnsResolverCount(c)
994
1296
  }
995
1297
  return false
996
1298
  }
@@ -1002,39 +1304,82 @@ const dnsResolverNextCandidate = (r: DnsResolver, l: i32, nowNanos: i64): boolea
1002
1304
  * bytes.
1003
1305
  */
1004
1306
  const dnsResolverCandidate = (r: DnsResolver, l: i32, suffix: i32, out: u8[]): i32 => {
1005
- const base: u8[] = r.bases[l]
1006
- let n: i32 = r.baseLength[l]
1307
+ const bases: u8[][] = r.bases
1308
+ const baseLengths: i32[] = r.baseLength
1309
+ // Every lookup has a base, and `start` keeps its length under 255, so
1310
+ // this never returns.
1311
+ if (l < 0 || l >= toI32(bases.length) || l >= toI32(baseLengths.length)) {
1312
+ return 0
1313
+ }
1314
+ const base: u8[] = bases[l]
1315
+ let n: i32 = baseLengths[l]
1316
+ if (n < 0 || n > 255) {
1317
+ return 0
1318
+ }
1007
1319
  let i: i32 = 0
1008
1320
  while (i < n && i < toI32(out.length) && i < toI32(base.length)) {
1009
1321
  out[i] = base[i]
1010
1322
  i += 1
1011
1323
  }
1012
1324
  if (suffix >= 0) {
1013
- const start: i32 = r.config.searchStart[suffix]
1014
- const length: i32 = r.config.searchLength[suffix]
1325
+ const starts: i32[] = r.config.searchStart
1326
+ const lengths: i32[] = r.config.searchLength
1327
+ const search: u8[] = r.config.search
1328
+ // A suffix is one of the config's domains, inside its 256-byte
1329
+ // DNS_MAX_SEARCH_BYTES buffer, so this never returns.
1330
+ if (suffix >= toI32(starts.length) || suffix >= toI32(lengths.length)) {
1331
+ return 0
1332
+ }
1333
+ const start: i32 = starts[suffix]
1334
+ const length: i32 = lengths[suffix]
1335
+ if (start < 0 || start > 256 || length < 0 || length > 256) {
1336
+ return 0
1337
+ }
1015
1338
  // The suffix's text becomes labels: each dot, and its end, closes the
1016
- // label whose length byte sits at `n`.
1339
+ // label whose length byte sits at `n`. The caps 63 (DNS_MAX_LABEL_BYTES)
1340
+ // and 255 or 252 (from DNS_MAX_NAME_BYTES) are written out so they bound
1341
+ // the offsets; each only restates the test beside it.
1017
1342
  let label: i32 = 0
1018
1343
  let k: i32 = 0
1019
1344
  while (k <= length) {
1020
- const b: i32 = k < length ? toI32(r.config.search[start + k]) : 46
1345
+ let b: i32 = 46
1346
+ if (k < length) {
1347
+ const at: i32 = start + k
1348
+ if (at >= toI32(search.length)) {
1349
+ return 0
1350
+ }
1351
+ b = toI32(search[at])
1352
+ }
1021
1353
  if (b === 46) {
1022
- if (label === 0 || label > DNS_MAX_LABEL_BYTES || n < 0 || n >= toI32(out.length)) {
1354
+ if (label === 0 || label > 63 || n < 0 || n > 255 || n >= toI32(out.length)) {
1023
1355
  return 0
1024
1356
  }
1025
1357
  out[n] = toU8(label)
1026
1358
  n += label + 1
1027
1359
  label = 0
1028
- } else if (b < 33 || b > 126 || n + 1 + label > DNS_MAX_NAME_BYTES - 2) {
1360
+ } else if (
1361
+ b < 33 ||
1362
+ b > 126 ||
1363
+ n < 0 ||
1364
+ n > 252 ||
1365
+ label < 0 ||
1366
+ label > 252 ||
1367
+ n + 1 + label > DNS_MAX_NAME_BYTES - 2
1368
+ ) {
1029
1369
  return 0
1030
1370
  } else {
1031
- out[n + 1 + label] = toU8(b)
1371
+ const at: i32 = n + 1 + label
1372
+ if (at >= toI32(out.length)) {
1373
+ return 0
1374
+ }
1375
+ out[at] = toU8(b)
1032
1376
  label += 1
1033
1377
  }
1034
1378
  k += 1
1035
1379
  }
1036
1380
  }
1037
- if (n + 1 > DNS_MAX_NAME_BYTES) {
1381
+ // `n > 254` is `n + 1 > DNS_MAX_NAME_BYTES`; `out` holds 255 bytes.
1382
+ if (n < 0 || n > 254 || n >= toI32(out.length)) {
1038
1383
  return 0
1039
1384
  }
1040
1385
  out[n] = toU8(0)
@@ -1047,12 +1392,29 @@ const dnsResolverCandidate = (r: DnsResolver, l: i32, suffix: i32, out: u8[]): i
1047
1392
  * server.
1048
1393
  */
1049
1394
  const dnsResolverBegin = (r: DnsResolver, q: i32, length: i32, qtype: i32, nowNanos: i64): void => {
1050
- r.hops[q] = 0
1051
- r.nameLength[q] = length
1395
+ const hops: i32[] = r.hops
1396
+ const nameLength: i32[] = r.nameLength
1397
+ const names: u8[][] = r.names
1398
+ const starts: u8[][] = r.starts
1399
+ // A query is below 2 * maxLookups, these arrays' length, so this never returns.
1400
+ if (
1401
+ q < 0 ||
1402
+ q >= toI32(hops.length) ||
1403
+ q >= toI32(nameLength.length) ||
1404
+ q >= toI32(names.length) ||
1405
+ q >= toI32(starts.length)
1406
+ ) {
1407
+ return
1408
+ }
1409
+ hops[q] = 0
1410
+ nameLength[q] = length
1411
+ const name: u8[] = names[q]
1412
+ const start: u8[] = starts[q]
1413
+ // Both hold 255 bytes, and a candidate name is at most that long.
1052
1414
  let i: i32 = 0
1053
- while (i < length) {
1054
- const b: i32 = toI32(r.names[q][i])
1055
- r.starts[q][i] = toU8(b >= 65 && b <= 90 ? b + 32 : b)
1415
+ while (i < length && i < toI32(name.length) && i < toI32(start.length)) {
1416
+ const b: i32 = toI32(name[i])
1417
+ start[i] = toU8(b >= 65 && b <= 90 ? b + 32 : b)
1056
1418
  i += 1
1057
1419
  }
1058
1420
  dnsResolverAsk(r, q, qtype, nowNanos)
@@ -1060,8 +1422,33 @@ const dnsResolverBegin = (r: DnsResolver, q: i32, length: i32, qtype: i32, nowNa
1060
1422
 
1061
1423
  /** Writes query `q`'s bytes for its current name and `qtype`, ID 0 until it is sent, and sends it to the first server. */
1062
1424
  const dnsResolverAsk = (r: DnsResolver, q: i32, qtype: i32, nowNanos: i64): void => {
1063
- const query: u8[] = r.queries[q]
1064
- const length: i32 = r.nameLength[q]
1425
+ const queries: u8[][] = r.queries
1426
+ const names: u8[][] = r.names
1427
+ const nameLength: i32[] = r.nameLength
1428
+ const queryLength: i32[] = r.queryLength
1429
+ const nxdomain: boolean[] = r.nxdomain
1430
+ const server: i32[] = r.server
1431
+ const round: i32[] = r.round
1432
+ // As in dnsResolverBegin: never taken.
1433
+ if (
1434
+ q < 0 ||
1435
+ q >= toI32(queries.length) ||
1436
+ q >= toI32(names.length) ||
1437
+ q >= toI32(nameLength.length) ||
1438
+ q >= toI32(queryLength.length) ||
1439
+ q >= toI32(nxdomain.length) ||
1440
+ q >= toI32(server.length) ||
1441
+ q >= toI32(round.length)
1442
+ ) {
1443
+ return
1444
+ }
1445
+ const query: u8[] = queries[q]
1446
+ const name: u8[] = names[q]
1447
+ const length: i32 = nameLength[q]
1448
+ // `255` is DNS_MAX_NAME_BYTES: every name written to `names` fits it, so this never returns.
1449
+ if (length < 0 || length > 255) {
1450
+ return
1451
+ }
1065
1452
  // The bytes are dnsBuildQuery's: the ID, RD, one question, then the name, type and class IN.
1066
1453
  setU16BE(query, 0, toU16(0))
1067
1454
  setU16BE(query, 2, toU16(0x0100))
@@ -1070,27 +1457,72 @@ const dnsResolverAsk = (r: DnsResolver, q: i32, qtype: i32, nowNanos: i64): void
1070
1457
  setU16BE(query, 8, toU16(0))
1071
1458
  setU16BE(query, 10, toU16(0))
1072
1459
  let i: i32 = 0
1073
- while (i < length && 12 + i < toI32(query.length)) {
1074
- query[12 + i] = r.names[q][i]
1460
+ let o: i32 = 12
1461
+ while (i < length && i < toI32(name.length) && o >= 0 && o < toI32(query.length)) {
1462
+ query[o] = name[i]
1075
1463
  i += 1
1464
+ o += 1
1076
1465
  }
1077
1466
  setU16BE(query, 12 + length, toU16(qtype))
1078
1467
  setU16BE(query, 14 + length, toU16(1))
1079
- r.queryLength[q] = 16 + length
1468
+ queryLength[q] = 16 + length
1080
1469
  // Only this name's reply may say NXDOMAIN, not an earlier candidate's or lookup's.
1081
- r.nxdomain[q] = false
1082
- r.server[q] = 0
1083
- r.round[q] = 0
1470
+ nxdomain[q] = false
1471
+ server[q] = 0
1472
+ round[q] = 0
1084
1473
  dnsResolverSend(r, q, DNS_Q_UDP, nowNanos)
1085
1474
  }
1086
1475
 
1087
1476
  /** Owes the driver a send of query `q` to its current server over `how`, with a deadline `timeout` seconds after `nowNanos`. */
1088
1477
  const dnsResolverSend = (r: DnsResolver, q: i32, how: i32, nowNanos: i64): void => {
1089
- r.state[q] = how
1090
- r.pending[q] = how === DNS_Q_TCP ? DNS_SEND_TCP : DNS_SEND_UDP
1091
- r.streamHave[q] = 0
1092
- r.streamWant[q] = 0
1093
- r.deadline[q] = nowNanos + toI64(r.config.timeout) * toI64(1000000000)
1478
+ const state: i32[] = r.state
1479
+ const pending: i32[] = r.pending
1480
+ const streamHave: i32[] = r.streamHave
1481
+ const streamWant: i32[] = r.streamWant
1482
+ const deadline: i64[] = r.deadline
1483
+ // As in dnsResolverBegin: never taken.
1484
+ if (
1485
+ q < 0 ||
1486
+ q >= toI32(state.length) ||
1487
+ q >= toI32(pending.length) ||
1488
+ q >= toI32(streamHave.length) ||
1489
+ q >= toI32(streamWant.length) ||
1490
+ q >= toI32(deadline.length)
1491
+ ) {
1492
+ return
1493
+ }
1494
+ state[q] = how
1495
+ pending[q] = how === DNS_Q_TCP ? DNS_SEND_TCP : DNS_SEND_UDP
1496
+ streamHave[q] = 0
1497
+ streamWant[q] = 0
1498
+ deadline[q] = dnsResolverDeadline(nowNanos, r.config.timeout)
1499
+ }
1500
+
1501
+ /** 2^62 - 1 nanoseconds, about 146 years: as far from 0 as a clock `dnsResolverDeadline` adds to. */
1502
+ const DNS_RESOLVER_FAR: i64 = 1073741824 * 4294967296 - 1
1503
+
1504
+ /** The last nanosecond an `i64` clock holds, 2^63 - 1: a deadline no clock reaches. */
1505
+ const DNS_RESOLVER_NEVER: i64 = DNS_RESOLVER_FAR * 2 + 1
1506
+
1507
+ /**
1508
+ * `timeout` seconds after `nowNanos`. `timeout * 10^9` is below 2^62 from 0
1509
+ * for any `i32`, so the sum fits wherever `nowNanos` is within 2^62 of 0,
1510
+ * which the masks below say without changing it. A clock later than that
1511
+ * gives `DNS_RESOLVER_NEVER`, a deadline no clock reaches, and one earlier,
1512
+ * which no monotonic clock reads, gives itself: a deadline already passed.
1513
+ */
1514
+ const dnsResolverDeadline = (nowNanos: i64, timeout: i32): i64 => {
1515
+ if (nowNanos > DNS_RESOLVER_FAR) {
1516
+ return DNS_RESOLVER_NEVER
1517
+ }
1518
+ if (nowNanos >= 0) {
1519
+ return (nowNanos & DNS_RESOLVER_FAR) + toI64(timeout) * 1000000000
1520
+ }
1521
+ if (nowNanos < -DNS_RESOLVER_FAR - 1) {
1522
+ return nowNanos
1523
+ }
1524
+ // `-1 - nowNanos` is `nowNanos`'s bitwise complement, at most 2^62 - 1 here, so the mask keeps it.
1525
+ return toI64(timeout) * 1000000000 - ((-1 - nowNanos) & DNS_RESOLVER_FAR) - 1
1094
1526
  }
1095
1527
 
1096
1528
  /**
@@ -1100,12 +1532,19 @@ const dnsResolverSend = (r: DnsResolver, q: i32, how: i32, nowNanos: i64): void
1100
1532
  */
1101
1533
  const dnsResolverServerFailed = (r: DnsResolver, q: i32, nowNanos: i64): void => {
1102
1534
  r.serverFailures = dnsResolverCount(r.serverFailures)
1103
- r.server[q] = r.server[q] + 1
1104
- if (r.server[q] >= r.config.serverCount) {
1105
- r.server[q] = 0
1106
- r.round[q] = r.round[q] + 1
1535
+ const server: i32[] = r.server
1536
+ const round: i32[] = r.round
1537
+ // As in dnsResolverBegin: never taken.
1538
+ if (q < 0 || q >= toI32(server.length) || q >= toI32(round.length)) {
1539
+ return
1107
1540
  }
1108
- if (r.round[q] >= r.config.attempts) {
1541
+ // A server is below serverCount and a round below attempts, so the counts never saturate.
1542
+ server[q] = dnsResolverCount(server[q])
1543
+ if (server[q] >= r.config.serverCount) {
1544
+ server[q] = 0
1545
+ round[q] = dnsResolverCount(round[q])
1546
+ }
1547
+ if (round[q] >= r.config.attempts) {
1109
1548
  dnsResolverDone(r, q, DNS_RESOLVE_NO_ANSWER, nowNanos)
1110
1549
  return
1111
1550
  }
@@ -1119,7 +1558,27 @@ const dnsResolverServerFailed = (r: DnsResolver, q: i32, nowNanos: i64): void =>
1119
1558
  */
1120
1559
  const dnsResolverAnswer = (r: DnsResolver, q: i32, status: i32, nowNanos: i64): void => {
1121
1560
  const reply: DnsReply = r.reply
1122
- r.nxdomain[q] = status === DNS_NXDOMAIN
1561
+ const nxdomain: boolean[] = r.nxdomain
1562
+ const answers: u8[][] = r.answers
1563
+ const answerTtls: i32[][] = r.answerTtls
1564
+ const hops: i32[] = r.hops
1565
+ const names: u8[][] = r.names
1566
+ const nameLength: i32[] = r.nameLength
1567
+ const starts: u8[][] = r.starts
1568
+ // As in dnsResolverBegin: never taken.
1569
+ if (
1570
+ q < 0 ||
1571
+ q >= toI32(nxdomain.length) ||
1572
+ q >= toI32(answers.length) ||
1573
+ q >= toI32(answerTtls.length) ||
1574
+ q >= toI32(hops.length) ||
1575
+ q >= toI32(names.length) ||
1576
+ q >= toI32(nameLength.length) ||
1577
+ q >= toI32(starts.length)
1578
+ ) {
1579
+ return
1580
+ }
1581
+ nxdomain[q] = status === DNS_NXDOMAIN
1123
1582
  if (status === DNS_NXDOMAIN) {
1124
1583
  dnsResolverDone(r, q, DNS_RESOLVE_NOT_FOUND, nowNanos)
1125
1584
  return
@@ -1128,18 +1587,25 @@ const dnsResolverAnswer = (r: DnsResolver, q: i32, status: i32, nowNanos: i64):
1128
1587
  dnsResolverServerFailed(r, q, nowNanos)
1129
1588
  return
1130
1589
  }
1131
- if (reply.count > 0) {
1590
+ const count: i32 = reply.count
1591
+ if (count > 0) {
1592
+ const answer: u8[] = answers[q]
1593
+ const addresses: u8[] = reply.addresses
1594
+ // `i / 18 < count` is `i < count * DNS_ADDRESS_BYTES` without the product;
1595
+ // both arrays hold DNS_MAX_ANSWERS addresses, as many as a reply counts.
1132
1596
  let i: i32 = 0
1133
- while (i < reply.count * DNS_ADDRESS_BYTES) {
1134
- r.answers[q][i] = reply.addresses[i]
1597
+ while (i < toI32(answer.length) && i < toI32(addresses.length) && i / 18 < count) {
1598
+ answer[i] = addresses[i]
1135
1599
  i += 1
1136
1600
  }
1601
+ const answerTtl: i32[] = answerTtls[q]
1602
+ const ttls: i32[] = reply.ttls
1137
1603
  i = 0
1138
- while (i < reply.count) {
1139
- r.answerTtls[q][i] = reply.ttls[i]
1604
+ while (i < count && i < toI32(answerTtl.length) && i < toI32(ttls.length)) {
1605
+ answerTtl[i] = ttls[i]
1140
1606
  i += 1
1141
1607
  }
1142
- dnsResolverDone(r, q, reply.count, nowNanos)
1608
+ dnsResolverDone(r, q, count, nowNanos)
1143
1609
  return
1144
1610
  }
1145
1611
  if (dnsResolverSameName(reply.chain, reply.asked)) {
@@ -1147,29 +1613,36 @@ const dnsResolverAnswer = (r: DnsResolver, q: i32, status: i32, nowNanos: i64):
1147
1613
  return
1148
1614
  }
1149
1615
  // The chain left the reply without an address: ask for its end.
1150
- if (r.hops[q] >= DNS_RESOLVER_MAX_CNAME_HOPS || dnsResolverSameName(reply.chain, r.starts[q])) {
1616
+ if (hops[q] >= DNS_RESOLVER_MAX_CNAME_HOPS || dnsResolverSameName(reply.chain, starts[q])) {
1151
1617
  dnsResolverDone(r, q, DNS_RESOLVE_CNAME, nowNanos)
1152
1618
  return
1153
1619
  }
1154
- r.hops[q] = r.hops[q] + 1
1620
+ hops[q] = dnsResolverCount(hops[q])
1155
1621
  r.cnameQueries = dnsResolverCount(r.cnameQueries)
1156
- const length: i32 = dnsResolverNameLength(reply.chain)
1622
+ const chain: u8[] = reply.chain
1623
+ const name: u8[] = names[q]
1624
+ const length: i32 = dnsResolverNameLength(chain)
1625
+ // `chain` and `name` both hold 255 bytes, and the chain is a name dnsReadName read.
1157
1626
  let i: i32 = 0
1158
- while (i < length) {
1159
- r.names[q][i] = reply.chain[i]
1627
+ while (i < length && i < toI32(chain.length) && i < toI32(name.length)) {
1628
+ name[i] = chain[i]
1160
1629
  i += 1
1161
1630
  }
1162
- r.nameLength[q] = length
1631
+ nameLength[q] = length
1163
1632
  dnsResolverAsk(r, q, (q & 1) === 0 ? DNS_TYPE_A : DNS_TYPE_AAAA, nowNanos)
1164
1633
  }
1165
1634
 
1166
1635
  /** The length of the wire name in `name`, its root byte included, read label by label since a label may hold a 0 byte. */
1167
1636
  const dnsResolverNameLength = (name: u8[]): i32 => {
1637
+ // `i < 256`: every name here is held in DNS_MAX_NAME_BYTES (255), so `i < name.length` ends the walk first.
1168
1638
  let i: i32 = 0
1169
- while (i >= 0 && i < toI32(name.length) && name[i] !== toU8(0)) {
1170
- i += toI32(name[i]) + 1
1639
+ let end: i32 = 1
1640
+ while (i >= 0 && i < 256 && i < toI32(name.length) && name[i] !== toU8(0)) {
1641
+ const next: i32 = i + toI32(name[i]) + 1
1642
+ i = next
1643
+ end = next + 1
1171
1644
  }
1172
- return i + 1
1645
+ return end
1173
1646
  }
1174
1647
 
1175
1648
  /** Whether the lowercase wire names `a` and `b` are the same name. */
@@ -1203,21 +1676,46 @@ const dnsResolverSameName = (a: u8[], b: u8[]): boolean => {
1203
1676
  * when no later candidate answers.
1204
1677
  */
1205
1678
  const dnsResolverDone = (r: DnsResolver, q: i32, outcome: i32, nowNanos: i64): void => {
1206
- if (r.state[q] === DNS_Q_UDP || r.state[q] === DNS_Q_TCP) {
1207
- r.pending[q] = DNS_CLOSE
1679
+ const state: i32[] = r.state
1680
+ const pending: i32[] = r.pending
1681
+ const outcomes: i32[] = r.outcome
1682
+ const nxdomain: boolean[] = r.nxdomain
1683
+ const fallback: i32[] = r.fallback
1684
+ const candidate: i32[] = r.candidate
1685
+ // A query is below 2 * maxLookups, the per-query arrays' even length, and
1686
+ // its lookup below maxLookups, the others', so this never returns.
1687
+ if (q < 0 || q >= toI32(state.length) || q >= toI32(pending.length) || q >= toI32(outcomes.length)) {
1688
+ return
1689
+ }
1690
+ if (state[q] === DNS_Q_UDP || state[q] === DNS_Q_TCP) {
1691
+ pending[q] = DNS_CLOSE
1208
1692
  }
1209
- r.state[q] = DNS_Q_DONE
1210
- r.outcome[q] = outcome
1693
+ state[q] = DNS_Q_DONE
1694
+ outcomes[q] = outcome
1211
1695
  const l: i32 = q / 2
1212
1696
  const a: i32 = l * 2
1213
- if (r.state[a] !== DNS_Q_DONE || r.state[a + 1] !== DNS_Q_DONE) {
1697
+ // As above: never taken.
1698
+ if (
1699
+ a >= toI32(state.length) - 1 ||
1700
+ a >= toI32(outcomes.length) - 1 ||
1701
+ a >= toI32(nxdomain.length) - 1 ||
1702
+ l >= toI32(fallback.length) ||
1703
+ l >= toI32(candidate.length)
1704
+ ) {
1214
1705
  return
1215
1706
  }
1216
- const v4: i32 = r.outcome[a]
1217
- const v6: i32 = r.outcome[a + 1]
1707
+ if (state[a] !== DNS_Q_DONE || state[a + 1] !== DNS_Q_DONE) {
1708
+ return
1709
+ }
1710
+ const v4: i32 = outcomes[a]
1711
+ const v6: i32 = outcomes[a + 1]
1218
1712
  if (v4 > 0 || v6 > 0) {
1219
1713
  const four: i32 = v4 > 0 ? v4 : 0
1220
1714
  const six: i32 = v6 > 0 ? v6 : 0
1715
+ // A positive outcome is a reply's address count, at most DNS_MAX_ANSWERS (8): never taken.
1716
+ if (four < 0 || four > 8 || six < 0 || six > 8) {
1717
+ return
1718
+ }
1221
1719
  dnsResolverGather(r, l, four, six, true)
1222
1720
  dnsResolverFinish(r, l, four + six)
1223
1721
  return
@@ -1228,22 +1726,23 @@ const dnsResolverDone = (r: DnsResolver, q: i32, outcome: i32, nowNanos: i64): v
1228
1726
  } else if (v6 === DNS_RESOLVE_NO_ANSWER || v6 === DNS_RESOLVE_CNAME) {
1229
1727
  error = v6
1230
1728
  }
1231
- const named: boolean = r.nxdomain[a] || r.nxdomain[a + 1]
1729
+ const named: boolean = nxdomain[a] || nxdomain[a + 1]
1232
1730
  if (error !== 0 && !named) {
1233
1731
  dnsResolverFinish(r, l, error)
1234
1732
  return
1235
1733
  }
1236
- if (error === 0 && !(r.nxdomain[a] && r.nxdomain[a + 1])) {
1734
+ if (error === 0 && !(nxdomain[a] && nxdomain[a + 1])) {
1237
1735
  // NODATA answers for the name, so an earlier candidate's fallback does not stand.
1238
1736
  dnsResolverFinish(r, l, DNS_RESOLVE_NOT_FOUND)
1239
1737
  return
1240
1738
  }
1241
- if (error !== 0 && r.fallback[l] === 0) {
1242
- r.fallback[l] = error
1739
+ if (error !== 0 && fallback[l] === 0) {
1740
+ fallback[l] = error
1243
1741
  }
1244
- r.candidate[l] = r.candidate[l] + 1
1742
+ candidate[l] = dnsResolverCount(candidate[l])
1245
1743
  if (dnsResolverNextCandidate(r, l, nowNanos)) {
1246
1744
  return
1247
1745
  }
1248
- dnsResolverFinish(r, l, r.fallback[l] !== 0 ? r.fallback[l] : DNS_RESOLVE_NOT_FOUND)
1746
+ const last: i32 = fallback[l]
1747
+ dnsResolverFinish(r, l, last !== 0 ? last : DNS_RESOLVE_NOT_FOUND)
1249
1748
  }