Coverage Report

Created: 2026-02-16 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/ex_data.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/ex_data.h>
16
17
#include <assert.h>
18
#include <limits.h>
19
#include <stdlib.h>
20
#include <string.h>
21
22
#include <openssl/crypto.h>
23
#include <openssl/err.h>
24
#include <openssl/mem.h>
25
26
#include "internal.h"
27
#include "mem_internal.h"
28
29
30
BSSL_NAMESPACE_BEGIN
31
32
struct crypto_ex_data_func_st {
33
  long argl;   // Arbitrary long
34
  void *argp;  // Arbitrary void pointer
35
  CRYPTO_EX_free *free_func;
36
  // next points to the next |CRYPTO_EX_DATA_FUNCS| or NULL if this is the last
37
  // one. It may only be read if synchronized with a read from |num_funcs|.
38
  CRYPTO_EX_DATA_FUNCS *next;
39
};
40
41
int CRYPTO_get_ex_new_index_ex(CRYPTO_EX_DATA_CLASS *ex_data_class, long argl,
42
0
                               void *argp, CRYPTO_EX_free *free_func) {
43
0
  CRYPTO_EX_DATA_FUNCS *funcs = New<CRYPTO_EX_DATA_FUNCS>();
44
0
  if (funcs == nullptr) {
45
0
    return -1;
46
0
  }
47
48
0
  funcs->argl = argl;
49
0
  funcs->argp = argp;
50
0
  funcs->free_func = free_func;
51
0
  funcs->next = nullptr;
52
53
0
  CRYPTO_MUTEX_lock_write(&ex_data_class->lock);
54
55
0
  uint32_t num_funcs = ex_data_class->num_funcs.load();
56
  // The index must fit in |int|.
57
0
  if (num_funcs > (size_t)(INT_MAX - ex_data_class->num_reserved)) {
58
0
    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
59
0
    CRYPTO_MUTEX_unlock_write(&ex_data_class->lock);
60
0
    return -1;
61
0
  }
62
63
  // Append |funcs| to the linked list.
64
0
  if (ex_data_class->last == nullptr) {
65
0
    assert(num_funcs == 0);
66
0
    ex_data_class->funcs = funcs;
67
0
    ex_data_class->last = funcs;
68
0
  } else {
69
0
    ex_data_class->last->next = funcs;
70
0
    ex_data_class->last = funcs;
71
0
  }
72
73
0
  ex_data_class->num_funcs.store(num_funcs + 1);
74
0
  CRYPTO_MUTEX_unlock_write(&ex_data_class->lock);
75
0
  return (int)num_funcs + ex_data_class->num_reserved;
76
0
}
77
78
7.47k
int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val) {
79
7.47k
  if (index < 0) {
80
    // A caller that can accidentally pass in an invalid index into this
81
    // function will hit an memory error if |index| happened to be valid, and
82
    // expected |val| to be of a different type.
83
0
    abort();
84
0
  }
85
86
7.47k
  if (ad->sk == nullptr) {
87
7.47k
    ad->sk = sk_void_new_null();
88
7.47k
    if (ad->sk == nullptr) {
89
0
      return 0;
90
0
    }
91
7.47k
  }
92
93
  // Add NULL values until the stack is long enough.
94
14.9k
  for (size_t i = sk_void_num(ad->sk); i <= (size_t)index; i++) {
95
7.47k
    if (!sk_void_push(ad->sk, nullptr)) {
96
0
      return 0;
97
0
    }
98
7.47k
  }
99
100
7.47k
  sk_void_set(ad->sk, (size_t)index, val);
101
7.47k
  return 1;
102
7.47k
}
103
104
0
void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx) {
105
0
  if (ad->sk == nullptr || idx < 0 || (size_t)idx >= sk_void_num(ad->sk)) {
106
0
    return nullptr;
107
0
  }
108
0
  return sk_void_value(ad->sk, idx);
109
0
}
110
111
1.00M
void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad) { ad->sk = nullptr; }
112
113
void CRYPTO_free_ex_data(CRYPTO_EX_DATA_CLASS *ex_data_class,
114
1.01M
                         CRYPTO_EX_DATA *ad) {
115
1.01M
  if (ad->sk == nullptr) {
116
    // Nothing to do.
117
1.00M
    return;
118
1.00M
  }
119
120
7.47k
  uint32_t num_funcs = ex_data_class->num_funcs.load();
121
  // |CRYPTO_get_ex_new_index_ex| will not allocate indices beyond |INT_MAX|.
122
7.47k
  assert(num_funcs <= (size_t)(INT_MAX - ex_data_class->num_reserved));
123
124
  // Defer dereferencing |ex_data_class->funcs| and |funcs->next|. It must come
125
  // after the |num_funcs| comparison to be correctly synchronized.
126
7.47k
  CRYPTO_EX_DATA_FUNCS *const *funcs = &ex_data_class->funcs;
127
7.47k
  for (uint32_t i = 0; i < num_funcs; i++) {
128
0
    if ((*funcs)->free_func != nullptr) {
129
0
      int index = (int)i + ex_data_class->num_reserved;
130
0
      void *ptr = CRYPTO_get_ex_data(ad, index);
131
0
      (*funcs)->free_func(/*parent=*/nullptr, ptr, /*ad*/ nullptr, index,
132
0
                          (*funcs)->argl, (*funcs)->argp);
133
0
    }
134
0
    funcs = &(*funcs)->next;
135
0
  }
136
137
7.47k
  sk_void_free(ad->sk);
138
7.47k
  ad->sk = nullptr;
139
7.47k
}
140
141
BSSL_NAMESPACE_END
142
143
0
void CRYPTO_cleanup_all_ex_data() {}