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.

308 lines
6.7KB

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