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