From 3765ca4e92db3b53b1558a3cf03325105e52ea8e Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Wed, 21 Sep 2016 21:33:53 +0200 Subject: [PATCH] Cleanups and cruft removal. --- src/Makefile | 1 + src/bele.h | 69 ++++++----------------------------- src/bele_policy.h | 6 +--- src/conf.h | 2 +- src/mem.cpp | 19 ++++++++++ src/mem.h | 7 ---- src/p_lx_elf.cpp | 5 +-- src/p_mach.h | 11 ------ src/snprintf.cpp | 11 +++--- src/util.cpp | 6 +--- src/util.h | 92 +++++++++++++++-------------------------------- src/work.cpp | 2 +- 12 files changed, 67 insertions(+), 164 deletions(-) diff --git a/src/Makefile b/src/Makefile index ba4c4b11..cc7557d1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -132,6 +132,7 @@ endif CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h CLANG_FORMAT_FILES += snprintf.cpp CLANG_FORMAT_FILES += stdcxx.h stdcxx.cpp +CLANG_FORMAT_FILES += util.h clang-format: $(top_srcdir)/src/stub/scripts/upx-clang-format -i $(addprefix $(top_srcdir)/src/,$(CLANG_FORMAT_FILES)) .PHONY: clang-format diff --git a/src/bele.h b/src/bele.h index 3a2fd86f..459adb20 100644 --- a/src/bele.h +++ b/src/bele.h @@ -314,9 +314,6 @@ inline void acc_ua_swab32s(void *p) __packed_struct(BE16) unsigned char d[2]; - //inline BE16() { } - //BE16(unsigned v) { set_be16(d, v); } - BE16& operator = (unsigned v) { set_be16(d, v); return *this; } BE16& operator += (unsigned v) { set_be16(d, get_be16(d) + v); return *this; } BE16& operator -= (unsigned v) { set_be16(d, get_be16(d) - v); return *this; } @@ -335,9 +332,6 @@ __packed_struct_end() __packed_struct(BE32) unsigned char d[4]; - //inline BE32() { } - //BE32(unsigned v) { set_be32(d, v); } - BE32& operator = (unsigned v) { set_be32(d, v); return *this; } BE32& operator += (unsigned v) { set_be32(d, get_be32(d) + v); return *this; } BE32& operator -= (unsigned v) { set_be32(d, get_be32(d) - v); return *this; } @@ -356,9 +350,6 @@ __packed_struct_end() __packed_struct(BE64) unsigned char d[8]; - //inline BE64() { } - //BE64(upx_uint64_t v) { set_be64(d, v); } - BE64& operator = (upx_uint64_t v) { set_be64(d, v); return *this; } BE64& operator += (upx_uint64_t v) { set_be64(d, get_be64(d) + v); return *this; } BE64& operator -= (upx_uint64_t v) { set_be64(d, get_be64(d) - v); return *this; } @@ -377,9 +368,6 @@ __packed_struct_end() __packed_struct(LE16) unsigned char d[2]; - //inline LE16() { } - //LE16(unsigned v) { set_le16(d, v); } - LE16& operator = (unsigned v) { set_le16(d, v); return *this; } LE16& operator += (unsigned v) { set_le16(d, get_le16(d) + v); return *this; } LE16& operator -= (unsigned v) { set_le16(d, get_le16(d) - v); return *this; } @@ -419,9 +407,6 @@ __packed_struct_end() __packed_struct(LE64) unsigned char d[8]; - //inline LE64() { } - //LE64(upx_uint64_t v) { set_le64(d, v); } - LE64& operator = (upx_uint64_t v) { set_le64(d, v); return *this; } LE64& operator += (upx_uint64_t v) { set_le64(d, get_le64(d) + v); return *this; } LE64& operator -= (upx_uint64_t v) { set_le64(d, get_le64(d) - v); return *this; } @@ -484,12 +469,16 @@ template T* operator - (T* ptr, const LE64& v); // global overloads **************************************************************************/ +inline unsigned ALIGN_DOWN(unsigned a, const BE32& b) { return ALIGN_DOWN(a, (unsigned) b); } +inline unsigned ALIGN_DOWN(const BE32& a, unsigned b) { return ALIGN_DOWN((unsigned) a, b); } +inline unsigned ALIGN_UP (unsigned a, const BE32& b) { return ALIGN_UP (a, (unsigned) b); } +inline unsigned ALIGN_UP (const BE32& a, unsigned b) { return ALIGN_UP ((unsigned) a, b); } + inline unsigned ALIGN_DOWN(unsigned a, const LE32& b) { return ALIGN_DOWN(a, (unsigned) b); } inline unsigned ALIGN_DOWN(const LE32& a, unsigned b) { return ALIGN_DOWN((unsigned) a, b); } inline unsigned ALIGN_UP (unsigned a, const LE32& b) { return ALIGN_UP (a, (unsigned) b); } inline unsigned ALIGN_UP (const LE32& a, unsigned b) { return ALIGN_UP ((unsigned) a, b); } - inline unsigned UPX_MAX(unsigned a, const BE16& b) { return UPX_MAX(a, (unsigned) b); } inline unsigned UPX_MAX(const BE16& a, unsigned b) { return UPX_MAX((unsigned) a, b); } inline unsigned UPX_MIN(unsigned a, const BE16& b) { return UPX_MIN(a, (unsigned) b); } @@ -536,42 +525,6 @@ int __acc_cdecl_qsort le64_compare_signed(const void *, const void *); } // extern "C" -// just for testing... -#if !(ACC_CFG_NO_UNALIGNED) -#if 0 && (ACC_ARCH_AMD64 || ACC_ARCH_I386) && (ACC_CC_GNUC >= 0x030200) - typedef upx_uint16_t LE16_unaligned __attribute__((__aligned__(1))); - typedef upx_uint32_t LE32_unaligned __attribute__((__aligned__(1))); -# ifndef LE16 -# define LE16 LE16_unaligned -# endif -# ifndef LE32 -# define LE32 LE32_unaligned -# endif -#endif -#if 0 && (ACC_ARCH_I386) && (ACC_CC_INTELC) - typedef __declspec(align(1)) upx_uint16_t LE16_unaligned; - typedef __declspec(align(1)) upx_uint32_t LE32_unaligned; -# ifndef LE16 -# define LE16 LE16_unaligned -# endif -# ifndef LE32 -# define LE32 LE32_unaligned -# endif -#endif -#if 0 && (ACC_ARCH_AMD64 || ACC_ARCH_I386) && (ACC_CC_MSC) && (_MSC_VER >= 1200) - typedef __declspec(align(1)) upx_uint16_t LE16_unaligned; - typedef __declspec(align(1)) upx_uint32_t LE32_unaligned; -# ifndef LE16 -# define LE16 LE16_unaligned -# endif -# ifndef LE32 -# define LE32 LE32_unaligned -# endif -# pragma warning(disable: 4244) // Wx: conversion, possible loss of data -#endif -#endif - - /************************************************************************* // Provide namespaces and classes to abstract endianness policies. // @@ -581,13 +534,15 @@ int __acc_cdecl_qsort le64_compare_signed(const void *, const void *); // forward declarations namespace N_BELE_CTP { -struct BEPolicy; struct LEPolicy; +struct BEPolicy; +struct LEPolicy; extern const BEPolicy be_policy; extern const LEPolicy le_policy; } namespace N_BELE_RTP { struct AbstractPolicy; -struct BEPolicy; struct LEPolicy; +struct BEPolicy; +struct LEPolicy; extern const BEPolicy be_policy; extern const LEPolicy le_policy; } @@ -614,8 +569,4 @@ inline const N_BELE_RTP::AbstractPolicy* getRTP(const LEPolicy*) #endif /* already included */ - -/* -vi:ts=4:et:nowrap -*/ - +/* vim:set ts=4 sw=4 et: */ diff --git a/src/bele_policy.h b/src/bele_policy.h index aee15c64..2ac05a7a 100644 --- a/src/bele_policy.h +++ b/src/bele_policy.h @@ -276,8 +276,4 @@ struct HostAlignedPolicy #undef S #undef C - -/* -vi:ts=4:et:nowrap -*/ - +/* vim:set ts=4 sw=4 et: */ diff --git a/src/conf.h b/src/conf.h index f3be97fb..f3a1d0cb 100644 --- a/src/conf.h +++ b/src/conf.h @@ -49,7 +49,7 @@ #endif // FIXME - quick hack for arm-wince-gcc-3.4 (Debian pocketpc-*.deb packages) -#if 1 && (ACC_ARCH_ARM) && defined(__pe__) && !defined(_WIN32) +#if 1 && (ACC_ARCH_ARM) && defined(__pe__) && !defined(__CEGCC__) && !defined(_WIN32) # undef HAVE_CHMOD # undef HAVE_CHOWN # undef HAVE_LSTAT diff --git a/src/mem.cpp b/src/mem.cpp index 2322968c..49523fcb 100644 --- a/src/mem.cpp +++ b/src/mem.cpp @@ -67,6 +67,25 @@ bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extr return true; } +bool mem_size_valid_bytes(upx_uint64_t bytes) +{ + if (bytes > MAX_BUF_SIZE) return false; + return true; +} + + +int ptr_diff(const char *p1, const char *p2) +{ + assert(p1 != NULL); + assert(p2 != NULL); + ptrdiff_t d = p1 - p2; + if (p1 >= p2) + assert(mem_size_valid_bytes(d)); + else + assert(mem_size_valid_bytes(-d)); + return ACC_ICONV(int, d); +} + #undef MAX_BUF_SIZE diff --git a/src/mem.h b/src/mem.h index f21215ad..59d5a387 100644 --- a/src/mem.h +++ b/src/mem.h @@ -34,13 +34,6 @@ // **************************************************************************/ -size_t mem_size (upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra=0); -size_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n); -bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra=0); - -#define New(type,n) new type [ mem_size_get_n(sizeof(type),n) ] - - class MemBuffer { public: diff --git a/src/p_lx_elf.cpp b/src/p_lx_elf.cpp index 357fc445..75dfd78c 100644 --- a/src/p_lx_elf.cpp +++ b/src/p_lx_elf.cpp @@ -2126,7 +2126,7 @@ PackNetBSDElf32x86::generateElfHdr( set_te32(&phdr->p_align, 4); /* &np_PaX->body[4] */ - const unsigned char *p4 = &(ACC_STATIC_CAST2(const unsigned char *, const void *, (1+ np_PaX)))[4]; + const unsigned char *p4 = &(ACC_CCAST(const unsigned char *, (1+ np_PaX)))[4]; unsigned bits = get_te32(p4); bits &= ~PAX_MPROTECT; bits |= PAX_NOMPROTECT; @@ -3746,10 +3746,7 @@ void PackLinuxElf32::unpack(OutputFile *fo) #define MAX_ELF_HDR 512 union { unsigned char buf[MAX_ELF_HDR]; -#if (ACC_CC_BORLANDC || ACC_CC_SUNPROC) -#else struct { Elf32_Ehdr ehdr; Elf32_Phdr phdr; } e; -#endif } u; COMPILE_TIME_ASSERT(sizeof(u) == MAX_ELF_HDR) Elf32_Ehdr *const ehdr = (Elf32_Ehdr *) u.buf; diff --git a/src/p_mach.h b/src/p_mach.h index e7ecb7c1..359ccf54 100644 --- a/src/p_mach.h +++ b/src/p_mach.h @@ -494,11 +494,7 @@ struct MachClass_32 typedef typename TP::U32 TE32; typedef typename TP::U64 TE64; typedef N_Mach::MachITypes MachITypes; -#if (ACC_CC_BORLANDC) - typedef TE32 Addr; -#else typedef typename MachITypes::Addr Addr; -#endif // Mach types typedef N_Mach::Mach_header Mach_header; @@ -532,11 +528,7 @@ struct MachClass_64 typedef typename TP::U32 TE32; typedef typename TP::U64 TE64; typedef N_Mach::MachITypes MachITypes; -#if (ACC_CC_BORLANDC) - typedef TE64 Addr; -#else typedef typename MachITypes::Addr Addr; -#endif // Mach types typedef N_Mach::Mach_header64 Mach_header; @@ -1059,9 +1051,6 @@ protected: virtual void buildLoader(const Filter *ft); virtual Linker* newLinker() const; -#if (ACC_CC_BORLANDC) -public: -#endif enum { N_FAT_ARCH = 5 }; protected: __packed_struct(Fat_head) diff --git a/src/snprintf.cpp b/src/snprintf.cpp index 7d5ed944..cab3ce99 100644 --- a/src/snprintf.cpp +++ b/src/snprintf.cpp @@ -83,16 +83,15 @@ #undef LDOUBLE #if 1 #define NO_FLOAT 1 -#if (ACC_CC_SUNPROC) -#else #define float error no_float #define double error no_float -#endif -#elif 0 || (HAVE_LONG_DOUBLE) +#else +#if (HAVE_LONG_DOUBLE) #define LDOUBLE long double #else #define LDOUBLE double #endif +#endif /* * dopr(): poor man's version of doprintf @@ -969,6 +968,4 @@ int main(void) } #endif /* SNPRINTF_TEST */ -/* -vi:ts=4:et -*/ +/* vim:set ts=4 sw=4 et: */ diff --git a/src/util.cpp b/src/util.cpp index 35b0cfce..557111fd 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -603,8 +603,4 @@ time_t time(time_t *t) } // extern "C" - -/* -vi:ts=4:et -*/ - +/* vim:set ts=4 sw=4 et: */ diff --git a/src/util.h b/src/util.h index e68c41ee..b08c5518 100644 --- a/src/util.h +++ b/src/util.h @@ -25,11 +25,9 @@ */ - #ifndef __UPX_UTIL_H #define __UPX_UTIL_H 1 - /************************************************************************* // misc. support functions **************************************************************************/ @@ -37,19 +35,17 @@ char *fn_basename(const char *name); int fn_strcmp(const char *n1, const char *n2); char *fn_strlwr(char *n); -bool fn_has_ext(const char *name, const char *ext, bool ignore_case=true); +bool fn_has_ext(const char *name, const char *ext, bool ignore_case = true); bool file_exists(const char *name); -bool maketempname(char *ofilename, size_t size, - const char *ifilename, const char *ext, bool force=true); -bool makebakname(char *ofilename, size_t size, - const char *ifilename, bool force=true); +bool maketempname(char *ofilename, size_t size, const char *ifilename, const char *ext, + bool force = true); +bool makebakname(char *ofilename, size_t size, const char *ifilename, bool force = true); unsigned get_ratio(unsigned u_len, unsigned c_len); bool set_method_name(char *buf, size_t size, int method, int level); void center_string(char *buf, size_t size, const char *s); - int find(const void *b, int blen, const void *what, int wlen); int find_be16(const void *b, int blen, unsigned what); int find_be32(const void *b, int blen, unsigned what); @@ -60,74 +56,42 @@ int find_le64(const void *b, int blen, upx_uint64_t what); int mem_replace(void *b, int blen, const void *what, int wlen, const void *r); +/************************************************************************* +// protect against integer overflows and malicious header fields +**************************************************************************/ -#if (ACC_CC_BORLANDC && (__BORLANDC__ < 0x0530)) -#elif (ACC_CC_DMC && (__DMC__ < 0x830)) -#elif (ACC_CC_MSC && (_MSC_VER < 1310)) -#else -template -inline int ptr_diff(const T *p1, const T *p2) -{ - COMPILE_TIME_ASSERT(sizeof(T) == 1) - assert(p1 != NULL); assert(p2 != NULL); - ptrdiff_t d = (const char*) p1 - (const char*) p2; - assert((int)d == d); - return (int) d; -} -#endif -inline int ptr_diff(const void *p1, const void *p2) -{ - assert(p1 != NULL); assert(p2 != NULL); - ptrdiff_t d = (const char*) p1 - (const char*) p2; - assert((int)d == d); - return (int) d; +size_t mem_size(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra = 0); +size_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n); + +bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra = 0); +bool mem_size_valid_bytes(upx_uint64_t bytes); + +#define New(type, n) new type[mem_size_get_n(sizeof(type), n)] + +int ptr_diff(const char *p1, const char *p2); + +inline int ptr_diff(const unsigned char *p1, const unsigned char *p2) { + return ptr_diff((const char *) p1, (const char *) p2); } +inline int ptr_diff(const void *p1, const void *p2) { + return ptr_diff((const char *) p1, (const char *) p2); +} /************************************************************************* // some unsigned char string support functions **************************************************************************/ -inline char *strcpy(unsigned char *s1,const unsigned char *s2) -{ - return strcpy((char*) s1,(const char*) s2); +inline char *strcpy(unsigned char *s1, const unsigned char *s2) { + return strcpy((char *) s1, (const char *) s2); } -inline int strcasecmp(const unsigned char *s1,const unsigned char *s2) -{ - return strcasecmp((const char*) s1,(const char*) s2); +inline int strcasecmp(const unsigned char *s1, const unsigned char *s2) { + return strcasecmp((const char *) s1, (const char *) s2); } -inline size_t strlen(const unsigned char *s) -{ - return strlen((const char*) s); -} - - -/************************************************************************* -// -**************************************************************************/ - -#if 0 -bool upx_isdigit(int c); -bool upx_islower(int c); -bool upx_isspace(int c); -int upx_tolower(int c); -#undef isdigit -#undef islower -#undef isspace -#undef tolower -#define isdigit upx_isdigit -#define islower upx_islower -#define isspace upx_isspace -#define tolower upx_tolower -#endif - +inline size_t strlen(const unsigned char *s) { return strlen((const char *) s); } #endif /* already included */ - -/* -vi:ts=4:et -*/ - +/* vim:set ts=4 sw=4 et: */ diff --git a/src/work.cpp b/src/work.cpp index 802d3566..47422631 100644 --- a/src/work.cpp +++ b/src/work.cpp @@ -83,7 +83,7 @@ void do_one_file(const char *iname, char *oname) throwIOException("empty file -- skipped"); if (st.st_size < 512) throwIOException("file is too small -- skipped"); - if (!mem_size_valid(1, st.st_size)) + if (!mem_size_valid_bytes(st.st_size)) throwIOException("file is too large -- skipped"); if ((st.st_mode & S_IWUSR) == 0) {