| Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
| W. Felix Handte | 5d693cc | 2022-12-20 12:49:47 -0500 | [diff] [blame] | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 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). |
| Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
| Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 9 | */ |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 10 | |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 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 | |
| Yann Collet | 72bff50 | 2016-02-03 12:06:24 +0100 | [diff] [blame] | 16 | /* **************************************** |
| 17 | * Dependencies |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 18 | ******************************************/ |
| Nick Terrell | 09149be | 2021-04-30 15:02:12 -0700 | [diff] [blame] | 19 | #include "../zstd_errors.h" /* enum list */ |
| Nick Terrell | a5f2c45 | 2021-09-17 11:43:04 -0700 | [diff] [blame] | 20 | #include "compiler.h" |
| 21 | #include "debug.h" |
| Nick Terrell | 09149be | 2021-04-30 15:02:12 -0700 | [diff] [blame] | 22 | #include "zstd_deps.h" /* size_t */ |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 23 | |
| Yann Collet | 72bff50 | 2016-02-03 12:06:24 +0100 | [diff] [blame] | 24 | /* **************************************** |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 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 | |
| Yann Collet | 72bff50 | 2016-02-03 12:06:24 +0100 | [diff] [blame] | 38 | /*-**************************************** |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 39 | * Customization (error_public.h) |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 40 | ******************************************/ |
| Yann Collet | 982ffc7 | 2016-02-05 02:33:10 +0100 | [diff] [blame] | 41 | typedef ZSTD_ErrorCode ERR_enum; |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 42 | #define PREFIX(name) ZSTD_error_##name |
| 43 | |
| Yann Collet | 72bff50 | 2016-02-03 12:06:24 +0100 | [diff] [blame] | 44 | |
| 45 | /*-**************************************** |
| 46 | * Error codes handling |
| 47 | ******************************************/ |
| Yann Collet | 20bd246 | 2020-05-11 19:29:36 -0700 | [diff] [blame] | 48 | #undef ERROR /* already defined on Visual Studio */ |
| Nick Terrell | 7c365eb | 2017-08-25 17:44:32 -0700 | [diff] [blame] | 49 | #define ERROR(name) ZSTD_ERROR(name) |
| 50 | #define ZSTD_ERROR(name) ((size_t)-PREFIX(name)) |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 51 | |
| 52 | ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); } |
| 53 | |
| Yann Collet | c75e4c2 | 2016-05-10 17:47:11 +0200 | [diff] [blame] | 54 | ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); } |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 55 | |
| Carl Woffenden | edd9a07 | 2020-04-07 11:02:06 +0200 | [diff] [blame] | 56 | /* check and forward error code */ |
| Nick Terrell | 8193250 | 2023-11-21 13:26:25 -0800 | [diff] [blame] | 57 | #define CHECK_V_F(e, f) \ |
| 58 | size_t const e = f; \ |
| 59 | do { \ |
| 60 | if (ERR_isError(e)) \ |
| 61 | return e; \ |
| 62 | } while (0) |
| 63 | #define CHECK_F(f) do { CHECK_V_F(_var_err__, f); } while (0) |
| Carl Woffenden | edd9a07 | 2020-04-07 11:02:06 +0200 | [diff] [blame] | 64 | |
| Yann Collet | 72bff50 | 2016-02-03 12:06:24 +0100 | [diff] [blame] | 65 | |
| 66 | /*-**************************************** |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 67 | * Error Strings |
| 68 | ******************************************/ |
| 69 | |
| Yann Collet | ef2357d | 2016-10-11 17:24:50 -0700 | [diff] [blame] | 70 | const char* ERR_getErrorString(ERR_enum code); /* error_private.c */ |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 71 | |
| Yann Collet | c75e4c2 | 2016-05-10 17:47:11 +0200 | [diff] [blame] | 72 | ERR_STATIC const char* ERR_getErrorName(size_t code) |
| 73 | { |
| 74 | return ERR_getErrorString(ERR_getErrorCode(code)); |
| 75 | } |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 76 | |
| Nick Terrell | a5f2c45 | 2021-09-17 11:43:04 -0700 | [diff] [blame] | 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 | void _force_has_format_string(const char *format, ...) { |
| 88 | (void)format; |
| 89 | } |
| 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 | */ |
| Nick Terrell | 8193250 | 2023-11-21 13:26:25 -0800 | [diff] [blame] | 97 | #define _FORCE_HAS_FORMAT_STRING(...) \ |
| 98 | do { \ |
| 99 | if (0) { \ |
| 100 | _force_has_format_string(__VA_ARGS__); \ |
| 101 | } \ |
| 102 | } while (0) |
| Nick Terrell | a5f2c45 | 2021-09-17 11:43:04 -0700 | [diff] [blame] | 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 | */ |
| Nick Terrell | 8193250 | 2023-11-21 13:26:25 -0800 | [diff] [blame] | 113 | #define RETURN_ERROR_IF(cond, err, ...) \ |
| 114 | do { \ |
| 115 | if (cond) { \ |
| 116 | RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \ |
| 117 | __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \ |
| 118 | _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \ |
| 119 | RAWLOG(3, ": " __VA_ARGS__); \ |
| 120 | RAWLOG(3, "\n"); \ |
| 121 | return ERROR(err); \ |
| 122 | } \ |
| 123 | } while (0) |
| Nick Terrell | a5f2c45 | 2021-09-17 11:43:04 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Unconditionally return the specified error. |
| 127 | * |
| 128 | * In debug modes, prints additional information. |
| 129 | */ |
| Nick Terrell | 8193250 | 2023-11-21 13:26:25 -0800 | [diff] [blame] | 130 | #define RETURN_ERROR(err, ...) \ |
| 131 | do { \ |
| 132 | RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \ |
| 133 | __FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \ |
| 134 | _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \ |
| 135 | RAWLOG(3, ": " __VA_ARGS__); \ |
| 136 | RAWLOG(3, "\n"); \ |
| 137 | return ERROR(err); \ |
| 138 | } while(0) |
| Nick Terrell | a5f2c45 | 2021-09-17 11:43:04 -0700 | [diff] [blame] | 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 | */ |
| Nick Terrell | 8193250 | 2023-11-21 13:26:25 -0800 | [diff] [blame] | 145 | #define FORWARD_IF_ERROR(err, ...) \ |
| 146 | do { \ |
| 147 | size_t const err_code = (err); \ |
| 148 | if (ERR_isError(err_code)) { \ |
| 149 | RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \ |
| 150 | __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \ |
| 151 | _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \ |
| 152 | RAWLOG(3, ": " __VA_ARGS__); \ |
| 153 | RAWLOG(3, "\n"); \ |
| 154 | return err_code; \ |
| 155 | } \ |
| 156 | } while(0) |
| Nick Terrell | a5f2c45 | 2021-09-17 11:43:04 -0700 | [diff] [blame] | 157 | |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 158 | #endif /* ERROR_H_MODULE */ |