err_blocks.c 3.0 KB

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