mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Update bounds-checking note for overflow-checking
This commit is contained in:
1 parent
5ad98921e7
commit
89bbe30115
2 files changed
+31
-16
No files matched your search
+4
-2
@@ -11,8 +11,10 @@ where it left off, in the middle of the function body. See the
|
||||
A type that depends on another value. For example, a variable `n`'s type might
|
||||
be "the length of `s`", for some other
|
||||
[slice](/doc/note/slices-arrays-and-tables.md)-typed variable `s`. Dependent
|
||||
types are *a* way to implement [bounds checking](/doc/note/bounds-checking.md),
|
||||
but they're not the only way. Wuffs does not use them.
|
||||
types are *a* way to implement compile-time [bounds
|
||||
checking](/doc/note/bounds-checking.md), but they're not the only way, and
|
||||
there's more to programming languages than their type systems. Wuffs does not
|
||||
use dependent types.
|
||||
|
||||
#### Effect
|
||||
|
||||
|
||||
+27
-14
@@ -7,26 +7,39 @@ example, the expression `x[..]` is always valid because the implicit `i` and
|
||||
length.
|
||||
|
||||
Similarly, an expression like `x[i]` is invalid unless there is a compile-time
|
||||
proof that `i` is in bounds: that `((0 <= i) and (i < x.length()))`. Proofs can
|
||||
proof that `i` is in-bounds: that `((0 <= i) and (i < x.length()))`. Proofs can
|
||||
involve natural bounds (e.g. if `i` has type `base.u8` then `(0 <= i)` is
|
||||
trivially true since a `base.u8` is unsigned),
|
||||
[refinements](/doc/glossary.md#refinement-type) (e.g. if `i`has type
|
||||
`base.u32[..= 100]` then 100 is an upper bound for `i`) and [interval
|
||||
arithmetic](/doc/note/interval-arithmetic.md) (e.g. for an expression like
|
||||
`x[m + n]`, the upper bound for `m + n` is the sum of the upper bounds of `m`
|
||||
and `n`).
|
||||
`base.u32[..= 100]` then 100 is an inclusive upper bound for `i`),
|
||||
[facts](/doc/note/facts.md) (e.g. an explicit prior check `if i < x.length()`)
|
||||
and [interval arithmetic](/doc/note/interval-arithmetic.md) (e.g. for an
|
||||
expression like `x[m + n]`, the upper bound for `m + n` is the sum of the upper
|
||||
bounds of `m` and `n`).
|
||||
|
||||
For example, if `a` is an `array[1024] base.u8`, and `expr` is some expression
|
||||
of type `base.u32`, then `a[expr & 1023]` is valid, because the bitwise-and
|
||||
of type `base.u32`, then `a[expr & 1023]` is valid (in-bounds). The bitwise-and
|
||||
means that the overall index expression is in the range `[0 ..= 1023]`,
|
||||
regardless of `expr`'s range.
|
||||
|
||||
Proofs can also involve [facts](/doc/note/facts.md), dynamic constraints that
|
||||
are not confined to the type system. For example, when the if-true branch of an
|
||||
`if i < j { etc }` statement is taken, that `(i < j)` fact could combine with
|
||||
another `(j <= x.length())` fact to deduce that `(i < x.length())` when inside
|
||||
that if-true branch (but not necessarily outside of that if statement).
|
||||
|
||||
Wuffs does not have dependent types. Dependent types are one way to enforce
|
||||
bounds checking, but they're not the only way, and there's more to programming
|
||||
languages than their type systems.
|
||||
## Overflow Checking
|
||||
|
||||
Arithmetic overflow checking uses the same mechanisms as bounds checking. For
|
||||
example, if `m` and `n` both have type `base.u8` (or a refinement thereof) then
|
||||
the expression `m + n` also has type `base.u8`. Proving that `m + n` evaluates
|
||||
to something inside the `base.u8` range, `[0 ..= 255]`, is equivalent to
|
||||
proving that the expression `h[m + n]` is valid (in-bounds) for a hypothetical
|
||||
`h` array of length `256`.
|
||||
|
||||
|
||||
## `nullptr` Checking
|
||||
|
||||
Guarding against `nullptr` dereferences is another case of bounds checking.
|
||||
Like other programming languages, `nptr T` and `ptr T` are types: nullable and
|
||||
non-null pointers to some type `T`. In Wuffs, the latter is effectively a
|
||||
refinement. Assuming that `nullptr` is equivalent to `0`, `ptr T` is
|
||||
conceptually just `(nptr T)[1 ..= UINTPTR_MAX]`, although that's not actually
|
||||
valid syntax. Similarly, if `expr` is a pointer-typed expression then checking
|
||||
that `expr` is not `nullptr` is equivalent to checking that `expr` is in the
|
||||
range `[1 ..= UINTPTR_MAX]`.
|
||||
Reference in new issue
Block a user