ngtcp2_unreachable.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2022 ngtcp2 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "ngtcp2_unreachable.h"
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <stdlib.h>
  29. #ifdef HAVE_UNISTD_H
  30. # define NGTCP2_UNREACHABLE_LOG
  31. # include <unistd.h>
  32. #elif defined(WIN32)
  33. # define NGTCP2_UNREACHABLE_LOG
  34. # include <io.h>
  35. #endif /* defined(WIN32) */
  36. void ngtcp2_unreachable_fail(const char *file, int line, const char *func) {
  37. #ifdef NGTCP2_UNREACHABLE_LOG
  38. char *buf;
  39. size_t buflen;
  40. int rv;
  41. # define NGTCP2_UNREACHABLE_TEMPLATE "%s:%d %s: Unreachable.\n"
  42. rv = snprintf(NULL, 0, NGTCP2_UNREACHABLE_TEMPLATE, file, line, func);
  43. if (rv < 0) {
  44. abort();
  45. }
  46. /* here we explicitly use system malloc */
  47. buflen = (size_t)rv + 1;
  48. buf = malloc(buflen);
  49. if (buf == NULL) {
  50. abort();
  51. }
  52. rv = snprintf(buf, buflen, NGTCP2_UNREACHABLE_TEMPLATE, file, line, func);
  53. if (rv < 0) {
  54. abort();
  55. }
  56. # ifndef WIN32
  57. while (write(STDERR_FILENO, buf, (size_t)rv) == -1 && errno == EINTR)
  58. ;
  59. # else /* defined(WIN32) */
  60. _write(_fileno(stderr), buf, (unsigned int)rv);
  61. # endif /* defined(WIN32) */
  62. free(buf);
  63. #endif /* defined(NGTCP2_UNREACHABLE_LOG) */
  64. abort();
  65. }