Coverage Report

Created: 2026-05-11 06:45

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 ExDataFuncs {
33
  long argl;   // Arbitrary long
34
  void *argp;  // Arbitrary void pointer
35
  CRYPTO_EX_free *free_func;
36
  // next points to the next |ExDataFuncs| or NULL if this is the last
37
  // one. It may only be read if synchronized with a read from |num_funcs|.
38
  ExDataFuncs *next;
39
};
40
41
int CRYPTO_get_ex_new_index_ex(ExDataClass *ex_data_class, long argl,
42
0
                               void *argp, CRYPTO_EX_free *free_func) {
43
0
  ExDataFuncs *funcs = New<ExDataFuncs>();
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
  MutexWriteLock lock(&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
    return -1;
60
0
  }
61
62
  // Append |funcs| to the linked list.
63
0
  if (ex_data_class->last == nullptr) {
64
0
    assert(num_funcs == 0);
65
0
    ex_data_class->funcs = funcs;
66
0
    ex_data_class->last = funcs;
67
0
  } else {
68
0
    ex_data_class->last->next = funcs;
69
0
    ex_data_class->last = funcs;
70
0
  }
71
72
0
  ex_data_class->num_funcs.store(num_funcs + 1);
73
0
  return (int)num_funcs + ex_data_class->num_reserved;
74
0
}
75
76
8.14k
int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int index, void *val) {
77
8.14k
  if (index < 0) {
78
    // A caller that can accidentally pass in an invalid index into this
79
    // function will hit an memory error if |index| happened to be valid, and
80
    // expected |val| to be of a different type.
81
0
    abort();
82
0
  }
83
84
8.14k
  if (ad->sk == nullptr) {
85
8.14k
    ad->sk = sk_void_new_null();
86
8.14k
    if (ad->sk == nullptr) {
87
0
      return 0;
88
0
    }
89
8.14k
  }
90
91
  // Add NULL values until the stack is long enough.
92
16.2k
  for (size_t i = sk_void_num(ad->sk); i <= (size_t)index; i++) {
93
8.14k
    if (!sk_void_push(ad->sk, nullptr)) {
94
0
      return 0;
95
0
    }
96
8.14k
  }
97
98
8.14k
  sk_void_set(ad->sk, (size_t)index, val);
99
8.14k
  return 1;
100
8.14k
}
101
102
0
void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx) {
103
0
  if (ad->sk == nullptr || idx < 0 || (size_t)idx >= sk_void_num(ad->sk)) {
104
0
    return nullptr;
105
0
  }
106
0
  return sk_void_value(ad->sk, idx);
107
0
}
108
109
1.08M
void CRYPTO_new_ex_data(CRYPTO_EX_DATA *ad) { ad->sk = nullptr; }
110
111
1.09M
void CRYPTO_free_ex_data(ExDataClass *ex_data_class, CRYPTO_EX_DATA *ad) {
112
1.09M
  if (ad->sk == nullptr) {
113
    // Nothing to do.
114
1.08M
    return;
115
1.08M
  }
116
117
8.14k
  uint32_t num_funcs = ex_data_class->num_funcs.load();
118
  // |CRYPTO_get_ex_new_index_ex| will not allocate indices beyond |INT_MAX|.
119
8.14k
  assert(num_funcs <= (size_t)(INT_MAX - ex_data_class->num_reserved));
120
121
  // Defer dereferencing |ex_data_class->funcs| and |funcs->next|. It must come
122
  // after the |num_funcs| comparison to be correctly synchronized.
123
8.14k
  ExDataFuncs *const *funcs = &ex_data_class->funcs;
124
8.14k
  for (uint32_t i = 0; i < num_funcs; i++) {
125
0
    if ((*funcs)->free_func != nullptr) {
126
0
      int index = (int)i + ex_data_class->num_reserved;
127
0
      void *ptr = CRYPTO_get_ex_data(ad, index);
128
0
      (*funcs)->free_func(/*parent=*/nullptr, ptr, /*ad*/ nullptr, index,
129
0
                          (*funcs)->argl, (*funcs)->argp);
130
0
    }
131
0
    funcs = &(*funcs)->next;
132
0
  }
133
134
8.14k
  sk_void_free(ad->sk);
135
8.14k
  ad->sk = nullptr;
136
8.14k
}
137
138
BSSL_NAMESPACE_END
139
140
0
void CRYPTO_cleanup_all_ex_data() {}