all: misc cleanups

This commit is contained in:
Markus F.X.J. Oberhumer
2024-06-07 22:29:57 +02:00
parent 937ae4c5e0
commit 1aff5f5bb7
10 changed files with 87 additions and 70 deletions
+3 -1
View File
@@ -84,7 +84,9 @@ static_assert((char) (-1) == 255);
#if __cplusplus >= 202002L // C++20
#define upx_is_constant_evaluated std::is_constant_evaluated
#elif __has_builtin(__builtin_is_constant_evaluated) // clang-9, gcc-9
#elif __has_builtin(__builtin_is_constant_evaluated) // clang-9, gcc-10
#define upx_is_constant_evaluated __builtin_is_constant_evaluated
#elif (ACC_CC_GNUC >= 0x090000) && 1 // gcc-9
#define upx_is_constant_evaluated __builtin_is_constant_evaluated
#endif
+4 -4
View File
@@ -152,7 +152,7 @@ void throwCantPack(const char *format, ...) {
char msg[1024];
va_list ap;
va_start(ap, format);
(void) upx_safe_vsnprintf_noexcept(msg, sizeof(msg), format, ap);
upx_safe_vsnprintf_noexcept(msg, sizeof(msg), format, ap);
va_end(ap);
throwCantPack(msg);
}
@@ -162,7 +162,7 @@ void throwCantUnpack(const char *format, ...) {
char msg[1024];
va_list ap;
va_start(ap, format);
(void) upx_safe_vsnprintf_noexcept(msg, sizeof(msg), format, ap);
upx_safe_vsnprintf_noexcept(msg, sizeof(msg), format, ap);
va_end(ap);
throwCantUnpack(msg);
}
@@ -172,9 +172,9 @@ void throwInternalError(const char *format, ...) {
char msg[1024];
va_list ap;
va_start(ap, format);
(void) upx_safe_vsnprintf_noexcept(msg, sizeof(msg), format, ap);
upx_safe_vsnprintf_noexcept(msg, sizeof(msg), format, ap);
va_end(ap);
throwCantUnpack(msg);
throwInternalError(msg);
}
/*************************************************************************
+4 -4
View File
@@ -31,7 +31,7 @@
// ElfLinker
**************************************************************************/
class ElfLinker : private upx::noncopyable {
class ElfLinker /*not_final*/ : private upx::noncopyable {
friend class Packer;
public:
@@ -114,7 +114,7 @@ protected:
const char *type);
};
struct ElfLinker::Section : private upx::noncopyable {
struct ElfLinker::Section final : private upx::noncopyable {
char *name = nullptr;
void *input = nullptr;
byte *output = nullptr;
@@ -128,7 +128,7 @@ struct ElfLinker::Section : private upx::noncopyable {
~Section() noexcept;
};
struct ElfLinker::Symbol : private upx::noncopyable {
struct ElfLinker::Symbol final : private upx::noncopyable {
char *name = nullptr;
Section *section = nullptr;
upx_uint64_t offset = 0;
@@ -137,7 +137,7 @@ struct ElfLinker::Symbol : private upx::noncopyable {
~Symbol() noexcept;
};
struct ElfLinker::Relocation : private upx::noncopyable {
struct ElfLinker::Relocation final : private upx::noncopyable {
const Section *section = nullptr;
unsigned offset = 0;
const char *type = nullptr;
+17 -20
View File
@@ -884,8 +884,9 @@ public:
ACC_COMPILE_TIME_ASSERT(sizeof(C1) == 1) // "char" or "byte"
ACC_COMPILE_TIME_ASSERT(sizeof(C2) == 1) // "char" or "byte"
const Section *s = getThunk((const char *) dll, (const char *) proc, thunk_separator_first);
if (s == nullptr &&
(s = getThunk((const char *) dll, (const char *) proc, thunk_separator)) == nullptr)
if (s == nullptr)
s = getThunk((const char *) dll, (const char *) proc, thunk_separator);
if (s == nullptr)
throwInternalError("entry not found");
return s->offset;
}
@@ -897,7 +898,9 @@ public:
char ord[1 + 5 + 1];
upx_safe_snprintf(ord, sizeof(ord), "%c%05u", ordinal_id, ordinal);
const Section *s = getThunk((const char *) dll, ord, thunk_separator_first);
if (s == nullptr && (s = getThunk((const char *) dll, ord, thunk_separator)) == nullptr)
if (s == nullptr)
s = getThunk((const char *) dll, ord, thunk_separator);
if (s == nullptr)
throwInternalError("entry not found");
return s->offset;
}
@@ -910,12 +913,13 @@ public:
}
template <typename C>
upx_uint64_t hasDll(const C *dll) const {
bool hasDll(const C *dll) const {
ACC_COMPILE_TIME_ASSERT(sizeof(C) == 1) // "char" or "byte"
TStr sdll(name_for_dll((const char *) dll, dll_name_id));
return findSection(sdll, false) != nullptr;
}
};
}; // class PeFile::ImportLinker
/*static*/ const char PeFile::ImportLinker::zeros[sizeof(import_desc)] = {0};
void PeFile::addKernelImport(const char *name) { ilinker->add_import(kernelDll(), name); }
@@ -928,8 +932,7 @@ void PeFile::addStubImports() {
addKernelImport("VirtualProtect");
}
void PeFile::processImports2(unsigned myimport, unsigned) // pass 2
{
void PeFile::processImports2(unsigned myimport, unsigned) { // pass 2
COMPILE_TIME_ASSERT(sizeof(import_desc) == 20)
if (ilinker == nullptr)
return;
@@ -941,8 +944,7 @@ void PeFile::processImports2(unsigned myimport, unsigned) // pass 2
}
template <typename LEXX, typename ord_mask_t>
unsigned PeFile::processImports0(ord_mask_t ord_mask) // pass 1
{
unsigned PeFile::processImports0(ord_mask_t ord_mask) { // pass 1
if (isefi) {
if (IDSIZE(PEDIR_IMPORT))
throwCantPack("imports not supported on EFI");
@@ -1354,8 +1356,7 @@ struct PeFile::tls_traits<LE64> final {
template <typename LEXX>
void PeFile::processTls1(Interval *iv, typename tls_traits<LEXX>::cb_value_t imagebase,
unsigned imagesize) // pass 1
{
unsigned imagesize) { // pass 1
typedef typename tls_traits<LEXX>::tls tls;
typedef typename tls_traits<LEXX>::cb_value_t cb_value_t;
constexpr unsigned cb_size = tls_traits<LEXX>::cb_size;
@@ -1441,8 +1442,7 @@ void PeFile::processTls1(Interval *iv, typename tls_traits<LEXX>::cb_value_t ima
template <typename LEXX>
void PeFile::processTls2(Reloc *const rel, const Interval *const iv, unsigned newaddr,
typename tls_traits<LEXX>::cb_value_t imagebase) // pass 2
{
typename tls_traits<LEXX>::cb_value_t imagebase) { // pass 2
typedef typename tls_traits<LEXX>::tls tls;
typedef typename tls_traits<LEXX>::cb_value_t cb_value_t;
constexpr unsigned cb_size = tls_traits<LEXX>::cb_size;
@@ -1501,8 +1501,7 @@ void PeFile::processTls2(Reloc *const rel, const Interval *const iv, unsigned ne
// Load Configuration handling
**************************************************************************/
void PeFile::processLoadConf(Interval *iv) // pass 1
{
void PeFile::processLoadConf(Interval *iv) { // pass 1
if (IDSIZE(PEDIR_LOAD_CONFIG) == 0)
return;
@@ -2350,7 +2349,7 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh, unsigned subsystem_mask,
unsigned newvsize = (isection[objs - 1].vaddr + isection[objs - 1].vsize + oam1) & ~oam1;
NO_fprintf(stderr, "newvsize=%x objs=%d\n", newvsize, objs);
if (newvsize + soimport + sorelocs > ibuf.getSize())
if ((upx_uint64_t) newvsize + soimport + sorelocs > ibuf.getSize())
throwInternalError("buffer too small 2");
memcpy(ibuf + newvsize, oimport, soimport);
memcpy(ibuf + newvsize + soimport, orelocs, sorelocs);
@@ -3144,8 +3143,7 @@ tribool PeFile32::canUnpack() {
return canUnpack0(getFormat() == UPX_F_WINCE_ARM ? 4 : 3, ih.objects, ih.entry, sizeof(ih));
}
unsigned PeFile32::processImports() // pass 1
{
unsigned PeFile32::processImports() { // pass 1
return processImports0<LE32>(1u << 31);
}
@@ -3195,8 +3193,7 @@ tribool PeFile64::canUnpack() {
return canUnpack0(3, ih.objects, ih.entry, sizeof(ih));
}
unsigned PeFile64::processImports() // pass 1
{
unsigned PeFile64::processImports() { // pass 1
return processImports0<LE64>(1ULL << 63);
}
+2 -2
View File
@@ -743,7 +743,7 @@ inline void owner_delete(OwningPointer(T)(&object)) noexcept {
static_assert(std::is_class_v<T>); // UPX convention
static_assert(std::is_nothrow_destructible_v<T>);
if (object != nullptr) {
delete (T *) object;
delete (T *) object; // single object delete
object = nullptr;
}
assert_noexcept((T *) object == nullptr);
@@ -754,7 +754,7 @@ template <class T>
inline void owner_free(OwningPointer(T)(&object)) noexcept {
static_assert(!std::is_class_v<T>); // UPX convention
if (object != nullptr) {
::free((T *) object);
::free((T *) object); // free memory from malloc()
object = nullptr;
}
assert_noexcept((T *) object == nullptr);
+2 -2
View File
@@ -146,7 +146,7 @@ void MemBuffer::fill(unsigned off, unsigned len, int value) {
**************************************************************************/
// for use_simple_mcheck()
#define PTR_BITS32(p) ((unsigned) (ptr_get_address(p) & 0xffffffff))
#define PTR_BITS32(p) ((upx_uint32_t) (ptr_get_address(p) & 0xffffffff))
#define MAGIC1(p) ((PTR_BITS32(p) ^ 0xfefdbeeb) | 1)
#define MAGIC2(p) ((PTR_BITS32(p) ^ 0xfefdbeeb ^ 0x88224411) | 1)
@@ -279,7 +279,7 @@ TEST_CASE("MemBuffer core") {
CHECK_THROWS(mb.subref("", N, 1));
if (use_simple_mcheck()) {
byte *p = raw_bytes(mb, 0);
unsigned magic1 = get_ne32(p - 4);
upx_uint32_t magic1 = get_ne32(p - 4);
set_ne32(p - 4, magic1 ^ 1);
CHECK_THROWS(mb.checkState());
set_ne32(p - 4, magic1);