• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.51
/src/lib/tls/tls_session_manager_memory.cpp
1
/**
2
 * TLS Session Management
3
 * (C) 2011,2012 Jack Lloyd
4
 *     2023 René Meusel - Rohde & Schwarz Cybersecurity
5
 *
6
 * Botan is released under the Simplified BSD License (see license.txt)
7
 */
8

9
#include <botan/tls_session_manager_memory.h>
10

11
#include <botan/rng.h>
12
#include <botan/internal/stl_util.h>
13

14
namespace Botan::TLS {
15

16
Session_Manager_In_Memory::Session_Manager_In_Memory(const std::shared_ptr<RandomNumberGenerator>& rng,
2,140✔
17
                                                     size_t max_sessions) :
2,140✔
18
      Session_Manager(rng), m_max_sessions(max_sessions) {
2,140✔
19
   if(max_sessions > 0) {
2,140✔
20
      m_fifo.emplace();
2,140✔
21
   }
22
}
2,140✔
23

24
void Session_Manager_In_Memory::store(const Session& session, const Session_Handle& handle) {
1,484✔
25
   // TODO: C++20 allows CTAD for template aliases (read: lock_guard_type), so
26
   //       technically we should be able to omit the explicit mutex type.
27
   //       Unfortunately clang does not agree, yet.
28
   lock_guard_type<recursive_mutex_type> lk(mutex());
1,484✔
29

30
   if(m_fifo.has_value()) {
1,484✔
31
      while(m_sessions.size() >= capacity()) {
1,490✔
32
         BOTAN_ASSERT_NOMSG(m_sessions.size() <= m_fifo->size());
6✔
33
         m_sessions.erase(m_fifo->front());
6✔
34
         m_fifo->pop_front();
6✔
35
      }
36
   }
37

38
   // Generate a random session ID if the peer did not provide one. Note that
39
   // this ID is just for internal use and won't be returned on ::find().
40
   auto id = handle.id().value_or(m_rng->random_vec<Session_ID>(32));
3,145✔
41
   m_sessions.emplace(id, Session_with_Handle{session, handle});
2,968✔
42

43
   if(m_fifo.has_value()) {
1,484✔
44
      m_fifo->emplace_back(std::move(id));
1,484✔
45
   }
46
}
1,484✔
47

48
std::optional<Session> Session_Manager_In_Memory::retrieve_one(const Session_Handle& handle) {
136✔
49
   lock_guard_type<recursive_mutex_type> lk(mutex());
136✔
50

51
   if(auto id = handle.id()) {
136✔
52
      const auto session = m_sessions.find(id.value());
129✔
53
      if(session != m_sessions.end()) {
129✔
54
         return session->second.session;
61✔
55
      }
56
   }
61✔
57

58
   return std::nullopt;
75✔
59
}
136✔
60

61
std::vector<Session_with_Handle> Session_Manager_In_Memory::find_some(const Server_Information& info,
1,553✔
62
                                                                      const size_t max_sessions_hint) {
63
   BOTAN_UNUSED(max_sessions_hint);
1,553✔
64

65
   lock_guard_type<recursive_mutex_type> lk(mutex());
1,553✔
66

67
   std::vector<Session_with_Handle> found_sessions;
1,553✔
68
   // TODO: std::copy_if?
69
   for(const auto& [_, session_and_handle] : m_sessions) {
2,270✔
70
      if(session_and_handle.session.server_info() == info) {
717✔
71
         found_sessions.emplace_back(session_and_handle);
717✔
72
      }
73
   }
74

75
   return found_sessions;
1,553✔
76
}
1,553✔
77

78
size_t Session_Manager_In_Memory::remove(const Session_Handle& handle) {
471✔
79
   lock_guard_type<recursive_mutex_type> lk(mutex());
471✔
80
   return remove_internal(handle);
471✔
81
}
471✔
82

83
size_t Session_Manager_In_Memory::remove_internal(const Session_Handle& handle) {
574✔
84
   return std::visit(overloaded{
1,148✔
85
                        // We deliberately leave the deleted session in m_fifo. Finding it would be
86
                        // another O(n) operation. Instead, purging will run in a loop and skip m_fifo
87
                        // entries that were not found anymore.
88
                        [&](const Session_Ticket& ticket) -> size_t {
454✔
89
                           // TODO: This is an O(n) operation. Typically, the Session_Manager will
90
                           //       not contain a plethora of sessions and this should be fine. If
91
                           //       it's not, we'll need to consider another index on tickets.
92
                           return std::erase_if(m_sessions, [&](const auto& item) {
454✔
93
                              const auto& [_unused1, session_and_handle] = item;
406✔
94
                              const auto& [_unused2, this_handle] = session_and_handle;
406✔
95
                              return this_handle.is_ticket() && this_handle.ticket().value() == ticket;
406✔
96
                           });
97
                        },
98
                        [&](const Session_ID& id) -> size_t { return m_sessions.erase(id); },
18✔
99
                        [&](const Opaque_Session_Handle&) -> size_t {
102✔
100
                           if(auto id = handle.id()) {
102✔
101
                              auto removed = remove_internal(Session_Handle(id.value()));
12✔
102
                              if(removed > 0) {
4✔
103
                                 return removed;
3✔
104
                              }
105
                           }
3✔
106

107
                           if(auto ticket = handle.ticket()) {
99✔
108
                              return remove_internal(Session_Handle(ticket.value()));
297✔
109
                           }
99✔
110

111
                           return 0;
×
112
                        },
113
                     },
114
                     handle.get());
1,148✔
115
}
116

117
size_t Session_Manager_In_Memory::remove_all() {
9✔
118
   lock_guard_type<recursive_mutex_type> lk(mutex());
9✔
119

120
   const auto sessions = m_sessions.size();
9✔
121
   m_sessions.clear();
9✔
122
   if(m_fifo.has_value()) {
9✔
123
      m_fifo->clear();
9✔
124
   }
125

126
   return sessions;
9✔
127
}
9✔
128

129
}  // namespace Botan::TLS
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc