Coverage Report

Created: 2026-02-14 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/upx/src/msg.cpp
Line
Count
Source
1
/* msg.cpp -- info and error messages
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 "util/system_headers.h"
29
#include <typeinfo> // typeid()
30
#include "conf.h"
31
32
/*************************************************************************
33
//
34
**************************************************************************/
35
36
static int pr_need_nl = 0;
37
38
12.3k
void printSetNl(int need_nl) noexcept { pr_need_nl = need_nl; }
39
40
171
void printClearLine(FILE *f) noexcept {
41
171
    static char clear_line_msg[1 + 79 + 1 + 1];
42
171
    if (!clear_line_msg[0]) {
43
1
        char *msg = clear_line_msg;
44
1
        msg[0] = '\r';
45
1
        memset(msg + 1, ' ', 79);
46
1
        msg[80] = '\r';
47
1
        msg[81] = 0;
48
1
    }
49
50
171
    fflush(stdout);
51
171
    fflush(stderr);
52
171
    if (f == nullptr)
53
0
        f = stdout;
54
171
    con_fprintf(f, "%s", clear_line_msg); // NOLINT(clang-analyzer-unix.Stream)
55
171
    fflush(f);
56
171
    printSetNl(0);
57
171
}
58
59
57.3k
static void pr_print(bool c, const char *msg) noexcept {
60
57.3k
    if (c && !opt->to_stdout)
61
0
        con_fprintf(stderr, "%s", msg);
62
57.3k
    else
63
57.3k
        fprintf(stderr, "%s", msg);
64
57.3k
}
65
66
14.3k
static void pr_error(const char *iname, const char *msg, bool is_warning) noexcept {
67
14.3k
    fflush(stdout);
68
14.3k
    fflush(stderr);
69
14.3k
    char buf[1024];
70
14.3k
    buf[0] = 0;
71
14.3k
    if (pr_need_nl == 2)
72
0
        printClearLine(stdout);
73
14.3k
    else if (pr_need_nl) {
74
5.49k
        buf[0] = '\n';
75
5.49k
        buf[1] = 0;
76
5.49k
        printSetNl(0);
77
5.49k
    }
78
79
    // This hack is needed, otherwise error messages may get lost
80
    // when the cursor is not yet at the bottom of the screen.
81
    // At least I can use some colors then...
82
14.3k
    bool c = acc_isatty(STDERR_FILENO) ? 1 : 0;
83
84
14.3k
    int fg = con_fg(stderr, FG_BRTRED);
85
14.3k
    upx_safe_snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s: ", progname);
86
14.3k
    pr_print(c, buf);
87
    //(void)con_fg(stderr,FG_RED);
88
14.3k
    upx_safe_snprintf(buf, sizeof(buf), "%s: ", iname);
89
14.3k
    pr_print(c, buf);
90
    //(void)con_fg(stderr,FG_BRTRED);
91
14.3k
    pr_print(c, msg);
92
14.3k
    pr_print(c, "\n");
93
14.3k
    fflush(stdout);
94
14.3k
    fflush(stderr);
95
14.3k
    fg = con_fg(stderr, fg);
96
97
14.3k
    UNUSED(is_warning);
98
14.3k
    UNUSED(fg);
99
14.3k
}
100
101
14.3k
void printErr(const char *iname, const Throwable &e) noexcept {
102
14.3k
    char buf[1024];
103
14.3k
    size_t l;
104
105
14.3k
    upx_safe_snprintf(buf, sizeof(buf), "%s", prettyExceptionName(typeid(e).name()));
106
14.3k
    l = strlen(buf);
107
14.3k
    if (l < sizeof(buf) && e.getMsg())
108
14.3k
        upx_safe_snprintf(buf + l, sizeof(buf) - l, ": %s", e.getMsg());
109
14.3k
    l = strlen(buf);
110
14.3k
    if (l < sizeof(buf) && e.getErrno()) {
111
0
        upx_safe_snprintf(buf + l, sizeof(buf) - l, ": %s", strerror(e.getErrno()));
112
0
#if 1
113
        // some compilers (e.g. Borland C++) put a trailing '\n'
114
        // into the strerror() result
115
0
        l = strlen(buf);
116
0
        while (l-- > 0 && (buf[l] == '\n' || buf[l] == ' '))
117
0
            buf[l] = 0;
118
0
#endif
119
0
    }
120
14.3k
    pr_error(iname, buf, e.isWarning());
121
14.3k
}
122
123
0
void printErr(const char *iname, const char *format, ...) noexcept {
124
0
    va_list args;
125
0
    char buf[1024];
126
127
0
    va_start(args, format);
128
0
    upx_safe_vsnprintf(buf, sizeof(buf), format, args);
129
0
    va_end(args);
130
131
0
    pr_error(iname, buf, false);
132
0
}
133
134
0
void printWarn(const char *iname, const char *format, ...) noexcept {
135
0
    va_list args;
136
0
    char buf[1024];
137
138
0
    va_start(args, format);
139
0
    upx_safe_vsnprintf(buf, sizeof(buf), format, args);
140
0
    va_end(args);
141
142
0
    pr_error(iname, buf, true);
143
0
}
144
145
0
void printUnhandledException(const char *iname, const std::exception *e) noexcept {
146
0
    if (e != nullptr)
147
0
        printErr(iname, "unhandled exception: %s\n", prettyExceptionName(e->what()));
148
0
    else
149
0
        printErr(iname, "internal error: unhandled exception!\n");
150
0
    if (opt->cmd != CMD_COMPRESS) {
151
0
        printErr(iname, "  this file has possibly been modified/hacked; take care!\n");
152
0
    }
153
0
}
154
155
/*************************************************************************
156
// info
157
**************************************************************************/
158
159
static int info_header = 0;
160
161
903
static void info_print(const char *msg) {
162
903
    if (opt->info_mode <= 0)
163
0
        return;
164
903
    FILE *f = opt->to_stdout ? stderr : stdout;
165
903
    if (pr_need_nl) {
166
171
        printClearLine(f);
167
171
        con_fprintf(f, "%s\n", msg);
168
732
    } else if (pr_need_nl)
169
0
        con_fprintf(f, "\n%s\n", msg);
170
732
    else
171
732
        con_fprintf(f, "%s\n", msg);
172
903
    fflush(f);
173
903
    printSetNl(0);
174
903
}
175
176
14.3k
void infoHeader() { info_header = 0; }
177
178
0
void infoHeader(const char *format, ...) {
179
0
    if (opt->info_mode <= 0)
180
0
        return;
181
0
    va_list args;
182
0
    char buf[1024];
183
0
    va_start(args, format);
184
0
    upx_safe_vsnprintf(buf, sizeof(buf), format, args);
185
0
    va_end(args);
186
0
    info_print(buf);
187
0
    info_header = 1;
188
0
}
189
190
955
void info(const char *format, ...) {
191
955
    if (opt->info_mode <= 0)
192
52
        return;
193
903
    va_list args;
194
903
    char buf[1024];
195
903
    const int n = 4 * info_header;
196
903
    memset(buf, ' ', n);
197
903
    va_start(args, format);
198
903
    upx_safe_vsnprintf(buf + n, sizeof(buf) - n, format, args);
199
903
    va_end(args);
200
903
    info_print(buf);
201
903
}
202
203
1.78k
void infoWarning(const char *format, ...) {
204
1.78k
    if (opt->info_mode <= 0)
205
880
        return;
206
903
    va_list args;
207
903
    char buf[1024];
208
903
    va_start(args, format);
209
903
    upx_safe_vsnprintf(buf, sizeof(buf), format, args);
210
903
    va_end(args);
211
903
    info("[WARNING] %s\n", buf);
212
903
}
213
214
0
void infoWriting(const char *what, upx_int64_t size) {
215
0
    if (opt->info_mode <= 0)
216
0
        return;
217
0
    info("Writing %s: %lld bytes", what, size);
218
0
}
219
220
/* vim:set ts=4 sw=4 et: */