| 1 | #ifndef GIT_COMPAT_UTIL_H |
| 2 | #define GIT_COMPAT_UTIL_H |
| 3 | |
| 4 | #if __STDC_VERSION__ - 0 < 199901L |
| 5 | /* |
| 6 | * Git is in a testing period for mandatory C99 support in the compiler. If |
| 7 | * your compiler is reasonably recent, you can try to enable C99 support (or, |
| 8 | * for MSVC, C11 support). If you encounter a problem and can't enable C99 |
| 9 | * support with your compiler (such as with "-std=gnu99") and don't have access |
| 10 | * to one with this support, such as GCC or Clang, you can remove this #if |
| 11 | * directive, but please report the details of your system to |
| 12 | * git@vger.kernel.org. |
| 13 | */ |
| 14 | #error "Required C99 support is in a test phase. Please see git-compat-util.h for more details." |
| 15 | #endif |
| 16 | |
| 17 | #ifdef USE_MSVC_CRTDBG |
| 18 | /* |
| 19 | * For these to work they must appear very early in each |
| 20 | * file -- before most of the standard header files. |
| 21 | */ |
| 22 | #include <stdlib.h> |
| 23 | #include <crtdbg.h> |
| 24 | #endif |
| 25 | |
| 26 | #include "compat/posix.h" |
| 27 | |
| 28 | struct strbuf; |
| 29 | |
| 30 | #if defined(__GNUC__) || defined(__clang__) |
| 31 | # define PRAGMA(pragma) _Pragma(#pragma) |
| 32 | # define DISABLE_WARNING(warning) PRAGMA(GCC diagnostic ignored #warning) |
| 33 | #else |
| 34 | # define DISABLE_WARNING(warning) |
| 35 | #endif |
| 36 | |
| 37 | #undef FLEX_ARRAY |
| 38 | #define FLEX_ARRAY /* empty - weather balloon to require C99 FAM */ |
| 39 | |
| 40 | /* |
| 41 | * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression. |
| 42 | * @cond: the compile-time condition which must be true. |
| 43 | * |
| 44 | * Your compile will fail if the condition isn't true, or can't be evaluated |
| 45 | * by the compiler. This can be used in an expression: its value is "0". |
| 46 | * |
| 47 | * Example: |
| 48 | * #define foo_to_char(foo) \ |
| 49 | * ((char *)(foo) \ |
| 50 | * + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0)) |
| 51 | */ |
| 52 | #define BUILD_ASSERT_OR_ZERO(cond) \ |
| 53 | (sizeof(char [1 - 2*!(cond)]) - 1) |
| 54 | |
| 55 | #if GIT_GNUC_PREREQ(3, 1) |
| 56 | /* &arr[0] degrades to a pointer: a different type from an array */ |
| 57 | # define BARF_UNLESS_AN_ARRAY(arr) \ |
| 58 | BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(__typeof__(arr), \ |
| 59 | __typeof__(&(arr)[0]))) |
| 60 | # define BARF_UNLESS_COPYABLE(dst, src) \ |
| 61 | BUILD_ASSERT_OR_ZERO(__builtin_types_compatible_p(__typeof__(*(dst)), \ |
| 62 | __typeof__(*(src)))) |
| 63 | |
| 64 | # define BARF_UNLESS_SIGNED(var) BUILD_ASSERT_OR_ZERO(((__typeof__(var)) -1) < 0) |
| 65 | # define BARF_UNLESS_UNSIGNED(var) BUILD_ASSERT_OR_ZERO(((__typeof__(var)) -1) > 0) |
| 66 | #else |
| 67 | # define BARF_UNLESS_AN_ARRAY(arr) 0 |
| 68 | # define BARF_UNLESS_COPYABLE(dst, src) \ |
| 69 | BUILD_ASSERT_OR_ZERO(0 ? ((*(dst) = *(src)), 0) : \ |
| 70 | sizeof(*(dst)) == sizeof(*(src))) |
| 71 | |
| 72 | # define BARF_UNLESS_SIGNED(var) 0 |
| 73 | # define BARF_UNLESS_UNSIGNED(var) 0 |
| 74 | #endif |
| 75 | |
| 76 | /* |
| 77 | * ARRAY_SIZE - get the number of elements in a visible array |
| 78 | * @x: the array whose size you want. |
| 79 | * |
| 80 | * This does not work on pointers, or arrays declared as [], or |
| 81 | * function parameters. With correct compiler support, such usage |
| 82 | * will cause a build error (see the build_assert_or_zero macro). |
| 83 | */ |
| 84 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + BARF_UNLESS_AN_ARRAY(x)) |
| 85 | |
| 86 | #define bitsizeof(x) (CHAR_BIT * sizeof(x)) |
| 87 | |
| 88 | #define maximum_signed_value_of_type(a) \ |
| 89 | (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a))) |
| 90 | |
| 91 | #define maximum_unsigned_value_of_type(a) \ |
| 92 | (UINTMAX_MAX >> (bitsizeof(uintmax_t) - bitsizeof(a))) |
| 93 | |
| 94 | /* |
| 95 | * Signed integer overflow is undefined in C, so here's a helper macro |
| 96 | * to detect if the sum of two integers will overflow. |
| 97 | * |
| 98 | * Requires: a >= 0, typeof(a) equals typeof(b) |
| 99 | */ |
| 100 | #define signed_add_overflows(a, b) \ |
| 101 | ((b) > maximum_signed_value_of_type(a) - (a)) |
| 102 | |
| 103 | #define unsigned_add_overflows(a, b) \ |
| 104 | ((b) > maximum_unsigned_value_of_type(a) - (a)) |
| 105 | |
| 106 | /* |
| 107 | * Returns true if the multiplication of "a" and "b" will |
| 108 | * overflow. The types of "a" and "b" must match and must be unsigned. |
| 109 | * Note that this macro evaluates "a" twice! |
| 110 | */ |
| 111 | #define unsigned_mult_overflows(a, b) \ |
| 112 | ((a) && (b) > maximum_unsigned_value_of_type(a) / (a)) |
| 113 | |
| 114 | /* |
| 115 | * Returns true if the left shift of "a" by "shift" bits will |
| 116 | * overflow. The type of "a" must be unsigned. |
| 117 | */ |
| 118 | #define unsigned_left_shift_overflows(a, shift) \ |
| 119 | ((shift) < bitsizeof(a) && \ |
| 120 | (a) > maximum_unsigned_value_of_type(a) >> (shift)) |
| 121 | |
| 122 | #ifdef __GNUC__ |
| 123 | #define TYPEOF(x) (__typeof__(x)) |
| 124 | #else |
| 125 | #define TYPEOF(x) |
| 126 | #endif |
| 127 | |
| 128 | #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (bitsizeof(x) - (bits)))) |
| 129 | #define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */ |
| 130 | |
| 131 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
| 132 | |
| 133 | /* Approximation of the length of the decimal representation of this type. */ |
| 134 | #define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1) |
| 135 | |
| 136 | #if defined(NO_UNIX_SOCKETS) || !defined(GIT_WINDOWS_NATIVE) |
| 137 | static inline int _have_unix_sockets(void) |
| 138 | { |
| 139 | #if defined(NO_UNIX_SOCKETS) |
| 140 | return 0; |
| 141 | #else |
| 142 | return 1; |
| 143 | #endif |
| 144 | } |
| 145 | #define have_unix_sockets _have_unix_sockets |
| 146 | #endif |
| 147 | |
| 148 | /* Used by compat/win32/path-utils.h, and more */ |
| 149 | static inline int is_xplatform_dir_sep(int c) |
| 150 | { |
| 151 | return c == '/' || c == '\\'; |
| 152 | } |
| 153 | |
| 154 | #if defined(__CYGWIN__) |
| 155 | #include "compat/win32/path-utils.h" |
| 156 | #endif |
| 157 | #if defined(__MINGW32__) |
| 158 | /* pull in Windows compatibility stuff */ |
| 159 | #include "compat/win32/path-utils.h" |
| 160 | #include "compat/mingw.h" |
| 161 | #elif defined(_MSC_VER) |
| 162 | #include "compat/win32/path-utils.h" |
| 163 | #include "compat/msvc.h" |
| 164 | #endif |
| 165 | |
| 166 | /* used on Mac OS X */ |
| 167 | #ifdef PRECOMPOSE_UNICODE |
| 168 | #include "compat/precompose_utf8.h" |
| 169 | #else |
| 170 | static inline const char *precompose_argv_prefix(int argc UNUSED, |
| 171 | const char **argv UNUSED, |
| 172 | const char *prefix) |
| 173 | { |
| 174 | return prefix; |
| 175 | } |
| 176 | static inline const char *precompose_string_if_needed(const char *in) |
| 177 | { |
| 178 | return in; |
| 179 | } |
| 180 | |
| 181 | #define probe_utf8_pathname_composition() |
| 182 | #endif |
| 183 | |
| 184 | #ifndef NO_OPENSSL |
| 185 | #ifdef __APPLE__ |
| 186 | #undef __AVAILABILITY_MACROS_USES_AVAILABILITY |
| 187 | #define __AVAILABILITY_MACROS_USES_AVAILABILITY 0 |
| 188 | #include <AvailabilityMacros.h> |
| 189 | #undef DEPRECATED_ATTRIBUTE |
| 190 | #define DEPRECATED_ATTRIBUTE |
| 191 | #undef __AVAILABILITY_MACROS_USES_AVAILABILITY |
| 192 | #endif |
| 193 | #include <openssl/ssl.h> |
| 194 | #include <openssl/err.h> |
| 195 | #endif |
| 196 | |
| 197 | #ifdef HAVE_SYSINFO |
| 198 | # include <sys/sysinfo.h> |
| 199 | #endif |
| 200 | |
| 201 | #ifndef PATH_SEP |
| 202 | #define PATH_SEP ':' |
| 203 | #endif |
| 204 | |
| 205 | #ifdef HAVE_PATHS_H |
| 206 | #include <paths.h> |
| 207 | #endif |
| 208 | #ifndef _PATH_DEFPATH |
| 209 | #define _PATH_DEFPATH "/usr/local/bin:/usr/bin:/bin" |
| 210 | #endif |
| 211 | |
| 212 | #ifndef platform_core_config |
| 213 | struct config_context; |
| 214 | static inline int noop_core_config(const char *var UNUSED, |
| 215 | const char *value UNUSED, |
| 216 | const struct config_context *ctx UNUSED, |
| 217 | void *cb UNUSED) |
| 218 | { |
| 219 | return 0; |
| 220 | } |
| 221 | #define platform_core_config noop_core_config |
| 222 | #endif |
| 223 | |
| 224 | #ifndef has_dos_drive_prefix |
| 225 | static inline int git_has_dos_drive_prefix(const char *path UNUSED) |
| 226 | { |
| 227 | return 0; |
| 228 | } |
| 229 | #define has_dos_drive_prefix git_has_dos_drive_prefix |
| 230 | #endif |
| 231 | |
| 232 | #ifndef skip_dos_drive_prefix |
| 233 | static inline int git_skip_dos_drive_prefix(char **path UNUSED) |
| 234 | { |
| 235 | return 0; |
| 236 | } |
| 237 | #define skip_dos_drive_prefix git_skip_dos_drive_prefix |
| 238 | #endif |
| 239 | |
| 240 | static inline int git_is_dir_sep(int c) |
| 241 | { |
| 242 | return c == '/'; |
| 243 | } |
| 244 | #ifndef is_dir_sep |
| 245 | #define is_dir_sep git_is_dir_sep |
| 246 | #endif |
| 247 | |
| 248 | #ifndef offset_1st_component |
| 249 | static inline int git_offset_1st_component(const char *path) |
| 250 | { |
| 251 | return is_dir_sep(path[0]); |
| 252 | } |
| 253 | #define offset_1st_component git_offset_1st_component |
| 254 | #endif |
| 255 | |
| 256 | #ifndef fspathcmp |
| 257 | #define fspathcmp git_fspathcmp |
| 258 | #endif |
| 259 | |
| 260 | #ifndef fspathncmp |
| 261 | #define fspathncmp git_fspathncmp |
| 262 | #endif |
| 263 | |
| 264 | #ifndef is_valid_path |
| 265 | #define is_valid_path(path) 1 |
| 266 | #endif |
| 267 | |
| 268 | #ifndef is_path_owned_by_current_user |
| 269 | |
| 270 | #ifdef __TANDEM |
| 271 | #define ROOT_UID 65535 |
| 272 | #else |
| 273 | #define ROOT_UID 0 |
| 274 | #endif |
| 275 | |
| 276 | /* |
| 277 | * Do not use this function when |
| 278 | * (1) geteuid() did not say we are running as 'root', or |
| 279 | * (2) using this function will compromise the system. |
| 280 | * |
| 281 | * PORTABILITY WARNING: |
| 282 | * This code assumes uid_t is unsigned because that is what sudo does. |
| 283 | * If your uid_t type is signed and all your ids are positive then it |
| 284 | * should all work fine. |
| 285 | * If your version of sudo uses negative values for uid_t or it is |
| 286 | * buggy and return an overflowed value in SUDO_UID, then git might |
| 287 | * fail to grant access to your repository properly or even mistakenly |
| 288 | * grant access to someone else. |
| 289 | * In the unlikely scenario this happened to you, and that is how you |
| 290 | * got to this message, we would like to know about it; so sent us an |
| 291 | * email to git@vger.kernel.org indicating which platform you are |
| 292 | * using and which version of sudo, so we can improve this logic and |
| 293 | * maybe provide you with a patch that would prevent this issue again |
| 294 | * in the future. |
| 295 | */ |
| 296 | static inline void extract_id_from_env(const char *env, uid_t *id) |
| 297 | { |
| 298 | const char *real_uid = getenv(env); |
| 299 | |
| 300 | /* discard anything empty to avoid a more complex check below */ |
| 301 | if (real_uid && *real_uid) { |
| 302 | char *endptr = NULL; |
| 303 | unsigned long env_id; |
| 304 | |
| 305 | errno = 0; |
| 306 | /* silent overflow errors could trigger a bug here */ |
| 307 | env_id = strtoul(real_uid, &endptr, 10); |
| 308 | if (!*endptr && !errno) |
| 309 | *id = env_id; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | static inline int is_path_owned_by_current_uid(const char *path, |
| 314 | struct strbuf *report UNUSED) |
| 315 | { |
| 316 | struct stat st; |
| 317 | uid_t euid; |
| 318 | |
| 319 | if (lstat(path, &st)) |
| 320 | return 0; |
| 321 | |
| 322 | euid = geteuid(); |
| 323 | if (euid == ROOT_UID) |
| 324 | { |
| 325 | if (st.st_uid == ROOT_UID) |
| 326 | return 1; |
| 327 | else |
| 328 | extract_id_from_env("SUDO_UID", &euid); |
| 329 | } |
| 330 | |
| 331 | return st.st_uid == euid; |
| 332 | } |
| 333 | |
| 334 | #define is_path_owned_by_current_user is_path_owned_by_current_uid |
| 335 | #endif |
| 336 | |
| 337 | #ifndef find_last_dir_sep |
| 338 | #define find_last_dir_sep(path) strrchr((path), '/') |
| 339 | #endif |
| 340 | |
| 341 | #ifndef has_dir_sep |
| 342 | static inline int git_has_dir_sep(const char *path) |
| 343 | { |
| 344 | return !!strchr(path, '/'); |
| 345 | } |
| 346 | #define has_dir_sep(path) git_has_dir_sep(path) |
| 347 | #endif |
| 348 | |
| 349 | #ifndef query_user_email |
| 350 | #define query_user_email() NULL |
| 351 | #endif |
| 352 | |
| 353 | #ifdef __TANDEM |
| 354 | #include <floss.h(floss_execl,floss_execlp,floss_execv,floss_execvp)> |
| 355 | #include <floss.h(floss_getpwuid)> |
| 356 | #ifndef NSIG |
| 357 | /* |
| 358 | * NonStop NSE and NSX do not provide NSIG. SIGGUARDIAN(99) is the highest |
| 359 | * known, by detective work using kill -l as a list is all signals |
| 360 | * instead of signal.h where it should be. |
| 361 | */ |
| 362 | # define NSIG 100 |
| 363 | #endif |
| 364 | #endif |
| 365 | |
| 366 | #if defined(__HP_cc) && (__HP_cc >= 61000) |
| 367 | #define NORETURN __attribute__((noreturn)) |
| 368 | #define NORETURN_PTR |
| 369 | #elif defined(__GNUC__) && !defined(NO_NORETURN) |
| 370 | #define NORETURN __attribute__((__noreturn__)) |
| 371 | #define NORETURN_PTR __attribute__((__noreturn__)) |
| 372 | #elif defined(_MSC_VER) |
| 373 | #define NORETURN __declspec(noreturn) |
| 374 | #define NORETURN_PTR |
| 375 | #else |
| 376 | #define NORETURN |
| 377 | #define NORETURN_PTR |
| 378 | #ifndef __GNUC__ |
| 379 | #ifndef __attribute__ |
| 380 | #define __attribute__(x) |
| 381 | #endif |
| 382 | #endif |
| 383 | #endif |
| 384 | |
| 385 | /* The sentinel attribute is valid from gcc version 4.0 */ |
| 386 | #if defined(__GNUC__) && (__GNUC__ >= 4) |
| 387 | #define LAST_ARG_MUST_BE_NULL __attribute__((sentinel)) |
| 388 | /* warn_unused_result exists as of gcc 3.4.0, but be lazy and check 4.0 */ |
| 389 | #define RESULT_MUST_BE_USED __attribute__ ((warn_unused_result)) |
| 390 | #else |
| 391 | #define LAST_ARG_MUST_BE_NULL |
| 392 | #define RESULT_MUST_BE_USED |
| 393 | #endif |
| 394 | |
| 395 | /* |
| 396 | * MAYBE_UNUSED marks a function parameter that may be unused, but |
| 397 | * whose use is not an error. It also can be used to annotate a |
| 398 | * function, a variable, or a type that may be unused. |
| 399 | * |
| 400 | * Depending on a configuration, all uses of such a thing may become |
| 401 | * #ifdef'ed away. Marking it with UNUSED would give a warning in a |
| 402 | * compilation where it is indeed used, and not marking it at all |
| 403 | * would give a warning in a compilation where it is unused. In such |
| 404 | * a case, MAYBE_UNUSED is the appropriate annotation to use. |
| 405 | */ |
| 406 | #define MAYBE_UNUSED __attribute__((__unused__)) |
| 407 | |
| 408 | #include "compat/bswap.h" |
| 409 | |
| 410 | #include "wrapper.h" |
| 411 | |
| 412 | /* General helper functions */ |
| 413 | NORETURN void usage(const char *err); |
| 414 | NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 415 | NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 416 | NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 417 | int die_message(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 418 | int die_message_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 419 | int error(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 420 | int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 421 | void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 422 | void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 423 | |
| 424 | void show_usage_if_asked(int ac, const char **av, const char *err); |
| 425 | |
| 426 | NORETURN void you_still_use_that(const char *command_name, const char *hint); |
| 427 | |
| 428 | #ifndef NO_OPENSSL |
| 429 | #ifdef APPLE_COMMON_CRYPTO |
| 430 | #include "compat/apple-common-crypto.h" |
| 431 | #else |
| 432 | #include <openssl/evp.h> |
| 433 | #include <openssl/hmac.h> |
| 434 | #endif /* APPLE_COMMON_CRYPTO */ |
| 435 | #include <openssl/x509v3.h> |
| 436 | #endif /* NO_OPENSSL */ |
| 437 | |
| 438 | #ifdef HAVE_OPENSSL_CSPRNG |
| 439 | #include <openssl/rand.h> |
| 440 | #endif |
| 441 | |
| 442 | /* |
| 443 | * Let callers be aware of the constant return value; this can help |
| 444 | * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though, |
| 445 | * because other compilers may be confused by this. |
| 446 | */ |
| 447 | #if defined(__GNUC__) |
| 448 | static inline int const_error(void) |
| 449 | { |
| 450 | return -1; |
| 451 | } |
| 452 | #define error(...) (error(__VA_ARGS__), const_error()) |
| 453 | #define error_errno(...) (error_errno(__VA_ARGS__), const_error()) |
| 454 | #endif |
| 455 | |
| 456 | typedef void (*report_fn)(const char *, va_list params); |
| 457 | |
| 458 | void set_die_routine(NORETURN_PTR report_fn routine); |
| 459 | report_fn get_die_message_routine(void); |
| 460 | void set_error_routine(report_fn routine); |
| 461 | report_fn get_error_routine(void); |
| 462 | void set_warn_routine(report_fn routine); |
| 463 | report_fn get_warn_routine(void); |
| 464 | void set_die_is_recursing_routine(int (*routine)(void)); |
| 465 | |
| 466 | /* |
| 467 | * Check that an out-parameter is "at least as const as" a matching |
| 468 | * in-parameter. For example, skip_prefix() will return "out" that is a subset |
| 469 | * of "str". So: |
| 470 | * |
| 471 | * const str, const out: ok |
| 472 | * non-const str, const out: ok |
| 473 | * non-const str, non-const out: ok |
| 474 | * const str, non-const out: compile error |
| 475 | * |
| 476 | * See the skip_prefix macro below for an example of use. |
| 477 | */ |
| 478 | #define CONST_OUTPARAM(in, out) \ |
| 479 | ((const char **)(0 ? ((*(out) = (in)),(out)) : (out))) |
| 480 | |
| 481 | /* |
| 482 | * If the string "str" begins with the string found in "prefix", return true. |
| 483 | * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in |
| 484 | * the string right after the prefix). |
| 485 | * |
| 486 | * Otherwise, return false and leave "out" untouched. |
| 487 | * |
| 488 | * Examples: |
| 489 | * |
| 490 | * [extract branch name, fail if not a branch] |
| 491 | * if (!skip_prefix(ref, "refs/heads/", &branch) |
| 492 | * return -1; |
| 493 | * |
| 494 | * [skip prefix if present, otherwise use whole string] |
| 495 | * skip_prefix(name, "refs/heads/", &name); |
| 496 | */ |
| 497 | #define skip_prefix(str, prefix, out) \ |
| 498 | skip_prefix_impl((str), (prefix), CONST_OUTPARAM((str), (out))) |
| 499 | static inline bool skip_prefix_impl(const char *str, const char *prefix, |
| 500 | const char **out) |
| 501 | { |
| 502 | do { |
| 503 | if (!*prefix) { |
| 504 | *out = str; |
| 505 | return true; |
| 506 | } |
| 507 | } while (*str++ == *prefix++); |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Like skip_prefix, but promises never to read past "len" bytes of the input |
| 513 | * buffer, and returns the remaining number of bytes in "out" via "outlen". |
| 514 | */ |
| 515 | static inline bool skip_prefix_mem(const char *buf, size_t len, |
| 516 | const char *prefix, |
| 517 | const char **out, size_t *outlen) |
| 518 | { |
| 519 | size_t prefix_len = strlen(prefix); |
| 520 | if (prefix_len <= len && !memcmp(buf, prefix, prefix_len)) { |
| 521 | *out = buf + prefix_len; |
| 522 | *outlen = len - prefix_len; |
| 523 | return true; |
| 524 | } |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * If buf ends with suffix, return true and subtract the length of the suffix |
| 530 | * from *len. Otherwise, return false and leave *len untouched. |
| 531 | */ |
| 532 | static inline bool strip_suffix_mem(const char *buf, size_t *len, |
| 533 | const char *suffix) |
| 534 | { |
| 535 | size_t suflen = strlen(suffix); |
| 536 | if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen)) |
| 537 | return false; |
| 538 | *len -= suflen; |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * If str ends with suffix, return true and set *len to the size of the string |
| 544 | * without the suffix. Otherwise, return false and set *len to the size of the |
| 545 | * string. |
| 546 | * |
| 547 | * Note that we do _not_ NUL-terminate str to the new length. |
| 548 | */ |
| 549 | static inline bool strip_suffix(const char *str, const char *suffix, |
| 550 | size_t *len) |
| 551 | { |
| 552 | *len = strlen(str); |
| 553 | return strip_suffix_mem(str, len, suffix); |
| 554 | } |
| 555 | |
| 556 | #define SWAP(a, b) do { \ |
| 557 | void *_swap_a_ptr = &(a); \ |
| 558 | void *_swap_b_ptr = &(b); \ |
| 559 | unsigned char _swap_buffer[sizeof(a)]; \ |
| 560 | memcpy(_swap_buffer, _swap_a_ptr, sizeof(a)); \ |
| 561 | memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) + \ |
| 562 | BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b))); \ |
| 563 | memcpy(_swap_b_ptr, _swap_buffer, sizeof(a)); \ |
| 564 | } while (0) |
| 565 | |
| 566 | #ifdef NO_MMAP |
| 567 | |
| 568 | /* This value must be multiple of (pagesize * 2) */ |
| 569 | #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024) |
| 570 | |
| 571 | #else /* NO_MMAP */ |
| 572 | |
| 573 | /* This value must be multiple of (pagesize * 2) */ |
| 574 | #define DEFAULT_PACKED_GIT_WINDOW_SIZE \ |
| 575 | (sizeof(void*) >= 8 \ |
| 576 | ? 1 * 1024 * 1024 * 1024 \ |
| 577 | : 32 * 1024 * 1024) |
| 578 | |
| 579 | #endif /* NO_MMAP */ |
| 580 | |
| 581 | #ifdef NO_ST_BLOCKS_IN_STRUCT_STAT |
| 582 | #define on_disk_bytes(st) ((st).st_size) |
| 583 | #else |
| 584 | #define on_disk_bytes(st) ((st).st_blocks * 512) |
| 585 | #endif |
| 586 | |
| 587 | #define DEFAULT_PACKED_GIT_LIMIT \ |
| 588 | ((1024L * 1024L) * (size_t)(sizeof(void*) >= 8 ? (32 * 1024L * 1024L) : 256)) |
| 589 | |
| 590 | #ifdef _MSC_VER |
| 591 | /* |
| 592 | * When traversing into too-deep trees, Visual C-compiled Git seems to |
| 593 | * run into some internal stack overflow detection in the |
| 594 | * `RtlpAllocateHeap()` function that is called from within |
| 595 | * `git_inflate_init()`'s call tree. The following value seems to be |
| 596 | * low enough to avoid that by letting Git exit with an error before |
| 597 | * the stack overflow can occur. |
| 598 | */ |
| 599 | #define DEFAULT_MAX_ALLOWED_TREE_DEPTH 512 |
| 600 | #elif defined(GIT_WINDOWS_NATIVE) && defined(__clang__) && defined(__aarch64__) |
| 601 | /* |
| 602 | * Similar to Visual C, it seems that on Windows/ARM64 the clang-based |
| 603 | * builds have a smaller stack space available. When running out of |
| 604 | * that stack space, a `STATUS_STACK_OVERFLOW` is produced. When the |
| 605 | * Git command was run from an MSYS2 Bash, this unfortunately results |
| 606 | * in an exit code 127. Let's prevent that by lowering the maximal |
| 607 | * tree depth; This value seems to be low enough. |
| 608 | */ |
| 609 | #define DEFAULT_MAX_ALLOWED_TREE_DEPTH 1280 |
| 610 | #else |
| 611 | #define DEFAULT_MAX_ALLOWED_TREE_DEPTH 2048 |
| 612 | #endif |
| 613 | |
| 614 | int git_open_cloexec(const char *name, int flags); |
| 615 | #define git_open(name) git_open_cloexec(name, O_RDONLY) |
| 616 | |
| 617 | |
| 618 | /* |
| 619 | * Help Clang; GCC generates the same instructions for both variants on |
| 620 | * x64 and aarch64. |
| 621 | */ |
| 622 | #ifdef __clang__ |
| 623 | #define st_add_overflow __builtin_add_overflow |
| 624 | #else |
| 625 | static inline bool st_add_overflow(size_t a, size_t b, size_t *out) |
| 626 | { |
| 627 | if (unsigned_add_overflows(a, b)) |
| 628 | return true; |
| 629 | *out = a + b; |
| 630 | return false; |
| 631 | } |
| 632 | #endif |
| 633 | |
| 634 | static inline size_t st_add(size_t a, size_t b) |
| 635 | { |
| 636 | size_t result; |
| 637 | if (st_add_overflow(a, b, &result)) |
| 638 | die("size_t overflow: %"PRIuMAX" + %"PRIuMAX, |
| 639 | (uintmax_t)a, (uintmax_t)b); |
| 640 | return result; |
| 641 | } |
| 642 | #define st_add3(a,b,c) st_add(st_add((a),(b)),(c)) |
| 643 | #define st_add4(a,b,c,d) st_add(st_add3((a),(b),(c)),(d)) |
| 644 | |
| 645 | static inline size_t st_mult(size_t a, size_t b) |
| 646 | { |
| 647 | if (unsigned_mult_overflows(a, b)) |
| 648 | die("size_t overflow: %"PRIuMAX" * %"PRIuMAX, |
| 649 | (uintmax_t)a, (uintmax_t)b); |
| 650 | return a * b; |
| 651 | } |
| 652 | |
| 653 | static inline size_t st_sub(size_t a, size_t b) |
| 654 | { |
| 655 | if (a < b) |
| 656 | die("size_t underflow: %"PRIuMAX" - %"PRIuMAX, |
| 657 | (uintmax_t)a, (uintmax_t)b); |
| 658 | return a - b; |
| 659 | } |
| 660 | |
| 661 | static inline size_t st_left_shift(size_t a, unsigned shift) |
| 662 | { |
| 663 | if (unsigned_left_shift_overflows(a, shift)) |
| 664 | die("size_t overflow: %"PRIuMAX" << %u", |
| 665 | (uintmax_t)a, shift); |
| 666 | return a << shift; |
| 667 | } |
| 668 | |
| 669 | static inline unsigned long cast_size_t_to_ulong(size_t a) |
| 670 | { |
| 671 | if (a != (unsigned long)a) |
| 672 | die("object too large to read on this platform: %" |
| 673 | PRIuMAX" is cut off to %lu", |
| 674 | (uintmax_t)a, (unsigned long)a); |
| 675 | return (unsigned long)a; |
| 676 | } |
| 677 | |
| 678 | static inline uint32_t cast_size_t_to_uint32_t(size_t a) |
| 679 | { |
| 680 | if (a != (uint32_t)a) |
| 681 | die("object too large to read on this platform: %" |
| 682 | PRIuMAX" is cut off to %u", |
| 683 | (uintmax_t)a, (uint32_t)a); |
| 684 | return (uint32_t)a; |
| 685 | } |
| 686 | |
| 687 | static inline int cast_size_t_to_int(size_t a) |
| 688 | { |
| 689 | if (a > INT_MAX) |
| 690 | die("number too large to represent as int on this platform: %"PRIuMAX, |
| 691 | (uintmax_t)a); |
| 692 | return (int)a; |
| 693 | } |
| 694 | |
| 695 | static inline uint64_t u64_mult(uint64_t a, uint64_t b) |
| 696 | { |
| 697 | if (unsigned_mult_overflows(a, b)) |
| 698 | die("uint64_t overflow: %"PRIuMAX" * %"PRIuMAX, |
| 699 | (uintmax_t)a, (uintmax_t)b); |
| 700 | return a * b; |
| 701 | } |
| 702 | |
| 703 | static inline uint64_t u64_add(uint64_t a, uint64_t b) |
| 704 | { |
| 705 | if (unsigned_add_overflows(a, b)) |
| 706 | die("uint64_t overflow: %"PRIuMAX" + %"PRIuMAX, |
| 707 | (uintmax_t)a, (uintmax_t)b); |
| 708 | return a + b; |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * Limit size of IO chunks, because huge chunks only cause pain. OS X |
| 713 | * 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in |
| 714 | * the absence of bugs, large chunks can result in bad latencies when |
| 715 | * you decide to kill the process. |
| 716 | * |
| 717 | * We pick 8 MiB as our default, but if the platform defines SSIZE_MAX |
| 718 | * that is smaller than that, clip it to SSIZE_MAX, as a call to |
| 719 | * read(2) or write(2) larger than that is allowed to fail. As the last |
| 720 | * resort, we allow a port to pass via CFLAGS e.g. "-DMAX_IO_SIZE=value" |
| 721 | * to override this, if the definition of SSIZE_MAX given by the platform |
| 722 | * is broken. |
| 723 | */ |
| 724 | #ifndef MAX_IO_SIZE |
| 725 | # define MAX_IO_SIZE_DEFAULT (8*1024*1024) |
| 726 | # if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE_DEFAULT) |
| 727 | # define MAX_IO_SIZE SSIZE_MAX |
| 728 | # else |
| 729 | # define MAX_IO_SIZE MAX_IO_SIZE_DEFAULT |
| 730 | # endif |
| 731 | #endif |
| 732 | |
| 733 | /* |
| 734 | * Default buffer size for buffered I/O in index-pack, unpack-objects, |
| 735 | * and the hashfile layer in csum-file. |
| 736 | */ |
| 737 | #define DEFAULT_IO_BUFFER_SIZE (128 * 1024) |
| 738 | |
| 739 | #ifdef HAVE_ALLOCA_H |
| 740 | # include <alloca.h> |
| 741 | # define xalloca(size) (alloca(size)) |
| 742 | # define xalloca_free(p) do {} while (0) |
| 743 | #else |
| 744 | # define xalloca(size) (xmalloc(size)) |
| 745 | # define xalloca_free(p) (free(p)) |
| 746 | #endif |
| 747 | |
| 748 | /* |
| 749 | * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note |
| 750 | * that ptr is used twice, so don't pass e.g. ptr++. |
| 751 | */ |
| 752 | #define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0) |
| 753 | |
| 754 | #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc))) |
| 755 | #define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x))) |
| 756 | #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc))) |
| 757 | #define MEMZERO_ARRAY(x, alloc) memset((x), 0x0, st_mult(sizeof(*(x)), (alloc))) |
| 758 | |
| 759 | #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \ |
| 760 | BARF_UNLESS_COPYABLE((dst), (src))) |
| 761 | static inline void copy_array(void *dst, const void *src, size_t n, size_t size) |
| 762 | { |
| 763 | if (n) |
| 764 | memcpy(dst, src, st_mult(size, n)); |
| 765 | } |
| 766 | |
| 767 | #define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \ |
| 768 | BARF_UNLESS_COPYABLE((dst), (src))) |
| 769 | static inline void move_array(void *dst, const void *src, size_t n, size_t size) |
| 770 | { |
| 771 | if (n) |
| 772 | memmove(dst, src, st_mult(size, n)); |
| 773 | } |
| 774 | |
| 775 | #define DUP_ARRAY(dst, src, n) do { \ |
| 776 | size_t dup_array_n_ = (n); \ |
| 777 | COPY_ARRAY(ALLOC_ARRAY((dst), dup_array_n_), (src), dup_array_n_); \ |
| 778 | } while (0) |
| 779 | |
| 780 | /* |
| 781 | * These functions help you allocate structs with flex arrays, and copy |
| 782 | * the data directly into the array. For example, if you had: |
| 783 | * |
| 784 | * struct foo { |
| 785 | * int bar; |
| 786 | * char name[FLEX_ARRAY]; |
| 787 | * }; |
| 788 | * |
| 789 | * you can do: |
| 790 | * |
| 791 | * struct foo *f; |
| 792 | * FLEX_ALLOC_MEM(f, name, src, len); |
| 793 | * |
| 794 | * to allocate a "foo" with the contents of "src" in the "name" field. |
| 795 | * The resulting struct is automatically zero'd, and the flex-array field |
| 796 | * is NUL-terminated (whether the incoming src buffer was or not). |
| 797 | * |
| 798 | * The FLEXPTR_* variants operate on structs that don't use flex-arrays, |
| 799 | * but do want to store a pointer to some extra data in the same allocated |
| 800 | * block. For example, if you have: |
| 801 | * |
| 802 | * struct foo { |
| 803 | * char *name; |
| 804 | * int bar; |
| 805 | * }; |
| 806 | * |
| 807 | * you can do: |
| 808 | * |
| 809 | * struct foo *f; |
| 810 | * FLEXPTR_ALLOC_STR(f, name, src); |
| 811 | * |
| 812 | * and "name" will point to a block of memory after the struct, which will be |
| 813 | * freed along with the struct (but the pointer can be repointed anywhere). |
| 814 | * |
| 815 | * The *_STR variants accept a string parameter rather than a ptr/len |
| 816 | * combination. |
| 817 | * |
| 818 | * Note that these macros will evaluate the first parameter multiple |
| 819 | * times, and it must be assignable as an lvalue. |
| 820 | */ |
| 821 | #define FLEX_ALLOC_MEM(x, flexname, buf, len) do { \ |
| 822 | size_t flex_array_len_ = (len); \ |
| 823 | (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \ |
| 824 | memcpy((void *)(x)->flexname, (buf), flex_array_len_); \ |
| 825 | } while (0) |
| 826 | #define FLEXPTR_ALLOC_MEM(x, ptrname, buf, len) do { \ |
| 827 | size_t flex_array_len_ = (len); \ |
| 828 | (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \ |
| 829 | memcpy((x) + 1, (buf), flex_array_len_); \ |
| 830 | (x)->ptrname = (void *)((x)+1); \ |
| 831 | } while(0) |
| 832 | #define FLEX_ALLOC_STR(x, flexname, str) \ |
| 833 | FLEX_ALLOC_MEM((x), flexname, (str), strlen(str)) |
| 834 | #define FLEXPTR_ALLOC_STR(x, ptrname, str) \ |
| 835 | FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str)) |
| 836 | |
| 837 | #define alloc_nr(x) (((x)+16)*3/2) |
| 838 | |
| 839 | /** |
| 840 | * Dynamically growing an array using realloc() is error prone and boring. |
| 841 | * |
| 842 | * Define your array with: |
| 843 | * |
| 844 | * - a pointer (`item`) that points at the array, initialized to `NULL` |
| 845 | * (although please name the variable based on its contents, not on its |
| 846 | * type); |
| 847 | * |
| 848 | * - an integer variable (`alloc`) that keeps track of how big the current |
| 849 | * allocation is, initialized to `0`; |
| 850 | * |
| 851 | * - another integer variable (`nr`) to keep track of how many elements the |
| 852 | * array currently has, initialized to `0`. |
| 853 | * |
| 854 | * Then before adding `n`th element to the item, call `ALLOC_GROW(item, n, |
| 855 | * alloc)`. This ensures that the array can hold at least `n` elements by |
| 856 | * calling `realloc(3)` and adjusting `alloc` variable. |
| 857 | * |
| 858 | * ------------ |
| 859 | * sometype *item; |
| 860 | * size_t nr; |
| 861 | * size_t alloc |
| 862 | * |
| 863 | * for (i = 0; i < nr; i++) |
| 864 | * if (we like item[i] already) |
| 865 | * return; |
| 866 | * |
| 867 | * // we did not like any existing one, so add one |
| 868 | * ALLOC_GROW(item, nr + 1, alloc); |
| 869 | * item[nr++] = value you like; |
| 870 | * ------------ |
| 871 | * |
| 872 | * You are responsible for updating the `nr` variable. |
| 873 | * |
| 874 | * If you need to specify the number of elements to allocate explicitly |
| 875 | * then use the macro `REALLOC_ARRAY(item, alloc)` instead of `ALLOC_GROW`. |
| 876 | * |
| 877 | * Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some |
| 878 | * added niceties. |
| 879 | * |
| 880 | * DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'. |
| 881 | */ |
| 882 | #define ALLOC_GROW(x, nr, alloc) \ |
| 883 | do { \ |
| 884 | if ((nr) > alloc) { \ |
| 885 | if (alloc_nr(alloc) < (nr)) \ |
| 886 | alloc = (nr); \ |
| 887 | else \ |
| 888 | alloc = alloc_nr(alloc); \ |
| 889 | REALLOC_ARRAY(x, alloc); \ |
| 890 | } \ |
| 891 | } while (0) |
| 892 | |
| 893 | /* |
| 894 | * Similar to ALLOC_GROW but handles updating of the nr value and |
| 895 | * zeroing the bytes of the newly-grown array elements. |
| 896 | * |
| 897 | * DO NOT USE any expression with side-effect for any of the |
| 898 | * arguments. |
| 899 | */ |
| 900 | #define ALLOC_GROW_BY(x, nr, increase, alloc) \ |
| 901 | do { \ |
| 902 | if (increase) { \ |
| 903 | size_t new_nr = nr + (increase); \ |
| 904 | if (new_nr < nr) \ |
| 905 | BUG("negative growth in ALLOC_GROW_BY"); \ |
| 906 | ALLOC_GROW(x, new_nr, alloc); \ |
| 907 | memset((x) + nr, 0, sizeof(*(x)) * (increase)); \ |
| 908 | nr = new_nr; \ |
| 909 | } \ |
| 910 | } while (0) |
| 911 | |
| 912 | static inline char *xstrdup_or_null(const char *str) |
| 913 | { |
| 914 | return str ? xstrdup(str) : NULL; |
| 915 | } |
| 916 | |
| 917 | static inline size_t xsize_t(off_t len) |
| 918 | { |
| 919 | if (len < 0 || (uintmax_t) len > SIZE_MAX) |
| 920 | die("Cannot handle files this big"); |
| 921 | return (size_t) len; |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * Like skip_prefix, but compare case-insensitively. Note that the comparison |
| 926 | * is done via tolower(), so it is strictly ASCII (no multi-byte characters or |
| 927 | * locale-specific conversions). |
| 928 | */ |
| 929 | #define skip_iprefix(str, prefix, out) \ |
| 930 | skip_iprefix_impl((str), (prefix), CONST_OUTPARAM((str), (out))) |
| 931 | static inline bool skip_iprefix_impl(const char *str, const char *prefix, |
| 932 | const char **out) |
| 933 | { |
| 934 | do { |
| 935 | if (!*prefix) { |
| 936 | *out = str; |
| 937 | return true; |
| 938 | } |
| 939 | } while (tolower(*str++) == tolower(*prefix++)); |
| 940 | return false; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Like skip_prefix_mem, but compare case-insensitively. Note that the |
| 945 | * comparison is done via tolower(), so it is strictly ASCII (no multi-byte |
| 946 | * characters or locale-specific conversions). |
| 947 | */ |
| 948 | static inline bool skip_iprefix_mem(const char *buf, size_t len, |
| 949 | const char *prefix, |
| 950 | const char **out, size_t *outlen) |
| 951 | { |
| 952 | do { |
| 953 | if (!*prefix) { |
| 954 | *out = buf; |
| 955 | *outlen = len; |
| 956 | return true; |
| 957 | } |
| 958 | } while (len-- > 0 && tolower(*buf++) == tolower(*prefix++)); |
| 959 | return false; |
| 960 | } |
| 961 | |
| 962 | static inline int strtoul_ui(char const *s, int base, unsigned int *result) |
| 963 | { |
| 964 | unsigned long ul; |
| 965 | char *p; |
| 966 | |
| 967 | errno = 0; |
| 968 | /* negative values would be accepted by strtoul */ |
| 969 | if (strchr(s, '-')) |
| 970 | return -1; |
| 971 | ul = strtoul(s, &p, base); |
| 972 | if (errno || *p || p == s || (unsigned int) ul != ul) |
| 973 | return -1; |
| 974 | *result = ul; |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | static inline int strtol_i(char const *s, int base, int *result) |
| 979 | { |
| 980 | long ul; |
| 981 | char *p; |
| 982 | |
| 983 | errno = 0; |
| 984 | ul = strtol(s, &p, base); |
| 985 | if (errno || *p || p == s || (int) ul != ul) |
| 986 | return -1; |
| 987 | *result = ul; |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | #ifndef REG_STARTEND |
| 992 | #error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd" |
| 993 | #endif |
| 994 | |
| 995 | static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size, |
| 996 | size_t nmatch, regmatch_t pmatch[], int eflags) |
| 997 | { |
| 998 | assert(nmatch > 0 && pmatch); |
| 999 | pmatch[0].rm_so = 0; |
| 1000 | pmatch[0].rm_eo = size; |
| 1001 | return regexec(preg, buf, nmatch, pmatch, eflags | REG_STARTEND); |
| 1002 | } |
| 1003 | |
| 1004 | #ifdef USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS |
| 1005 | int git_regcomp(regex_t *preg, const char *pattern, int cflags); |
| 1006 | #define regcomp git_regcomp |
| 1007 | #endif |
| 1008 | |
| 1009 | #ifndef DIR_HAS_BSD_GROUP_SEMANTICS |
| 1010 | # define FORCE_DIR_SET_GID S_ISGID |
| 1011 | #else |
| 1012 | # define FORCE_DIR_SET_GID 0 |
| 1013 | #endif |
| 1014 | |
| 1015 | #ifdef UNRELIABLE_FSTAT |
| 1016 | #define fstat_is_reliable() 0 |
| 1017 | #else |
| 1018 | #define fstat_is_reliable() 1 |
| 1019 | #endif |
| 1020 | |
| 1021 | /* usage.c: only to be used for testing BUG() implementation (see test-tool) */ |
| 1022 | extern int BUG_exit_code; |
| 1023 | |
| 1024 | /* usage.c: if bug() is called we should have a BUG_if_bug() afterwards */ |
| 1025 | extern int bug_called_must_BUG; |
| 1026 | |
| 1027 | __attribute__((format (printf, 3, 4))) NORETURN |
| 1028 | void BUG_fl(const char *file, int line, const char *fmt, ...); |
| 1029 | #define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__) |
| 1030 | /* ASSERT: like assert(), but won't be compiled out with NDEBUG */ |
| 1031 | #define ASSERT(a) if (!(a)) BUG("Assertion `" #a "' failed.") |
| 1032 | __attribute__((format (printf, 3, 4))) |
| 1033 | void bug_fl(const char *file, int line, const char *fmt, ...); |
| 1034 | #define bug(...) bug_fl(__FILE__, __LINE__, __VA_ARGS__) |
| 1035 | #define BUG_if_bug(...) do { \ |
| 1036 | if (bug_called_must_BUG) \ |
| 1037 | BUG_fl(__FILE__, __LINE__, __VA_ARGS__); \ |
| 1038 | } while (0) |
| 1039 | |
| 1040 | #ifndef FSYNC_METHOD_DEFAULT |
| 1041 | #ifdef __APPLE__ |
| 1042 | #define FSYNC_METHOD_DEFAULT FSYNC_METHOD_WRITEOUT_ONLY |
| 1043 | #else |
| 1044 | #define FSYNC_METHOD_DEFAULT FSYNC_METHOD_FSYNC |
| 1045 | #endif |
| 1046 | #endif |
| 1047 | |
| 1048 | #ifndef SHELL_PATH |
| 1049 | # define SHELL_PATH "/bin/sh" |
| 1050 | #endif |
| 1051 | |
| 1052 | /* |
| 1053 | * Our code often opens a path to an optional file, to work on its |
| 1054 | * contents when we can successfully open it. We can ignore a failure |
| 1055 | * to open if such an optional file does not exist, but we do want to |
| 1056 | * report a failure in opening for other reasons (e.g. we got an I/O |
| 1057 | * error, or the file is there, but we lack the permission to open). |
| 1058 | * |
| 1059 | * Call this function after seeing an error from open() or fopen() to |
| 1060 | * see if the errno indicates a missing file that we can safely ignore. |
| 1061 | */ |
| 1062 | static inline int is_missing_file_error(int errno_) |
| 1063 | { |
| 1064 | return (errno_ == ENOENT || errno_ == ENOTDIR); |
| 1065 | } |
| 1066 | |
| 1067 | int cmd_main(int, const char **); |
| 1068 | |
| 1069 | /* |
| 1070 | * Intercept all calls to exit() and route them to trace2 to |
| 1071 | * optionally emit a message before calling the real exit(). |
| 1072 | */ |
| 1073 | int common_exit(const char *file, int line, int code); |
| 1074 | #define exit(code) exit(common_exit(__FILE__, __LINE__, (code))) |
| 1075 | |
| 1076 | /* |
| 1077 | * This include must come after system headers, since it introduces macros that |
| 1078 | * replace system names. |
| 1079 | */ |
| 1080 | #include "banned.h" |
| 1081 | |
| 1082 | /* |
| 1083 | * container_of - Get the address of an object containing a field. |
| 1084 | * |
| 1085 | * @ptr: pointer to the field. |
| 1086 | * @type: type of the object. |
| 1087 | * @member: name of the field within the object. |
| 1088 | */ |
| 1089 | #define container_of(ptr, type, member) \ |
| 1090 | ((type *) ((char *)(ptr) - offsetof(type, member))) |
| 1091 | |
| 1092 | /* |
| 1093 | * helper function for `container_of_or_null' to avoid multiple |
| 1094 | * evaluation of @ptr |
| 1095 | */ |
| 1096 | static inline void *container_of_or_null_offset(void *ptr, size_t offset) |
| 1097 | { |
| 1098 | return ptr ? (char *)ptr - offset : NULL; |
| 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * like `container_of', but allows returned value to be NULL |
| 1103 | */ |
| 1104 | #define container_of_or_null(ptr, type, member) \ |
| 1105 | (type *)container_of_or_null_offset(ptr, offsetof(type, member)) |
| 1106 | |
| 1107 | /* |
| 1108 | * like offsetof(), but takes a pointer to a variable of type which |
| 1109 | * contains @member, instead of a specified type. |
| 1110 | * @ptr is subject to multiple evaluation since we can't rely on __typeof__ |
| 1111 | * everywhere. |
| 1112 | */ |
| 1113 | #if defined(__GNUC__) /* clang sets this, too */ |
| 1114 | #define OFFSETOF_VAR(ptr, member) offsetof(__typeof__(*ptr), member) |
| 1115 | #else /* !__GNUC__ */ |
| 1116 | #define OFFSETOF_VAR(ptr, member) \ |
| 1117 | ((uintptr_t)&(ptr)->member - (uintptr_t)(ptr)) |
| 1118 | #endif /* !__GNUC__ */ |
| 1119 | |
| 1120 | /* |
| 1121 | * Prevent an overly clever compiler from optimizing an expression |
| 1122 | * out, triggering a false positive when building with the |
| 1123 | * -Wunreachable-code option. false_but_the_compiler_does_not_know_it_ |
| 1124 | * is defined in a compilation unit separate from where the macro is |
| 1125 | * used, initialized to 0, and never modified. |
| 1126 | */ |
| 1127 | #define NOT_CONSTANT(expr) ((expr) || false_but_the_compiler_does_not_know_it_) |
| 1128 | extern int false_but_the_compiler_does_not_know_it_; |
| 1129 | |
| 1130 | #ifdef CHECK_ASSERTION_SIDE_EFFECTS |
| 1131 | #undef assert |
| 1132 | extern int not_supposed_to_survive; |
| 1133 | #define assert(expr) ((void)(not_supposed_to_survive || (expr))) |
| 1134 | #endif /* CHECK_ASSERTION_SIDE_EFFECTS */ |
| 1135 | |
| 1136 | #endif |
| 1137 | |
| 1138 | #ifdef DISABLE_SIGN_COMPARE_WARNINGS |
| 1139 | DISABLE_WARNING(-Wsign-compare) |
| 1140 | #endif |