summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Göttsche <cgzones@googlemail.com>2023-02-21 19:41:06 +0100
committerChristian Göttsche <cgzones@googlemail.com>2023-02-21 19:41:06 +0100
commit28e95b30f3e7f1920a51a43ae275ec6f3508b495 (patch)
treec51bfcc547354ec83d34653bd6edfcbe8c922241
parent959cd168db3860af6d63892d5cf6836d55bf2ced (diff)
downloadpatchelf-28e95b30f3e7f1920a51a43ae275ec6f3508b495.tar.gz
Avoid implicit value truncations in wri()
Abort on truncation of values being written to the ELF data, to prevent silent behavior mismatch.
-rw-r--r--src/patchelf.h12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/patchelf.h b/src/patchelf.h
index 7c1165b..101b1c9 100644
--- a/src/patchelf.h
+++ b/src/patchelf.h
@@ -2,6 +2,7 @@
#include <memory>
#include <optional>
#include <set>
+#include <stdexcept>
#include <string>
#include <vector>
@@ -157,11 +158,14 @@ private:
constexpr I rdi(I i) const noexcept;
/* Convert back to the ELF representation. */
- template<class I>
- constexpr I wri(I & t, unsigned long long i) const
+ template<class I, class U>
+ constexpr inline I wri(I & t, U i) const
{
- t = rdi((I) i);
- return i;
+ I val = static_cast<I>(i);
+ if (static_cast<U>(val) != i)
+ throw std::runtime_error { "value truncation" };
+ t = rdi(val);
+ return val;
}
[[nodiscard]] Elf_Ehdr *hdr() noexcept {