Skip to content

Commit 4be04f7

Browse files
committed
Do not leak bindings between sides in assert, closes #16181
1 parent 7ef9a91 commit 4be04f7

3 files changed

Lines changed: 83 additions & 11 deletions

File tree

‎lib/elixir/test/elixir/uri_test.exs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ defmodule URITest do
140140

141141
describe "new!/1" do
142142
test "returns the given URI if a %URI{} struct is given" do
143-
assert URI.new!(uri = %URI{scheme: "http", host: "foo.com"}) == uri
143+
uri = %URI{scheme: "http", host: "foo.com"}
144+
assert URI.new!(uri) == uri
144145
end
145146

146147
test "works with HTTP scheme" do
@@ -805,7 +806,8 @@ defmodule URITest do
805806

806807
describe "parse/1" do
807808
test "returns the given URI if a %URI{} struct is given" do
808-
assert URI.parse(uri = %URI{scheme: "http", host: "foo.com"}) == uri
809+
uri = %URI{scheme: "http", host: "foo.com"}
810+
assert URI.parse(uri) == uri
809811
end
810812

811813
test "works with HTTP scheme" do

‎lib/ex_unit/lib/ex_unit/assertions.ex‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,7 @@ defmodule ExUnit.Assertions do
300300
context = if op in [:===, :!==], do: :===, else: :==
301301

302302
quote do
303-
left = unquote(left)
304-
right = unquote(right)
303+
{left, right} = {unquote(left), unquote(right)}
305304
expr = unquote(expr)
306305
message = unquote(message)
307306

@@ -327,8 +326,7 @@ defmodule ExUnit.Assertions do
327326
context = if op in [:===, :!==], do: :===, else: :==
328327

329328
quote do
330-
left = unquote(left)
331-
right = unquote(right)
329+
{left, right} = {unquote(left), unquote(right)}
332330

333331
ExUnit.Assertions.assert(unquote(call),
334332
left: left,
@@ -428,11 +426,29 @@ defmodule ExUnit.Assertions do
428426
end
429427
)
430428

431-
quote do
432-
right = unquote(right)
433-
expr = unquote(code)
434-
{unquote_splicing(vars)} = unquote(match_expr)
435-
right
429+
if pins == [] do
430+
quote do
431+
right = unquote(right)
432+
expr = unquote(code)
433+
{unquote_splicing(vars)} = unquote(match_expr)
434+
right
435+
end
436+
else
437+
# The right side may rebind pinned variables, so we read them
438+
# alongside the right side and restore them within the match
439+
pinned = Enum.map(pins, &elem(&1, 1))
440+
441+
quote do
442+
{right, pinned} = {unquote(right), {unquote_splicing(pinned)}}
443+
expr = unquote(code)
444+
445+
{unquote_splicing(vars)} =
446+
case pinned do
447+
{unquote_splicing(pinned)} -> unquote(match_expr)
448+
end
449+
450+
right
451+
end
436452
end
437453
end
438454

‎lib/ex_unit/test/ex_unit/assertions_test.exs‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,35 @@ defmodule ExUnit.AssertionsTest do
278278
end
279279
end
280280

281+
test "assert match with pinned variable rebound on the right side" do
282+
a = "ab"
283+
284+
"ab" =
285+
assert ^a =
286+
(
287+
a = "cd"
288+
"ab"
289+
)
290+
291+
"cd" = a
292+
293+
try do
294+
assert ^a =
295+
(
296+
a = "ef"
297+
a
298+
)
299+
300+
flunk("This should never be tested")
301+
rescue
302+
error in [ExUnit.AssertionError] ->
303+
"match (=) failed\n" <> "The following variables were pinned:\n" <> " a = \"cd\"" =
304+
error.message
305+
306+
"ef" = error.right
307+
end
308+
end
309+
281310
test "assert match with pinned variable from another context" do
282311
var!(a, Elixir) = 1
283312
{2, 1} = assert {2, ^var!(a, Elixir)} = Value.tuple()
@@ -937,6 +966,31 @@ defmodule ExUnit.AssertionsTest do
937966
1 = error.right
938967
end
939968

969+
test "assert operator does not leak bindings from left to right" do
970+
x = 2
971+
972+
try do
973+
assert (
974+
x = 0
975+
x + 1
976+
) >= x
977+
978+
flunk("This should never be tested")
979+
rescue
980+
error in [ExUnit.AssertionError] ->
981+
1 = error.left
982+
2 = error.right
983+
end
984+
985+
x = 1
986+
987+
false =
988+
refute (
989+
x = 0
990+
x
991+
) == x
992+
end
993+
940994
test "assert operator with expressions" do
941995
greater = 5
942996
true = assert 1 + 2 < greater

0 commit comments

Comments
 (0)