Skip to content

Commit 75d5ecd

Browse files
authored
Merge pull request #22 from Microsoft/analysis
Fix VC++ Code Analysis and g++ warnings
2 parents f92980f + ca80d26 commit 75d5ecd

File tree

3 files changed

+41
-76
lines changed

3 files changed

+41
-76
lines changed

GraphQLResponse.cpp

Lines changed: 18 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,6 @@ Value::Value(Type type /*= Type::Null*/)
2828
_string.reset(new StringType());
2929
break;
3030

31-
case Type::Boolean:
32-
_boolean = false;
33-
break;
34-
35-
case Type::Int:
36-
_int = 0;
37-
break;
38-
39-
case Type::Float:
40-
_float = 0;
41-
break;
42-
4331
case Type::Scalar:
4432
_scalar.reset(new Value());
4533
break;
@@ -73,35 +61,28 @@ Value::Value(FloatType value)
7361
{
7462
}
7563

76-
Value::Value(Value&& other)
64+
Value::Value(Value&& other) noexcept
7765
: _type(other._type)
7866
, _members(std::move(other._members))
7967
, _map(std::move(other._map))
8068
, _list(std::move(other._list))
8169
, _string(std::move(other._string))
8270
, _scalar(std::move(other._scalar))
71+
, _boolean(other._boolean)
72+
, _int(other._int)
73+
, _float(other._float)
8374
{
84-
switch (_type)
85-
{
86-
case Type::Boolean:
87-
_boolean = other._boolean;
88-
break;
89-
90-
case Type::Int:
91-
_int = other._int;
92-
break;
93-
94-
case Type::Float:
95-
_float = other._float;
96-
break;
97-
98-
default:
99-
break;
100-
}
75+
const_cast<Type&>(other._type) = Type::Null;
76+
other._boolean = false;
77+
other._int = 0;
78+
other._float = 0.0;
10179
}
10280

10381
Value::Value(const Value& other)
10482
: _type(other._type)
83+
, _boolean(other._boolean)
84+
, _int(other._int)
85+
, _float(other._float)
10586
{
10687
switch (_type)
10788
{
@@ -119,18 +100,6 @@ Value::Value(const Value& other)
119100
_string.reset(new StringType(*other._string));
120101
break;
121102

122-
case Type::Boolean:
123-
_boolean = other._boolean;
124-
break;
125-
126-
case Type::Int:
127-
_int = other._int;
128-
break;
129-
130-
case Type::Float:
131-
_float = other._float;
132-
break;
133-
134103
case Type::Scalar:
135104
_scalar.reset(new Value(*other._scalar));
136105
break;
@@ -140,7 +109,7 @@ Value::Value(const Value& other)
140109
}
141110
}
142111

143-
Value& Value::operator=(Value&& rhs)
112+
Value& Value::operator=(Value&& rhs) noexcept
144113
{
145114
const_cast<Type&>(_type) = rhs._type;
146115
const_cast<Type&>(rhs._type) = Type::Null;
@@ -150,24 +119,12 @@ Value& Value::operator=(Value&& rhs)
150119
_list = std::move(rhs._list);
151120
_string = std::move(rhs._string);
152121
_scalar = std::move(rhs._scalar);
153-
154-
switch (_type)
155-
{
156-
case Type::Boolean:
157-
_boolean = rhs._boolean;
158-
break;
159-
160-
case Type::Int:
161-
_int = rhs._int;
162-
break;
163-
164-
case Type::Float:
165-
_float = rhs._float;
166-
break;
167-
168-
default:
169-
break;
170-
}
122+
_boolean = rhs._boolean;
123+
rhs._boolean = false;
124+
_int = rhs._int;
125+
rhs._int = 0;
126+
_float = rhs._float;
127+
rhs._float = 0.0;
171128

172129
return *this;
173130
}

JSONResponse.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <rapidjson/reader.h>
1414

1515
#include <stack>
16+
#include <limits>
17+
#include <stdexcept>
1618

1719
namespace facebook {
1820
namespace graphql {
@@ -141,6 +143,8 @@ struct ResponseHandler
141143

142144
bool Int(int i)
143145
{
146+
// https://facebook.github.io/graphql/June2018/#sec-Int
147+
static_assert(sizeof(i) == 4, "GraphQL only supports 32-bit signed integers");
144148
auto value = Value(Type::Int);
145149

146150
value.set<IntType>(std::move(i));
@@ -150,17 +154,24 @@ struct ResponseHandler
150154

151155
bool Uint(unsigned int i)
152156
{
157+
if (i > static_cast<unsigned int>(std::numeric_limits<int>::max()))
158+
{
159+
// https://facebook.github.io/graphql/June2018/#sec-Int
160+
throw std::overflow_error("GraphQL only supports 32-bit signed integers");
161+
}
153162
return Int(static_cast<int>(i));
154163
}
155164

156-
bool Int64(int64_t i)
165+
bool Int64(int64_t /*i*/)
157166
{
158-
return Int(static_cast<int>(i));
167+
// https://facebook.github.io/graphql/June2018/#sec-Int
168+
throw std::overflow_error("GraphQL only supports 32-bit signed integers");
159169
}
160170

161-
bool Uint64(uint64_t i)
171+
bool Uint64(uint64_t /*i*/)
162172
{
163-
return Int(static_cast<int>(i));
173+
// https://facebook.github.io/graphql/June2018/#sec-Int
174+
throw std::overflow_error("GraphQL only supports 32-bit signed integers");
164175
}
165176

166177
bool Double(double d)

include/graphqlservice/GraphQLResponse.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ struct Value
4848
explicit Value(IntType value);
4949
explicit Value(FloatType value);
5050

51-
Value(Value&& other);
51+
Value(Value&& other) noexcept;
5252
explicit Value(const Value& other);
5353

54-
Value& operator=(Value&& rhs);
54+
Value& operator=(Value&& rhs) noexcept;
5555
Value& operator=(const Value& rhs) = delete;
5656

5757
// Check the Type
@@ -95,17 +95,14 @@ struct Value
9595
// Type::String or Type::EnumValue
9696
std::unique_ptr<StringType> _string;
9797

98-
union
99-
{
100-
// Type::Boolean
101-
BooleanType _boolean;
98+
// Type::Boolean
99+
BooleanType _boolean = false;
102100

103-
// Type::Int
104-
IntType _int;
101+
// Type::Int
102+
IntType _int = 0;
105103

106-
// Type::Float
107-
FloatType _float;
108-
};
104+
// Type::Float
105+
FloatType _float = 0.0;
109106

110107
// Type::Scalar
111108
std::unique_ptr<ScalarType> _scalar;

0 commit comments

Comments
 (0)