Coverage Report

Created: 2026-11-12 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/upx/src/except.cpp
Line
Count
Source
1
/* except.cpp --
2
3
   This file is part of the UPX executable compressor.
4
5
   Copyright (C) Markus Franz Xaver Johannes Oberhumer
6
   Copyright (C) Laszlo Molnar
7
   All Rights Reserved.
8
9
   UPX and the UCL library are free software; you can redistribute them
10
   and/or modify them under the terms of the GNU General Public License as
11
   published by the Free Software Foundation; either version 2 of
12
   the License, or (at your option) any later version.
13
14
   This program is distributed in the hope that it will be useful,
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
   GNU General Public License for more details.
18
19
   You should have received a copy of the GNU General Public License
20
   along with this program; see the file COPYING.
21
   If not, write to the Free Software Foundation, Inc.,
22
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24
   Markus F.X.J. Oberhumer              Laszlo Molnar
25
   <markus@oberhumer.com>               <ezerotven+github@gmail.com>
26
 */
27
28
#include "conf.h"
29
30
/*************************************************************************
31
//
32
**************************************************************************/
33
34
/*static*/ Throwable::Stats Throwable::stats;
35
36
12.1M
Throwable::Throwable(const char *m, int e, bool w) noexcept : super(),
37
12.1M
                                                              msg(nullptr),
38
12.1M
                                                              err(e),
39
12.1M
                                                              is_warning(w) {
40
12.1M
    if (m != nullptr) {
41
12.1M
        msg = ::strdup(m);
42
12.1M
        assert_noexcept(msg != nullptr);
43
12.1M
    }
44
12.1M
    NO_fprintf(stderr, "construct exception: %zu %zu %s\n", size_t(stats.counter_current),
45
12.1M
               size_t(stats.counter_total), (const char *) msg);
46
12.1M
    stats.counter_current += 1;
47
12.1M
    stats.counter_total += 1;
48
12.1M
}
49
50
0
Throwable::Throwable(const Throwable &other) noexcept : super(other),
51
0
                                                        msg(nullptr),
52
0
                                                        err(other.err),
53
0
                                                        is_warning(other.is_warning) {
54
0
    if (other.msg != nullptr) {
55
0
        msg = ::strdup(other.msg);
56
0
        assert_noexcept(msg != nullptr);
57
0
    }
58
0
    NO_fprintf(stderr, "copy construct exception: %zu %zu %s\n", size_t(stats.counter_current),
59
0
               size_t(stats.counter_total), (const char *) msg);
60
0
    stats.counter_current += 1;
61
0
    stats.counter_total += 1;
62
0
}
63
64
12.1M
Throwable::~Throwable() noexcept {
65
12.1M
    stats.counter_current -= 1;
66
12.1M
    NO_fprintf(stderr, "destruct exception: %zu %zu %s\n", size_t(stats.counter_current),
67
12.1M
               size_t(stats.counter_total), (const char *) msg);
68
12.1M
    upx::owner_free(msg);
69
12.1M
}
70
71
/*************************************************************************
72
// compression
73
**************************************************************************/
74
75
11.9M
void throwCantPack(const char *msg) {
76
    // UGLY, but makes things easier
77
11.9M
    if (opt->cmd == CMD_NONE)
78
11.9M
        throw CantPackException(msg);
79
24.5k
    else if (opt->cmd == CMD_COMPRESS)
80
0
        throw CantPackException(msg);
81
24.5k
    else if (opt->cmd == CMD_FILEINFO)
82
0
        throw CantPackException(msg);
83
24.5k
    else
84
24.5k
        throw CantUnpackException(msg);
85
11.9M
}
86
87
0
void throwCantPackExact() { throwCantPack("option '--exact' does not work with this file"); }
88
89
0
void throwFilterException() { throwCantPack("filter problem"); }
90
91
94
void throwUnknownExecutableFormat(const char *msg, bool warn) {
92
94
    throw UnknownExecutableFormatException(msg, warn);
93
94
}
94
95
0
void throwNotCompressible(const char *msg) { throw NotCompressibleException(msg); }
96
97
0
void throwAlreadyPacked(const char *msg) { throw AlreadyPackedException(msg); }
98
99
0
void throwAlreadyPackedByUPX(const char *msg) {
100
0
    if (msg == nullptr)
101
0
        msg = "already packed by UPX";
102
0
    throwAlreadyPacked(msg);
103
0
}
104
105
/*************************************************************************
106
// decompression
107
**************************************************************************/
108
109
14.4k
void throwCantUnpack(const char *msg) {
110
    // UGLY, but makes things easier
111
14.4k
    throwCantPack(msg);
112
14.4k
}
113
114
7.94k
void throwNotPacked(const char *msg) {
115
7.94k
    if (msg == nullptr)
116
7.94k
        msg = "not packed by UPX";
117
7.94k
    throw NotPackedException(msg);
118
7.94k
}
119
120
483
void throwChecksumError() { throw Exception("checksum error"); }
121
122
5.62k
void throwCompressedDataViolation() { throw Exception("compressed data violation"); }
123
124
/*************************************************************************
125
// other
126
**************************************************************************/
127
128
80.2k
void throwInternalError(const char *msg) { throw InternalError(msg); }
129
130
0
void throwBadLoader() { throwInternalError("bad loader"); }
131
132
0
void throwOutOfMemoryException(const char *msg) {
133
0
    if (msg == nullptr)
134
0
        msg = "out of memory";
135
0
    throw OutOfMemoryException(msg);
136
0
}
137
138
4.20k
void throwIOException(const char *msg, int e) { throw IOException(msg, e); }
139
140
46.0k
void throwEOFException(const char *msg, int e) {
141
46.0k
    if (msg == nullptr && e == 0)
142
46.0k
        msg = "premature end of file";
143
46.0k
    throw EOFException(msg, e);
144
46.0k
}
145
146
/*************************************************************************
147
// varargs overloads
148
**************************************************************************/
149
150
template <>
151
44.7k
void throwCantPack(const char *format, ...) {
152
44.7k
    char msg[1024];
153
44.7k
    va_list ap;
154
44.7k
    va_start(ap, format);
155
44.7k
    upx_safe_vsnprintf_noexcept(msg, sizeof(msg), format, ap);
156
44.7k
    va_end(ap);
157
44.7k
    throwCantPack(msg);
158
44.7k
}
159
160
template <>
161
3.05k
void throwCantUnpack(const char *format, ...) {
162
3.05k
    char msg[1024];
163
3.05k
    va_list ap;
164
3.05k
    va_start(ap, format);
165
3.05k
    upx_safe_vsnprintf_noexcept(msg, sizeof(msg), format, ap);
166
3.05k
    va_end(ap);
167
3.05k
    throwCantUnpack(msg);
168
3.05k
}
169
170
template <>
171
14
void throwInternalError(const char *format, ...) {
172
14
    char msg[1024];
173
14
    va_list ap;
174
14
    va_start(ap, format);
175
14
    upx_safe_vsnprintf_noexcept(msg, sizeof(msg), format, ap);
176
14
    va_end(ap);
177
14
    throwInternalError(msg);
178
14
}
179
180
template <>
181
0
void throwIOException2(const char *format, ...) {
182
0
    char msg[1024];
183
0
    va_list ap;
184
0
    va_start(ap, format);
185
0
    upx_safe_vsnprintf_noexcept(msg, sizeof(msg), format, ap);
186
0
    va_end(ap);
187
0
    throwIOException(msg);
188
0
}
189
190
/*************************************************************************
191
// util
192
**************************************************************************/
193
194
0
void assertFailed(const char *expr, const char *file, int line, const char *func) noexcept {
195
0
    fflush(stdout);
196
0
    fprintf(stderr, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line);
197
0
    std::terminate();
198
0
}
199
200
51.1M
void assertFailed(int e, const char *expr, const char *file, int line, const char *func) noexcept {
201
51.1M
    if very_unlikely (!e)
202
0
        assertFailed(expr, file, line, func);
203
51.1M
}
204
205
249
void throwAssertFailed(const char *expr, const char *file, int line, const char *func) may_throw {
206
249
    if (opt->debug.debug_level >= 1) {
207
0
        throwCantPack("corrupted file; details: %s (%s: %s: %d)", expr, file, func, line);
208
249
    } else {
209
249
        throwCantPack("corrupted file; try '--debug' for more details");
210
249
    }
211
249
}
212
213
39.6k
const char *prettyExceptionName(const char *n) noexcept {
214
39.6k
    if (n == nullptr)
215
0
        return "(null)";
216
112k
    while (*n) {
217
112k
        if (*n >= '0' && *n <= '9') // Linux ABI
218
73.1k
            n++;
219
39.6k
        else if (*n == ' ')
220
0
            n++;
221
39.6k
        else if (strncmp(n, "class ", 6) == 0) // Visual C++ (MSVC)
222
0
            n += 6;
223
39.6k
        else
224
39.6k
            break;
225
112k
    }
226
39.6k
    return n;
227
39.6k
}
228
229
/* vim:set ts=4 sw=4 et: */