Coverage Report

Created: 2025-03-15 06:58

/src/zstd/lib/common/error_private.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 * All rights reserved.
4
 *
5
 * This source code is licensed under both the BSD-style license (found in the
6
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
 * in the COPYING file in the root directory of this source tree).
8
 * You may select, at your option, one of the above-listed licenses.
9
 */
10
11
/* Note : this module is expected to remain private, do not expose it */
12
13
#ifndef ERROR_H_MODULE
14
#define ERROR_H_MODULE
15
16
/* ****************************************
17
*  Dependencies
18
******************************************/
19
#include "../zstd_errors.h"  /* enum list */
20
#include "compiler.h"
21
#include "debug.h"
22
#include "zstd_deps.h"       /* size_t */
23
24
/* ****************************************
25
*  Compiler-specific
26
******************************************/
27
#if defined(__GNUC__)
28
#  define ERR_STATIC static __attribute__((unused))
29
#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
30
#  define ERR_STATIC static inline
31
#elif defined(_MSC_VER)
32
#  define ERR_STATIC static __inline
33
#else
34
#  define ERR_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
35
#endif
36
37
38
/*-****************************************
39
*  Customization (error_public.h)
40
******************************************/
41
typedef ZSTD_ErrorCode ERR_enum;
42
15.5M
#define PREFIX(name) ZSTD_error_##name
43
44
45
/*-****************************************
46
*  Error codes handling
47
******************************************/
48
#undef ERROR   /* already defined on Visual Studio */
49
15.5M
#define ERROR(name) ZSTD_ERROR(name)
50
15.5M
#define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
51
52
15.4M
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
zstd_common.c:ERR_isError
Line
Count
Source
52
117k
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
Unexecuted instantiation: zstd_compress.c:ERR_isError
Unexecuted instantiation: zstd_compress_literals.c:ERR_isError
Unexecuted instantiation: zstd_compress_sequences.c:ERR_isError
Unexecuted instantiation: zstd_compress_superblock.c:ERR_isError
Unexecuted instantiation: zstd_double_fast.c:ERR_isError
Unexecuted instantiation: zstd_fast.c:ERR_isError
Unexecuted instantiation: zstd_lazy.c:ERR_isError
Unexecuted instantiation: zstd_ldm.c:ERR_isError
Unexecuted instantiation: zstd_opt.c:ERR_isError
Unexecuted instantiation: zstd_preSplit.c:ERR_isError
Unexecuted instantiation: zstdmt_compress.c:ERR_isError
zstd_decompress.c:ERR_isError
Line
Count
Source
52
6.37M
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
zstd_decompress_block.c:ERR_isError
Line
Count
Source
52
6.20M
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
zstd_v05.c:ERR_isError
Line
Count
Source
52
422k
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
zstd_v06.c:ERR_isError
Line
Count
Source
52
882k
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
zstd_v07.c:ERR_isError
Line
Count
Source
52
1.34M
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
entropy_common.c:ERR_isError
Line
Count
Source
52
11.5k
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
Unexecuted instantiation: error_private.c:ERR_isError
fse_decompress.c:ERR_isError
Line
Count
Source
52
31.6k
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
Unexecuted instantiation: fse_compress.c:ERR_isError
Unexecuted instantiation: hist.c:ERR_isError
Unexecuted instantiation: huf_compress.c:ERR_isError
huf_decompress.c:ERR_isError
Line
Count
Source
52
66.5k
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
Unexecuted instantiation: zstd_ddict.c:ERR_isError
53
54
36.6k
ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
zstd_common.c:ERR_getErrorCode
Line
Count
Source
54
36.6k
ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
Unexecuted instantiation: zstd_compress.c:ERR_getErrorCode
Unexecuted instantiation: zstd_compress_literals.c:ERR_getErrorCode
Unexecuted instantiation: zstd_compress_sequences.c:ERR_getErrorCode
Unexecuted instantiation: zstd_compress_superblock.c:ERR_getErrorCode
Unexecuted instantiation: zstd_double_fast.c:ERR_getErrorCode
Unexecuted instantiation: zstd_fast.c:ERR_getErrorCode
Unexecuted instantiation: zstd_lazy.c:ERR_getErrorCode
Unexecuted instantiation: zstd_ldm.c:ERR_getErrorCode
Unexecuted instantiation: zstd_opt.c:ERR_getErrorCode
Unexecuted instantiation: zstd_preSplit.c:ERR_getErrorCode
Unexecuted instantiation: zstdmt_compress.c:ERR_getErrorCode
Unexecuted instantiation: zstd_decompress.c:ERR_getErrorCode
Unexecuted instantiation: zstd_decompress_block.c:ERR_getErrorCode
Unexecuted instantiation: zstd_v05.c:ERR_getErrorCode
Unexecuted instantiation: zstd_v06.c:ERR_getErrorCode
Unexecuted instantiation: zstd_v07.c:ERR_getErrorCode
Unexecuted instantiation: entropy_common.c:ERR_getErrorCode
Unexecuted instantiation: error_private.c:ERR_getErrorCode
Unexecuted instantiation: fse_decompress.c:ERR_getErrorCode
Unexecuted instantiation: fse_compress.c:ERR_getErrorCode
Unexecuted instantiation: hist.c:ERR_getErrorCode
Unexecuted instantiation: huf_compress.c:ERR_getErrorCode
Unexecuted instantiation: huf_decompress.c:ERR_getErrorCode
Unexecuted instantiation: zstd_ddict.c:ERR_getErrorCode
55
56
/* check and forward error code */
57
#define CHECK_V_F(e, f)     \
58
29.2k
    size_t const e = f;     \
59
29.2k
    do {                    \
60
29.2k
        if (ERR_isError(e)) \
61
29.2k
            return e;       \
62
29.2k
    } while (0)
63
29.2k
#define CHECK_F(f)   do { CHECK_V_F(_var_err__, f); } while (0)
64
65
66
/*-****************************************
67
*  Error Strings
68
******************************************/
69
70
const char* ERR_getErrorString(ERR_enum code);   /* error_private.c */
71
72
ERR_STATIC const char* ERR_getErrorName(size_t code)
73
36.1k
{
74
36.1k
    return ERR_getErrorString(ERR_getErrorCode(code));
75
36.1k
}
zstd_common.c:ERR_getErrorName
Line
Count
Source
73
36.1k
{
74
36.1k
    return ERR_getErrorString(ERR_getErrorCode(code));
75
36.1k
}
Unexecuted instantiation: zstd_compress.c:ERR_getErrorName
Unexecuted instantiation: zstd_compress_literals.c:ERR_getErrorName
Unexecuted instantiation: zstd_compress_sequences.c:ERR_getErrorName
Unexecuted instantiation: zstd_compress_superblock.c:ERR_getErrorName
Unexecuted instantiation: zstd_double_fast.c:ERR_getErrorName
Unexecuted instantiation: zstd_fast.c:ERR_getErrorName
Unexecuted instantiation: zstd_lazy.c:ERR_getErrorName
Unexecuted instantiation: zstd_ldm.c:ERR_getErrorName
Unexecuted instantiation: zstd_opt.c:ERR_getErrorName
Unexecuted instantiation: zstd_preSplit.c:ERR_getErrorName
Unexecuted instantiation: zstdmt_compress.c:ERR_getErrorName
Unexecuted instantiation: zstd_decompress.c:ERR_getErrorName
Unexecuted instantiation: zstd_decompress_block.c:ERR_getErrorName
Unexecuted instantiation: zstd_v05.c:ERR_getErrorName
Unexecuted instantiation: zstd_v06.c:ERR_getErrorName
Unexecuted instantiation: zstd_v07.c:ERR_getErrorName
Unexecuted instantiation: entropy_common.c:ERR_getErrorName
Unexecuted instantiation: error_private.c:ERR_getErrorName
Unexecuted instantiation: fse_decompress.c:ERR_getErrorName
Unexecuted instantiation: fse_compress.c:ERR_getErrorName
Unexecuted instantiation: hist.c:ERR_getErrorName
Unexecuted instantiation: huf_compress.c:ERR_getErrorName
Unexecuted instantiation: huf_decompress.c:ERR_getErrorName
Unexecuted instantiation: zstd_ddict.c:ERR_getErrorName
76
77
/**
78
 * Ignore: this is an internal helper.
79
 *
80
 * This is a helper function to help force C99-correctness during compilation.
81
 * Under strict compilation modes, variadic macro arguments can't be empty.
82
 * However, variadic function arguments can be. Using a function therefore lets
83
 * us statically check that at least one (string) argument was passed,
84
 * independent of the compilation flags.
85
 */
86
static INLINE_KEYWORD UNUSED_ATTR
87
0
void _force_has_format_string(const char *format, ...) {
88
0
  (void)format;
89
0
}
Unexecuted instantiation: zstd_common.c:_force_has_format_string
Unexecuted instantiation: zstd_compress.c:_force_has_format_string
Unexecuted instantiation: zstd_compress_literals.c:_force_has_format_string
Unexecuted instantiation: zstd_compress_sequences.c:_force_has_format_string
Unexecuted instantiation: zstd_compress_superblock.c:_force_has_format_string
Unexecuted instantiation: zstd_double_fast.c:_force_has_format_string
Unexecuted instantiation: zstd_fast.c:_force_has_format_string
Unexecuted instantiation: zstd_lazy.c:_force_has_format_string
Unexecuted instantiation: zstd_ldm.c:_force_has_format_string
Unexecuted instantiation: zstd_opt.c:_force_has_format_string
Unexecuted instantiation: zstd_preSplit.c:_force_has_format_string
Unexecuted instantiation: zstdmt_compress.c:_force_has_format_string
Unexecuted instantiation: zstd_decompress.c:_force_has_format_string
Unexecuted instantiation: zstd_decompress_block.c:_force_has_format_string
Unexecuted instantiation: zstd_v05.c:_force_has_format_string
Unexecuted instantiation: zstd_v06.c:_force_has_format_string
Unexecuted instantiation: zstd_v07.c:_force_has_format_string
Unexecuted instantiation: entropy_common.c:_force_has_format_string
Unexecuted instantiation: error_private.c:_force_has_format_string
Unexecuted instantiation: fse_decompress.c:_force_has_format_string
Unexecuted instantiation: fse_compress.c:_force_has_format_string
Unexecuted instantiation: hist.c:_force_has_format_string
Unexecuted instantiation: huf_compress.c:_force_has_format_string
Unexecuted instantiation: huf_decompress.c:_force_has_format_string
Unexecuted instantiation: zstd_ddict.c:_force_has_format_string
90
91
/**
92
 * Ignore: this is an internal helper.
93
 *
94
 * We want to force this function invocation to be syntactically correct, but
95
 * we don't want to force runtime evaluation of its arguments.
96
 */
97
#define _FORCE_HAS_FORMAT_STRING(...)              \
98
78.5k
    do {                                           \
99
78.5k
        if (0) {                                   \
100
0
            _force_has_format_string(__VA_ARGS__); \
101
0
        }                                          \
102
78.5k
    } while (0)
103
104
#define ERR_QUOTE(str) #str
105
106
/**
107
 * Return the specified error if the condition evaluates to true.
108
 *
109
 * In debug modes, prints additional information.
110
 * In order to do that (particularly, printing the conditional that failed),
111
 * this can't just wrap RETURN_ERROR().
112
 */
113
#define RETURN_ERROR_IF(cond, err, ...)                                        \
114
11.3M
    do {                                                                       \
115
13.1M
        if (cond) {                                                            \
116
12.7k
            RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s",          \
117
12.7k
                  __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \
118
12.7k
            _FORCE_HAS_FORMAT_STRING(__VA_ARGS__);                             \
119
12.7k
            RAWLOG(3, ": " __VA_ARGS__);                                       \
120
12.7k
            RAWLOG(3, "\n");                                                   \
121
12.7k
            return ERROR(err);                                                 \
122
12.7k
        }                                                                      \
123
11.3M
    } while (0)
124
125
/**
126
 * Unconditionally return the specified error.
127
 *
128
 * In debug modes, prints additional information.
129
 */
130
#define RETURN_ERROR(err, ...)                                               \
131
32.1k
    do {                                                                     \
132
32.1k
        RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
133
32.1k
              __FILE__, __LINE__, ERR_QUOTE(ERROR(err)));                    \
134
32.1k
        _FORCE_HAS_FORMAT_STRING(__VA_ARGS__);                               \
135
32.1k
        RAWLOG(3, ": " __VA_ARGS__);                                         \
136
32.1k
        RAWLOG(3, "\n");                                                     \
137
32.1k
        return ERROR(err);                                                   \
138
32.1k
    } while(0)
139
140
/**
141
 * If the provided expression evaluates to an error code, returns that error code.
142
 *
143
 * In debug modes, prints additional information.
144
 */
145
#define FORWARD_IF_ERROR(err, ...)                                                 \
146
4.28M
    do {                                                                           \
147
4.28M
        size_t const err_code = (err);                                             \
148
4.28M
        if (ERR_isError(err_code)) {                                               \
149
33.6k
            RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s",                 \
150
33.6k
                  __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \
151
33.6k
            _FORCE_HAS_FORMAT_STRING(__VA_ARGS__);                                 \
152
33.6k
            RAWLOG(3, ": " __VA_ARGS__);                                           \
153
33.6k
            RAWLOG(3, "\n");                                                       \
154
33.6k
            return err_code;                                                       \
155
33.6k
        }                                                                          \
156
4.28M
    } while(0)
157
158
#endif /* ERROR_H_MODULE */