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 #define PREFIX(name) ZSTD_error_##name 43 44 45 /*-**************************************** 46 * Error codes handling 47 ******************************************/ 48 #undef ERROR /* already defined on Visual Studio */ 49 #define ERROR(name) ZSTD_ERROR(name) 50 #define ZSTD_ERROR(name) ((size_t)-PREFIX(name)) 51 52 ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); } 53 54 ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); } 55 56 /* check and forward error code */ 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) 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 { 74 return ERR_getErrorString(ERR_getErrorCode(code)); 75 } 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 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 */ 97 #define _FORCE_HAS_FORMAT_STRING(...) \ 98 do { \ 99 if (0) { \ 100 _force_has_format_string(__VA_ARGS__); \ 101 } \ 102 } 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 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) 124 125 /** 126 * Unconditionally return the specified error. 127 * 128 * In debug modes, prints additional information. 129 */ 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) 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 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) 157 158 #endif /* ERROR_H_MODULE */ 159