all: minor cleanups

This commit is contained in:
Markus F.X.J. Oberhumer
2024-06-14 15:15:55 +02:00
parent 1aff5f5bb7
commit 3c294d9721
14 changed files with 91 additions and 62 deletions
+2 -5
View File
@@ -480,12 +480,9 @@ inline bele_constexpr upx_int64_t get_le64_signed(const XE64 *p) noexcept {
/*************************************************************************
// classes for portable unaligned access
//
// Important: these classes must be PODs (Plain Old Data), i.e. no
// Note: these classes must be PODs (Plain Old Data), i.e. no
// constructor, no destructor, no virtual functions and no default
// assignment operator, and all fields must be public(!).
//
// [Actually we _can_ use a safe non-POD subset, but for this we need
// to have gcc bug 17519 fixed - see http://gcc.gnu.org/PR17519 ]
// assignment operator, and all fields must be public
**************************************************************************/
struct alignas(1) BE16 final {
+8 -9
View File
@@ -52,8 +52,8 @@ static_assert(sizeof(short) == 2);
static_assert(sizeof(int) == 4);
static_assert(sizeof(long long) == 8);
// check sane compiler mandatory flags
static_assert(-1 == ~0); // two's complement - see http://wg21.link/P0907R4
static_assert(0u - 1 == ~0u); // two's complement - see http://wg21.link/P0907R4
static_assert(-1 == ~0); // two's complement - see https://wg21.link/P0907R4
static_assert(0u - 1 == ~0u); // two's complement - see https://wg21.link/P0907R4
static_assert((1u << 31) << 1 == 0);
static_assert(((int) (1u << 31)) >> 31 == -1); // arithmetic right shift
static_assert((-1) >> 31 == -1); // arithmetic right shift
@@ -468,13 +468,12 @@ noreturn void throwAssertFailed(const char *expr, const char *file, int line, co
// C++ support library
#include "util/cxxlib.h"
using upx::tribool;
#define usizeof(expr) (upx::UnsignedSizeOf<sizeof(expr)>::value)
#define ALIGN_DOWN(a, b) upx::align_down((a), (b))
#define ALIGN_UP(a, b) upx::align_up((a), (b))
#define ALIGN_GAP(a, b) upx::align_gap((a), (b))
#define UPX_MAX(a, b) upx::max((a), (b))
#define UPX_MIN(a, b) upx::min((a), (b))
#define usizeof(expr) (upx::UnsignedSizeOf<sizeof(expr)>::value)
#define ALIGN_DOWN(a, b) (upx::align_down((a), (b)))
#define ALIGN_UP(a, b) (upx::align_up((a), (b)))
#define ALIGN_GAP(a, b) (upx::align_gap((a), (b)))
#define UPX_MAX(a, b) (upx::max((a), (b)))
#define UPX_MIN(a, b) (upx::min((a), (b)))
/*************************************************************************
// constants
+1 -1
View File
@@ -227,7 +227,7 @@ void ElfLinker::preprocessSymbols(char *start, char const *end) {
else if (sscanf(start, "%x%*8c %1023s %*x %1023s", &offset, section, symbol) == 3)
#else
// work around broken scanf() implementations
// http://bugs.winehq.org/show_bug.cgi?id=10401 (fixed in Wine 0.9.58)
// https://bugs.winehq.org/show_bug.cgi?id=10401 (fixed in Wine 0.9.58)
else if (sscanf(start, "%x%*c%*c%*c%*c%*c%*c%*c%*c %1023s %*x %1023s", &offset, section,
symbol) == 3)
#endif
+1 -1
View File
@@ -39,7 +39,7 @@
PackerBase::PackerBase(InputFile *f)
: fi(f), file_size(f != nullptr ? f->st_size() : 0), file_size_i32(file_size) {
ph.reset();
mem_size_assert(1, file_size_u);
mem_size_assert(1, file_size_u); // limited by UPX_RSIZE_MAX
assert_noexcept(file_size_i32 == file_size);
assert_noexcept(file_size_u32 == file_size_u);
}
+2 -1
View File
@@ -82,7 +82,7 @@ public:
protected:
InputFile *const fi; // reference
// multiple names for "file_size" to avoid casts
// multiple names for "file_size" to avoid casts; limited by UPX_RSIZE_MAX
union { // unnamed union
const upx_int64_t file_size; // must get set by constructor
const upx_uint64_t file_size_u;
@@ -275,6 +275,7 @@ protected:
template <class T>
static inline constexpr bool is_te64_type =
upx::is_same_any_v<T, byte, upx_uint64_t, BE64, LE64>;
template <class T>
using enable_if_te16 = std::enable_if_t<is_te16_type<T>, T>;
template <class T>
+4 -4
View File
@@ -37,7 +37,7 @@
template <class T>
inline
typename std::enable_if<std::is_pointer<T>::value && !upx::is_bounded_array<T>::value, T>::type
raw_bytes(T ptr, size_t size_in_bytes) {
raw_bytes(T ptr, size_t size_in_bytes) may_throw {
if (size_in_bytes > 0) {
if very_unlikely (ptr == nullptr)
throwCantPack("raw_bytes unexpected NULL ptr");
@@ -53,7 +53,7 @@ template <class T>
inline typename std::enable_if<std::is_pointer<T>::value && !upx::is_bounded_array<T>::value &&
!std::is_void<typename std::remove_pointer<T>::type>::value,
T>::type
raw_index_bytes(T ptr, size_t index, size_t size_in_bytes) {
raw_index_bytes(T ptr, size_t index, size_t size_in_bytes) may_throw {
typedef typename std::remove_pointer<T>::type element_type;
if very_unlikely (ptr == nullptr)
throwCantPack("raw_index_bytes unexpected NULL ptr");
@@ -66,7 +66,7 @@ raw_index_bytes(T ptr, size_t index, size_t size_in_bytes) {
// same for bounded arrays
template <class T, size_t N>
inline T *raw_bytes(T (&array)[N], size_t size_in_bytes) {
inline T *raw_bytes(T (&array)[N], size_t size_in_bytes) may_throw {
typedef T element_type;
if very_unlikely (size_in_bytes > mem_size(sizeof(element_type), N))
throwCantPack("raw_bytes out of range");
@@ -74,7 +74,7 @@ inline T *raw_bytes(T (&array)[N], size_t size_in_bytes) {
}
template <class T, size_t N>
inline T *raw_index_bytes(T (&array)[N], size_t index, size_t size_in_bytes) {
inline T *raw_index_bytes(T (&array)[N], size_t index, size_t size_in_bytes) may_throw {
typedef T element_type;
return raw_bytes(array, mem_size(sizeof(element_type), index, size_in_bytes)) + index;
}
+33 -29
View File
@@ -31,25 +31,25 @@
// UPX version of string functions, with assertions and sane limits
**************************************************************************/
upx_rsize_t upx_safe_strlen(const char *) may_throw;
// info: snprintf() returns length and NOT size, but max_size is indeed size (incl NUL)
int upx_safe_vsnprintf(char *str, upx_rsize_t max_size, const char *format, va_list ap);
int upx_safe_vsnprintf(char *str, upx_rsize_t max_size, const char *format, va_list ap) may_throw;
int upx_safe_snprintf(char *str, upx_rsize_t max_size, const char *format, ...)
attribute_format(3, 4);
may_throw attribute_format(3, 4);
// malloc's *ptr
int upx_safe_vasprintf(char **ptr, const char *format, va_list ap);
int upx_safe_asprintf(char **ptr, const char *format, ...) attribute_format(2, 3);
int upx_safe_vasprintf(char **ptr, const char *format, va_list ap) may_throw;
int upx_safe_asprintf(char **ptr, const char *format, ...) may_throw attribute_format(2, 3);
// returns a malloc'd pointer
char *upx_safe_xprintf(const char *format, ...) attribute_format(1, 2);
upx_rsize_t upx_safe_strlen(const char *);
char *upx_safe_xprintf(const char *format, ...) may_throw attribute_format(1, 2);
// noexcept variants (these use "assert_noexcept")
upx_rsize_t upx_safe_strlen_noexcept(const char *) noexcept;
int upx_safe_vsnprintf_noexcept(char *str, upx_rsize_t max_size, const char *format,
va_list ap) noexcept;
upx_rsize_t upx_safe_strlen_noexcept(const char *) noexcept;
// globally redirect some functions
#undef strlen
@@ -66,28 +66,32 @@ upx_rsize_t upx_safe_strlen_noexcept(const char *) noexcept;
// some uchar string support functions to avoid casts
**************************************************************************/
forceinline uchar *strcpy(uchar *s1, const uchar *s2) {
return (uchar *) strcpy((char *) s1, (const char *) s2);
}
forceinline int strcmp(const uchar *s1, const char *s2) { return strcmp((const char *) s1, s2); }
forceinline int strcmp(const char *s1, const uchar *s2) { return strcmp(s1, (const char *) s2); }
forceinline int strcmp(const uchar *s1, const uchar *s2) {
return strcmp((const char *) s1, (const char *) s2);
}
forceinline int strcasecmp(const uchar *s1, const char *s2) {
return strcasecmp((const char *) s1, s2);
}
forceinline int strcasecmp(const char *s1, const uchar *s2) {
return strcasecmp(s1, (const char *) s2);
}
forceinline int strcasecmp(const uchar *s1, const uchar *s2) {
return strcasecmp((const char *) s1, (const char *) s2);
}
forceinline upx_rsize_t upx_safe_strlen(const uchar *s) {
forceinline upx_rsize_t upx_safe_strlen(const uchar *s) may_throw {
return upx_safe_strlen((const char *) s);
}
forceinline uchar *strcpy(uchar *s1, const uchar *s2) noexcept {
return (uchar *) strcpy((char *) s1, (const char *) s2);
}
forceinline int strcmp(const uchar *s1, const char *s2) noexcept {
return strcmp((const char *) s1, s2);
}
forceinline int strcmp(const char *s1, const uchar *s2) noexcept {
return strcmp(s1, (const char *) s2);
}
forceinline int strcmp(const uchar *s1, const uchar *s2) noexcept {
return strcmp((const char *) s1, (const char *) s2);
}
forceinline int strcasecmp(const uchar *s1, const char *s2) noexcept {
return strcasecmp((const char *) s1, s2);
}
forceinline int strcasecmp(const char *s1, const uchar *s2) noexcept {
return strcasecmp(s1, (const char *) s2);
}
forceinline int strcasecmp(const uchar *s1, const uchar *s2) noexcept {
return strcasecmp((const char *) s1, (const char *) s2);
}
/* vim:set ts=4 sw=4 et: */