Skip to content

Commit 28e95b3

Browse files
committed
Avoid implicit value truncations in wri()
Abort on truncation of values being written to the ELF data, to prevent silent behavior mismatch.
1 parent 959cd16 commit 28e95b3

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/patchelf.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <memory>
33
#include <optional>
44
#include <set>
5+
#include <stdexcept>
56
#include <string>
67
#include <vector>
78

@@ -157,11 +158,14 @@ class ElfFile
157158
constexpr I rdi(I i) const noexcept;
158159

159160
/* Convert back to the ELF representation. */
160-
template<class I>
161-
constexpr I wri(I & t, unsigned long long i) const
161+
template<class I, class U>
162+
constexpr inline I wri(I & t, U i) const
162163
{
163-
t = rdi((I) i);
164-
return i;
164+
I val = static_cast<I>(i);
165+
if (static_cast<U>(val) != i)
166+
throw std::runtime_error { "value truncation" };
167+
t = rdi(val);
168+
return val;
165169
}
166170

167171
[[nodiscard]] Elf_Ehdr *hdr() noexcept {

0 commit comments

Comments
 (0)