Prevent arbitrary overwriting, deletion or reading of files due to incorrect input sequencing
defmodule RaceCondition do
def process(file) do
Task.async(fn -> File.read!(file) end)
Task.async(fn -> File.rm!(file) end)
end
end
This Elixir code is vulnerable because it does not handle the concurrent operations properly, leading to a race condition where the file might be read and deleted by two different processes at the same time.
defmodule RaceCondition do
def process(file) do
File.read!(file)
File.rm!(file)
end
end
This Elixir code is safe because it ensures that the reading and deletion of a file are performed in a single process, preventing a race condition.