err_local.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 1995-2023 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. #include <string.h>
  10. #include <openssl/err.h>
  11. #include <openssl/e_os2.h>
  12. static ossl_inline void err_get_slot(ERR_STATE *es)
  13. {
  14. es->top = (es->top + 1) % ERR_NUM_ERRORS;
  15. if (es->top == es->bottom)
  16. es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
  17. }
  18. static ossl_inline void err_clear_data(ERR_STATE *es, size_t i, int deall)
  19. {
  20. if (es->err_data_flags[i] & ERR_TXT_MALLOCED) {
  21. if (deall) {
  22. OPENSSL_free(es->err_data[i]);
  23. es->err_data[i] = NULL;
  24. es->err_data_size[i] = 0;
  25. es->err_data_flags[i] = 0;
  26. } else if (es->err_data[i] != NULL) {
  27. es->err_data[i][0] = '\0';
  28. es->err_data_flags[i] = ERR_TXT_MALLOCED;
  29. }
  30. } else {
  31. es->err_data[i] = NULL;
  32. es->err_data_size[i] = 0;
  33. es->err_data_flags[i] = 0;
  34. }
  35. }
  36. static ossl_inline void err_set_error(ERR_STATE *es, size_t i,
  37. int lib, int reason)
  38. {
  39. es->err_buffer[i] =
  40. lib == ERR_LIB_SYS
  41. ? (unsigned int)(ERR_SYSTEM_FLAG | reason)
  42. : ERR_PACK(lib, 0, reason);
  43. }
  44. static ossl_inline void err_set_debug(ERR_STATE *es, size_t i,
  45. const char *file, int line,
  46. const char *fn)
  47. {
  48. /*
  49. * We dup the file and fn strings because they may be provider owned. If the
  50. * provider gets unloaded, they may not be valid anymore.
  51. */
  52. OPENSSL_free(es->err_file[i]);
  53. if (file == NULL || file[0] == '\0')
  54. es->err_file[i] = NULL;
  55. else if ((es->err_file[i] = CRYPTO_malloc(strlen(file) + 1,
  56. NULL, 0)) != NULL)
  57. /* We cannot use OPENSSL_strdup due to possible recursion */
  58. strcpy(es->err_file[i], file);
  59. es->err_line[i] = line;
  60. OPENSSL_free(es->err_func[i]);
  61. if (fn == NULL || fn[0] == '\0')
  62. es->err_func[i] = NULL;
  63. else if ((es->err_func[i] = CRYPTO_malloc(strlen(fn) + 1,
  64. NULL, 0)) != NULL)
  65. strcpy(es->err_func[i], fn);
  66. }
  67. static ossl_inline void err_set_data(ERR_STATE *es, size_t i,
  68. void *data, size_t datasz, int flags)
  69. {
  70. if ((es->err_data_flags[i] & ERR_TXT_MALLOCED) != 0)
  71. OPENSSL_free(es->err_data[i]);
  72. es->err_data[i] = data;
  73. es->err_data_size[i] = datasz;
  74. es->err_data_flags[i] = flags;
  75. }
  76. static ossl_inline void err_clear(ERR_STATE *es, size_t i, int deall)
  77. {
  78. err_clear_data(es, i, (deall));
  79. es->err_marks[i] = 0;
  80. es->err_flags[i] = 0;
  81. es->err_buffer[i] = 0;
  82. es->err_line[i] = -1;
  83. OPENSSL_free(es->err_file[i]);
  84. es->err_file[i] = NULL;
  85. OPENSSL_free(es->err_func[i]);
  86. es->err_func[i] = NULL;
  87. }
  88. ERR_STATE *ossl_err_get_state_int(void);
  89. void ossl_err_string_int(unsigned long e, const char *func,
  90. char *buf, size_t len);