Coverage Report

Created: 2025-07-18 06:49

/src/libtorrent/src/kademlia/msg.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
3
Copyright (c) 2015-2016, Steven Siloti
4
Copyright (c) 2016, Alden Torres
5
Copyright (c) 2016, 2018-2019, Arvid Norberg
6
All rights reserved.
7
8
Redistribution and use in source and binary forms, with or without
9
modification, are permitted provided that the following conditions
10
are met:
11
12
* Redistributions of source code must retain the above copyright
13
notice, this list of conditions and the following disclaimer.
14
* Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the following disclaimer in
16
the documentation and/or other materials provided with the distribution.
17
* Neither the name of the author nor the names of its
18
contributors may be used to endorse or promote products derived
19
from this software without specific prior written permission.
20
21
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
POSSIBILITY OF SUCH DAMAGE.
32
33
*/
34
35
#include "libtorrent/kademlia/msg.hpp"
36
#include "libtorrent/bdecode.hpp"
37
#include "libtorrent/entry.hpp"
38
39
namespace libtorrent { namespace dht {
40
41
bool verify_message_impl(bdecode_node const& message, span<key_desc_t const> desc
42
  , span<bdecode_node> ret, span<char> error)
43
0
{
44
0
  TORRENT_ASSERT(desc.size() == ret.size());
45
46
0
  auto const size = ret.size();
47
48
  // get a non-root bdecode_node that still
49
  // points to the root. message should not be copied
50
0
  bdecode_node msg = message.non_owning();
51
52
  // clear the return buffer
53
0
  for (int i = 0; i < size; ++i)
54
0
    ret[i].clear();
55
56
  // when parsing child nodes, this is the stack
57
  // of bdecode_nodes to return to
58
0
  bdecode_node stack[5];
59
0
  int stack_ptr = -1;
60
61
0
  if (msg.type() != bdecode_node::dict_t)
62
0
  {
63
0
    std::snprintf(error.data(), static_cast<std::size_t>(error.size()), "not a dictionary");
64
0
    return false;
65
0
  }
66
0
  ++stack_ptr;
67
0
  stack[stack_ptr] = msg;
68
0
  for (int i = 0; i < size; ++i)
69
0
  {
70
0
    key_desc_t const& k = desc[i];
71
72
    //    std::fprintf(stderr, "looking for %s in %s\n", k.name, print_entry(*msg).c_str());
73
74
0
    ret[i] = msg.dict_find(k.name);
75
    // none_t means any type
76
0
    if (ret[i] && ret[i].type() != k.type && k.type != bdecode_node::none_t)
77
0
      ret[i].clear();
78
0
    if (!ret[i] && (k.flags & key_desc_t::optional) == 0)
79
0
    {
80
      // the key was not found, and it's not an optional key
81
0
      std::snprintf(error.data(), static_cast<std::size_t>(error.size()), "missing '%s' key", k.name);
82
0
      return false;
83
0
    }
84
85
0
    if (k.size > 0
86
0
      && ret[i]
87
0
      && k.type == bdecode_node::string_t)
88
0
    {
89
0
      bool const invalid = (k.flags & key_desc_t::size_divisible)
90
0
        ? (ret[i].string_length() % k.size) != 0
91
0
        : ret[i].string_length() != k.size;
92
93
0
      if (invalid)
94
0
      {
95
        // the string was not of the required size
96
0
        ret[i].clear();
97
0
        if ((k.flags & key_desc_t::optional) == 0)
98
0
        {
99
0
          std::snprintf(error.data(), static_cast<std::size_t>(error.size())
100
0
            , "invalid value for '%s'", k.name);
101
0
          return false;
102
0
        }
103
0
      }
104
0
    }
105
0
    if (k.flags & key_desc_t::parse_children)
106
0
    {
107
0
      TORRENT_ASSERT(k.type == bdecode_node::dict_t);
108
109
0
      if (ret[i])
110
0
      {
111
0
        ++stack_ptr;
112
0
        TORRENT_ASSERT(stack_ptr < int(sizeof(stack) / sizeof(stack[0])));
113
0
        msg = ret[i];
114
0
        stack[stack_ptr] = msg;
115
0
      }
116
0
      else
117
0
      {
118
        // skip all children
119
0
        while (i < size && (desc[i].flags & key_desc_t::last_child) == 0) ++i;
120
        // if this assert is hit, desc is incorrect
121
0
        TORRENT_ASSERT(i < size);
122
0
      }
123
0
    }
124
0
    else if (k.flags & key_desc_t::last_child)
125
0
    {
126
0
      TORRENT_ASSERT(stack_ptr > 0);
127
      // this can happen if the specification passed
128
      // in is unbalanced. i.e. contain more last_child
129
      // nodes than parse_children
130
0
      if (stack_ptr == 0) return false;
131
0
      --stack_ptr;
132
0
      msg = stack[stack_ptr];
133
0
    }
134
0
  }
135
0
  return true;
136
0
}
137
138
} }