Coverage Report

Created: 2025-07-23 06:39

/src/upx/src/util/snprintf.h
Line
Count
Source (jump to first uncovered line)
1
/* snprintf.h --
2
3
   This file is part of the UPX executable compressor.
4
5
   Copyright (C) 1996-2025 Markus Franz Xaver Johannes Oberhumer
6
   Copyright (C) 1996-2025 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
#pragma once
29
30
/*************************************************************************
31
// UPX version of string functions, with assertions and sane limits
32
**************************************************************************/
33
34
upx_rsize_t upx_safe_strlen(const char *) may_throw;
35
36
// info: snprintf() returns length and NOT size, but max_size is indeed size (incl NUL)
37
38
int upx_safe_vsnprintf(char *str, upx_rsize_t max_size, const char *format, va_list ap) may_throw;
39
int upx_safe_snprintf(char *str, upx_rsize_t max_size, const char *format, ...)
40
    may_throw attribute_format(3, 4);
41
42
// malloc's *ptr
43
int upx_safe_vasprintf(char **ptr, const char *format, va_list ap) may_throw;
44
int upx_safe_asprintf(char **ptr, const char *format, ...) may_throw attribute_format(2, 3);
45
46
// returns a malloc'd pointer
47
char *upx_safe_xprintf(const char *format, ...) may_throw attribute_format(1, 2);
48
49
// noexcept variants (these use "assert_noexcept")
50
upx_rsize_t upx_safe_strlen_noexcept(const char *) noexcept;
51
int upx_safe_vsnprintf_noexcept(char *str, upx_rsize_t max_size, const char *format,
52
                                va_list ap) noexcept;
53
54
// globally redirect some functions
55
#undef strlen
56
209k
#define strlen upx_safe_strlen
57
58
#undef snprintf
59
#undef sprintf
60
#undef vsnprintf
61
419k
#define snprintf  upx_safe_snprintf
62
#define sprintf   ERROR_sprintf_IS_DANGEROUS_USE_snprintf
63
#define vsnprintf upx_safe_vsnprintf
64
65
/*************************************************************************
66
// some uchar string support functions to avoid casts
67
**************************************************************************/
68
69
15.2k
forceinline upx_rsize_t upx_safe_strlen(const uchar *s) may_throw {
70
15.2k
    return upx_safe_strlen((const char *) s);
71
15.2k
}
72
73
0
forceinline uchar *strcpy(uchar *s1, const uchar *s2) noexcept {
74
0
    return (uchar *) strcpy((char *) s1, (const char *) s2);
75
0
}
76
77
0
forceinline int strcmp(const uchar *s1, const char *s2) noexcept {
78
0
    return strcmp((const char *) s1, s2);
79
0
}
80
0
forceinline int strcmp(const char *s1, const uchar *s2) noexcept {
81
0
    return strcmp(s1, (const char *) s2);
82
0
}
83
0
forceinline int strcmp(const uchar *s1, const uchar *s2) noexcept {
84
0
    return strcmp((const char *) s1, (const char *) s2);
85
0
}
86
87
0
forceinline int strcasecmp(const uchar *s1, const char *s2) noexcept {
88
0
    return strcasecmp((const char *) s1, s2);
89
0
}
90
0
forceinline int strcasecmp(const char *s1, const uchar *s2) noexcept {
91
0
    return strcasecmp(s1, (const char *) s2);
92
0
}
93
0
forceinline int strcasecmp(const uchar *s1, const uchar *s2) noexcept {
94
0
    return strcasecmp((const char *) s1, (const char *) s2);
95
0
}
96
97
/* vim:set ts=4 sw=4 et: */