File 1673-erts-Fix-iovec-calculation.patch of Package erlang
From 1369f394a7b6e656f21a9eb1cfa7da36c29fae94 Mon Sep 17 00:00:00 2001
From: Lukas Larsson <lukas@erlang.org>
Date: Fri, 26 Aug 2022 09:39:21 +0200
Subject: [PATCH 2/2] erts: Fix iovec calculation
When the binary being encoded is was exactly the same size
as the maximum iovec segment the number of iovec_len would
end up being off by 1 which could lead to a segfault when
writing the buffers.
---
erts/emulator/beam/erl_nif.c | 2 +-
lib/kernel/test/file_SUITE.erl | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/erts/emulator/beam/erl_nif.c b/erts/emulator/beam/erl_nif.c
index 3f03243747..908f67e9a4 100644
--- a/erts/emulator/beam/erl_nif.c
+++ b/erts/emulator/beam/erl_nif.c
@@ -3736,7 +3736,7 @@ static int examine_iovec_term(Eterm list, UWord max_length, iovec_slice_t *resul
result->referenced_size += byte_size;
}
- result->iovec_len += 1 + byte_size / MAX_SYSIOVEC_IOVLEN;
+ result->iovec_len += (MAX_SYSIOVEC_IOVLEN - 1 + byte_size) / MAX_SYSIOVEC_IOVLEN;
}
result->sublist_length += 1;
diff --git a/lib/kernel/test/file_SUITE.erl b/lib/kernel/test/file_SUITE.erl
index dc9d0ebb2c..46ba3bfcc6 100644
--- a/lib/kernel/test/file_SUITE.erl
+++ b/lib/kernel/test/file_SUITE.erl
@@ -4130,6 +4130,10 @@ do_large_write(Name) ->
Bin = <<0:Size/unit:8>>,
ok = file:write_file(Name, Bin),
{ok,#file_info{size=Size}} = file:read_file_info(Name),
+
+ %% Even multiples of MAX_SYSIOVEC_IOVLEN would cause a crash
+ ok = file:write_file(Name, binary:part(Bin, 0, 1 bsl 31)),
+ {ok,#file_info{size=1 bsl 31}} = file:read_file_info(Name),
ok
end.
--
2.35.3