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

PowerDNS / pdns / 12595591960

03 Jan 2025 09:27AM UTC coverage: 62.774% (+2.5%) from 60.245%
12595591960

Pull #15008

github

web-flow
Merge c2a2749d3 into 788f396a7
Pull Request #15008: Do not follow CNAME records for ANY or CNAME queries

30393 of 78644 branches covered (38.65%)

Branch coverage included in aggregate %.

105822 of 138350 relevant lines covered (76.49%)

4613078.44 hits per line

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

24.51
/pdns/json.cc
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
 */
22
#ifdef HAVE_CONFIG_H
23
#include "config.h"
24
#endif
25
#include "json.hh"
26
#include "namespaces.hh"
27
#include "misc.hh"
28

29
using json11::Json;
30

31
static inline int intFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const int default_value)
32
{
×
33
  const auto& val = container[key];
×
34
  if (val.is_number()) {
×
35
    return val.int_value();
×
36
  }
×
37

38
  if (val.is_string()) {
×
39
    try {
×
40
      return std::stoi(val.string_value());
×
41
    } catch (std::out_of_range&) {
×
42
      throw JsonException("Key '" + string(key) + "' is out of range");
×
43
    }
×
44
  }
×
45

46
  if (have_default) {
×
47
    return default_value;
×
48
  }
×
49
  throw JsonException("Key '" + string(key) + "' not an Integer or not present");
×
50
}
×
51

52
int intFromJson(const Json& container, const std::string& key)
53
{
×
54
  return intFromJsonInternal(container, key, false, 0);
×
55
}
×
56

57
int intFromJson(const Json& container, const std::string& key, const int default_value)
58
{
×
59
  return intFromJsonInternal(container, key, true, default_value);
×
60
}
×
61

62
static inline unsigned int uintFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const unsigned int default_value)
63
{
×
64
  int intval = intFromJsonInternal(container, key, have_default, static_cast<int>(default_value));
×
65
  if (intval >= 0) {
×
66
    return intval;
×
67
  }
×
68
  throw JsonException("Key '" + string(key) + "' is not a positive Integer");
×
69
}
×
70

71
unsigned int uintFromJson(const Json& container, const std::string& key)
72
{
×
73
  return uintFromJsonInternal(container, key, false, 0);
×
74
}
×
75

76
unsigned int uintFromJson(const Json& container, const std::string& key, const unsigned int default_value)
77
{
×
78
  return uintFromJsonInternal(container, key, true, default_value);
×
79
}
×
80

81
static inline double doubleFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const double default_value)
82
{
×
83
  const auto& val = container[key];
×
84
  if (val.is_number()) {
×
85
    return val.number_value();
×
86
  }
×
87

88
  if (val.is_string()) {
×
89
    try {
×
90
      return std::stod(val.string_value());
×
91
    } catch (std::out_of_range&) {
×
92
      throw JsonException("Value for key '" + string(key) + "' is out of range");
×
93
    }
×
94
  }
×
95

96
  if (have_default) {
×
97
    return default_value;
×
98
  }
×
99
  throw JsonException("Key '" + string(key) + "' not an Integer or not present");
×
100
}
×
101

102
double doubleFromJson(const Json& container, const std::string& key)
103
{
×
104
  return doubleFromJsonInternal(container, key, false, 0);
×
105
}
×
106

107
double doubleFromJson(const Json& container, const std::string& key, const double default_value)
108
{
×
109
  return doubleFromJsonInternal(container, key, true, default_value);
×
110
}
×
111

112
string stringFromJson(const Json& container, const std::string &key)
113
{
30✔
114
  const auto& val = container[key];
30✔
115
  if (val.is_string()) {
30!
116
    return val.string_value();
30✔
117
  }
30✔
118
  throw JsonException("Key '" + string(key) + "' not present or not a String");
×
119
}
30✔
120

121
static inline bool boolFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const bool default_value)
122
{
20✔
123
  const auto& val = container[key];
20✔
124
  if (val.is_bool()) {
20✔
125
    return val.bool_value();
19✔
126
  }
19✔
127
  if (have_default) {
1!
128
    return default_value;
1✔
129
  }
1✔
130
  throw JsonException("Key '" + string(key) + "' not present or not a Bool");
×
131
}
1✔
132

133
bool boolFromJson(const Json& container, const std::string& key)
134
{
10✔
135
  return boolFromJsonInternal(container, key, false, false);
10✔
136
}
10✔
137

138
bool boolFromJson(const Json& container, const std::string& key, const bool default_value)
139
{
10✔
140
  return boolFromJsonInternal(container, key, true, default_value);
10✔
141
}
10✔
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

© 2025 Coveralls, Inc