THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
6.4KB

  1. /*
  2. * GIT - The information manager from hell
  3. *
  4. * Copyright (C) Linus Torvalds, 2005
  5. */
  6. #include "git-compat-util.h"
  7. #include "cache.h"
  8. void vreportf(const char *prefix, const char *err, va_list params)
  9. {
  10. char msg[4096];
  11. char *p;
  12. vsnprintf(msg, sizeof(msg), err, params);
  13. for (p = msg; *p; p++) {
  14. if (iscntrl(*p) && *p != '\t' && *p != '\n')
  15. *p = '?';
  16. }
  17. fprintf(stderr, "%s%s\n", prefix, msg);
  18. }
  19. static NORETURN void usage_builtin(const char *err, va_list params)
  20. {
  21. vreportf("usage: ", err, params);
  22. /*
  23. * When we detect a usage error *before* the command dispatch in
  24. * cmd_main(), we don't know what verb to report. Force it to this
  25. * to facilitate post-processing.
  26. */
  27. trace2_cmd_name("_usage_");
  28. /*
  29. * Currently, the (err, params) are usually just the static usage
  30. * string which isn't very useful here. Usually, the call site
  31. * manually calls fprintf(stderr,...) with the actual detailed
  32. * syntax error before calling usage().
  33. *
  34. * TODO It would be nice to update the call sites to pass both
  35. * the static usage string and the detailed error message.
  36. */
  37. exit(129);
  38. }
  39. static NORETURN void die_builtin(const char *err, va_list params)
  40. {
  41. /*
  42. * We call this trace2 function first and expect it to va_copy 'params'
  43. * before using it (because an 'ap' can only be walked once).
  44. */
  45. trace2_cmd_error_va(err, params);
  46. vreportf("fatal: ", err, params);
  47. exit(128);
  48. }
  49. static void error_builtin(const char *err, va_list params)
  50. {
  51. /*
  52. * We call this trace2 function first and expect it to va_copy 'params'
  53. * before using it (because an 'ap' can only be walked once).
  54. */
  55. trace2_cmd_error_va(err, params);
  56. vreportf("error: ", err, params);
  57. }
  58. static void warn_builtin(const char *warn, va_list params)
  59. {
  60. vreportf("warning: ", warn, params);
  61. }
  62. static int die_is_recursing_builtin(void)
  63. {
  64. static int dying;
  65. /*
  66. * Just an arbitrary number X where "a < x < b" where "a" is
  67. * "maximum number of pthreads we'll ever plausibly spawn" and
  68. * "b" is "something less than Inf", since the point is to
  69. * prevent infinite recursion.
  70. */
  71. static const int recursion_limit = 1024;
  72. dying++;
  73. if (dying > recursion_limit) {
  74. return 1;
  75. } else if (dying == 2) {
  76. warning("die() called many times. Recursion error or racy threaded death!");
  77. return 0;
  78. } else {
  79. return 0;
  80. }
  81. }
  82. /* If we are in a dlopen()ed .so write to a global variable would segfault
  83. * (ugh), so keep things static. */
  84. static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
  85. static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
  86. static void (*error_routine)(const char *err, va_list params) = error_builtin;
  87. static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
  88. static int (*die_is_recursing)(void) = die_is_recursing_builtin;
  89. void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
  90. {
  91. die_routine = routine;
  92. }
  93. void set_error_routine(void (*routine)(const char *err, va_list params))
  94. {
  95. error_routine = routine;
  96. }
  97. void (*get_error_routine(void))(const char *err, va_list params)
  98. {
  99. return error_routine;
  100. }
  101. void set_warn_routine(void (*routine)(const char *warn, va_list params))
  102. {
  103. warn_routine = routine;
  104. }
  105. void (*get_warn_routine(void))(const char *warn, va_list params)
  106. {
  107. return warn_routine;
  108. }
  109. void set_die_is_recursing_routine(int (*routine)(void))
  110. {
  111. die_is_recursing = routine;
  112. }
  113. void NORETURN usagef(const char *err, ...)
  114. {
  115. va_list params;
  116. va_start(params, err);
  117. usage_routine(err, params);
  118. va_end(params);
  119. }
  120. void NORETURN usage(const char *err)
  121. {
  122. usagef("%s", err);
  123. }
  124. void NORETURN die(const char *err, ...)
  125. {
  126. va_list params;
  127. if (die_is_recursing()) {
  128. fputs("fatal: recursion detected in die handler\n", stderr);
  129. exit(128);
  130. }
  131. va_start(params, err);
  132. die_routine(err, params);
  133. va_end(params);
  134. }
  135. static const char *fmt_with_err(char *buf, int n, const char *fmt)
  136. {
  137. char str_error[256], *err;
  138. int i, j;
  139. err = strerror(errno);
  140. for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
  141. if ((str_error[j++] = err[i++]) != '%')
  142. continue;
  143. if (j < sizeof(str_error) - 1) {
  144. str_error[j++] = '%';
  145. } else {
  146. /* No room to double the '%', so we overwrite it with
  147. * '\0' below */
  148. j--;
  149. break;
  150. }
  151. }
  152. str_error[j] = 0;
  153. /* Truncation is acceptable here */
  154. snprintf(buf, n, "%s: %s", fmt, str_error);
  155. return buf;
  156. }
  157. void NORETURN die_errno(const char *fmt, ...)
  158. {
  159. char buf[1024];
  160. va_list params;
  161. if (die_is_recursing()) {
  162. fputs("fatal: recursion detected in die_errno handler\n",
  163. stderr);
  164. exit(128);
  165. }
  166. va_start(params, fmt);
  167. die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
  168. va_end(params);
  169. }
  170. #undef error_errno
  171. int error_errno(const char *fmt, ...)
  172. {
  173. char buf[1024];
  174. va_list params;
  175. va_start(params, fmt);
  176. error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
  177. va_end(params);
  178. return -1;
  179. }
  180. #undef error
  181. int error(const char *err, ...)
  182. {
  183. va_list params;
  184. va_start(params, err);
  185. error_routine(err, params);
  186. va_end(params);
  187. return -1;
  188. }
  189. void warning_errno(const char *warn, ...)
  190. {
  191. char buf[1024];
  192. va_list params;
  193. va_start(params, warn);
  194. warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
  195. va_end(params);
  196. }
  197. void warning(const char *warn, ...)
  198. {
  199. va_list params;
  200. va_start(params, warn);
  201. warn_routine(warn, params);
  202. va_end(params);
  203. }
  204. /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
  205. int BUG_exit_code;
  206. static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
  207. {
  208. char prefix[256];
  209. /* truncation via snprintf is OK here */
  210. if (file)
  211. snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
  212. else
  213. snprintf(prefix, sizeof(prefix), "BUG: ");
  214. vreportf(prefix, fmt, params);
  215. if (BUG_exit_code)
  216. exit(BUG_exit_code);
  217. abort();
  218. }
  219. #ifdef HAVE_VARIADIC_MACROS
  220. NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
  221. {
  222. va_list ap;
  223. va_start(ap, fmt);
  224. BUG_vfl(file, line, fmt, ap);
  225. va_end(ap);
  226. }
  227. #else
  228. NORETURN void BUG(const char *fmt, ...)
  229. {
  230. va_list ap;
  231. va_start(ap, fmt);
  232. BUG_vfl(NULL, 0, fmt, ap);
  233. va_end(ap);
  234. }
  235. #endif
  236. #ifdef SUPPRESS_ANNOTATED_LEAKS
  237. void unleak_memory(const void *ptr, size_t len)
  238. {
  239. static struct suppressed_leak_root {
  240. struct suppressed_leak_root *next;
  241. char data[FLEX_ARRAY];
  242. } *suppressed_leaks;
  243. struct suppressed_leak_root *root;
  244. FLEX_ALLOC_MEM(root, data, ptr, len);
  245. root->next = suppressed_leaks;
  246. suppressed_leaks = root;
  247. }
  248. #endif