err_blocks.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* TODO: When ERR_STATE becomes opaque, this musts be removed */
  10. #define OSSL_FORCE_ERR_STATE
  11. #include <string.h>
  12. #include <openssl/err.h>
  13. #include "err_local.h"
  14. void ERR_new(void)
  15. {
  16. ERR_STATE *es;
  17. es = ossl_err_get_state_int();
  18. if (es == NULL)
  19. return;
  20. /* Allocate a slot */
  21. err_get_slot(es);
  22. err_clear(es, es->top, 0);
  23. }
  24. void ERR_set_debug(const char *file, int line, const char *func)
  25. {
  26. ERR_STATE *es;
  27. es = ossl_err_get_state_int();
  28. if (es == NULL)
  29. return;
  30. err_set_debug(es, es->top, file, line, func);
  31. }
  32. void ERR_set_error(int lib, int reason, const char *fmt, ...)
  33. {
  34. va_list args;
  35. va_start(args, fmt);
  36. ERR_vset_error(lib, reason, fmt, args);
  37. va_end(args);
  38. }
  39. void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
  40. {
  41. ERR_STATE *es;
  42. char *buf = NULL;
  43. size_t buf_size = 0;
  44. unsigned long flags = 0;
  45. size_t i;
  46. es = ossl_err_get_state_int();
  47. if (es == NULL)
  48. return;
  49. i = es->top;
  50. if (fmt != NULL) {
  51. int printed_len = 0;
  52. char *rbuf = NULL;
  53. buf = es->err_data[i];
  54. buf_size = es->err_data_size[i];
  55. /*
  56. * To protect the string we just grabbed from tampering by other
  57. * functions we may call, or to protect them from freeing a pointer
  58. * that may no longer be valid at that point, we clear away the
  59. * data pointer and the flags. We will set them again at the end
  60. * of this function.
  61. */
  62. es->err_data[i] = NULL;
  63. es->err_data_flags[i] = 0;
  64. /*
  65. * Try to maximize the space available. If that fails, we use what
  66. * we have.
  67. */
  68. if (buf_size < ERR_MAX_DATA_SIZE
  69. && (rbuf = OPENSSL_realloc(buf, ERR_MAX_DATA_SIZE)) != NULL) {
  70. buf = rbuf;
  71. buf_size = ERR_MAX_DATA_SIZE;
  72. }
  73. if (buf != NULL) {
  74. printed_len = BIO_vsnprintf(buf, buf_size, fmt, args);
  75. }
  76. if (printed_len < 0)
  77. printed_len = 0;
  78. if (buf != NULL)
  79. buf[printed_len] = '\0';
  80. /*
  81. * Try to reduce the size, but only if we maximized above. If that
  82. * fails, we keep what we have.
  83. * (According to documentation, realloc leaves the old buffer untouched
  84. * if it fails)
  85. */
  86. if ((rbuf = OPENSSL_realloc(buf, printed_len + 1)) != NULL) {
  87. buf = rbuf;
  88. buf_size = printed_len + 1;
  89. buf[printed_len] = '\0';
  90. }
  91. if (buf != NULL)
  92. flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
  93. }
  94. err_clear_data(es, es->top, 0);
  95. err_set_error(es, es->top, lib, reason);
  96. if (fmt != NULL)
  97. err_set_data(es, es->top, buf, buf_size, flags);
  98. }