Skip to content

Commit e0c5d2d

Browse files
KristofferCJeffBezanson
authored andcommitted
document that asserts might not be running at certain optimization levels (#25610)
1 parent 23ea201 commit e0c5d2d

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,8 @@ Deprecated or removed
971971
* `rand(t::Tuple{Vararg{Int}})` is deprecated in favor of `rand(Float64, t)` or `rand(t...)`;
972972
`rand(::Tuple)` will have another meaning in the future ([#25429], [#25278]).
973973

974+
* The `assert` function (and `@assert` macro) have been documented that they are not guaranteed to run under various optimization levels and should therefore not be used to e.g. verify passwords.
975+
974976
* `ObjectIdDict` has been deprecated in favor of `IdDict{Any,Any}` ([#25210]).
975977

976978
* `gc` and `gc_enable` have been deprecated in favor of `GC.gc` and `GC.enable` ([#25616]).

base/error.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ systemerror(p, b::Bool; extrainfo=nothing) = b ? throw(Main.Base.SystemError(str
113113
114114
Throw an [`AssertionError`](@ref) if `cond` is `false`.
115115
Also available as the macro [`@assert`](@ref).
116+
117+
!!! warning
118+
An assert might be disabled at various optimization levels.
119+
Assert should therefore only be used as a debugging tool
120+
and not used for authentication verification (e.g. verifying passwords),
121+
nor should side effects needed for the function to work correctly
122+
be used inside of asserts.
116123
"""
117124
assert(x) = x ? nothing : throw(AssertionError())
118125

@@ -122,6 +129,13 @@ assert(x) = x ? nothing : throw(AssertionError())
122129
Throw an [`AssertionError`](@ref) if `cond` is `false`. Preferred syntax for writing assertions.
123130
Message `text` is optionally displayed upon assertion failure.
124131
132+
!!! warning
133+
An assert might be disabled at various optimization levels.
134+
Assert should therefore only be used as a debugging tool
135+
and not used for authentication verification (e.g. verifying passwords),
136+
nor should side effects needed for the function to work correctly
137+
be used inside of asserts.
138+
125139
# Examples
126140
```jldoctest
127141
julia> @assert iseven(3) "3 is an odd number!"

0 commit comments

Comments
 (0)