Skip to content

Commit 8bf4f7b

Browse files
authored
Merge pull request Tencent#1188 from Martinfx/master
Fix warnings: dereference of null pointer
2 parents 9dfc437 + 72481d5 commit 8bf4f7b

File tree

1 file changed

+42
-37
lines changed
  • include/rapidjson/internal

1 file changed

+42
-37
lines changed

include/rapidjson/internal/itoa.h

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Tencent is pleased to support the open source community by making RapidJSON available.
2-
//
2+
//
33
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
44
//
55
// Licensed under the MIT License (the "License"); you may not use this file except
66
// in compliance with the License. You may obtain a copy of the License at
77
//
88
// http://opensource.org/licenses/MIT
99
//
10-
// Unless required by applicable law or agreed to in writing, software distributed
11-
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12-
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
// Unless required by applicable law or agreed to in writing, software distributed
11+
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
1313
// specific language governing permissions and limitations under the License.
1414

1515
#ifndef RAPIDJSON_ITOA_
@@ -37,12 +37,14 @@ inline const char* GetDigitsLut() {
3737
}
3838

3939
inline char* u32toa(uint32_t value, char* buffer) {
40+
RAPIDJSON_ASSERT(buffer != 0);
41+
4042
const char* cDigitsLut = GetDigitsLut();
4143

4244
if (value < 10000) {
4345
const uint32_t d1 = (value / 100) << 1;
4446
const uint32_t d2 = (value % 100) << 1;
45-
47+
4648
if (value >= 1000)
4749
*buffer++ = cDigitsLut[d1];
4850
if (value >= 100)
@@ -55,32 +57,32 @@ inline char* u32toa(uint32_t value, char* buffer) {
5557
// value = bbbbcccc
5658
const uint32_t b = value / 10000;
5759
const uint32_t c = value % 10000;
58-
60+
5961
const uint32_t d1 = (b / 100) << 1;
6062
const uint32_t d2 = (b % 100) << 1;
61-
63+
6264
const uint32_t d3 = (c / 100) << 1;
6365
const uint32_t d4 = (c % 100) << 1;
64-
66+
6567
if (value >= 10000000)
6668
*buffer++ = cDigitsLut[d1];
6769
if (value >= 1000000)
6870
*buffer++ = cDigitsLut[d1 + 1];
6971
if (value >= 100000)
7072
*buffer++ = cDigitsLut[d2];
7173
*buffer++ = cDigitsLut[d2 + 1];
72-
74+
7375
*buffer++ = cDigitsLut[d3];
7476
*buffer++ = cDigitsLut[d3 + 1];
7577
*buffer++ = cDigitsLut[d4];
7678
*buffer++ = cDigitsLut[d4 + 1];
7779
}
7880
else {
7981
// value = aabbbbcccc in decimal
80-
82+
8183
const uint32_t a = value / 100000000; // 1 to 42
8284
value %= 100000000;
83-
85+
8486
if (a >= 10) {
8587
const unsigned i = a << 1;
8688
*buffer++ = cDigitsLut[i];
@@ -91,13 +93,13 @@ inline char* u32toa(uint32_t value, char* buffer) {
9193

9294
const uint32_t b = value / 10000; // 0 to 9999
9395
const uint32_t c = value % 10000; // 0 to 9999
94-
96+
9597
const uint32_t d1 = (b / 100) << 1;
9698
const uint32_t d2 = (b % 100) << 1;
97-
99+
98100
const uint32_t d3 = (c / 100) << 1;
99101
const uint32_t d4 = (c % 100) << 1;
100-
102+
101103
*buffer++ = cDigitsLut[d1];
102104
*buffer++ = cDigitsLut[d1 + 1];
103105
*buffer++ = cDigitsLut[d2];
@@ -111,6 +113,7 @@ inline char* u32toa(uint32_t value, char* buffer) {
111113
}
112114

113115
inline char* i32toa(int32_t value, char* buffer) {
116+
RAPIDJSON_ASSERT(buffer != 0);
114117
uint32_t u = static_cast<uint32_t>(value);
115118
if (value < 0) {
116119
*buffer++ = '-';
@@ -121,6 +124,7 @@ inline char* i32toa(int32_t value, char* buffer) {
121124
}
122125

123126
inline char* u64toa(uint64_t value, char* buffer) {
127+
RAPIDJSON_ASSERT(buffer != 0);
124128
const char* cDigitsLut = GetDigitsLut();
125129
const uint64_t kTen8 = 100000000;
126130
const uint64_t kTen9 = kTen8 * 10;
@@ -131,13 +135,13 @@ inline char* u64toa(uint64_t value, char* buffer) {
131135
const uint64_t kTen14 = kTen8 * 1000000;
132136
const uint64_t kTen15 = kTen8 * 10000000;
133137
const uint64_t kTen16 = kTen8 * kTen8;
134-
138+
135139
if (value < kTen8) {
136140
uint32_t v = static_cast<uint32_t>(value);
137141
if (v < 10000) {
138142
const uint32_t d1 = (v / 100) << 1;
139143
const uint32_t d2 = (v % 100) << 1;
140-
144+
141145
if (v >= 1000)
142146
*buffer++ = cDigitsLut[d1];
143147
if (v >= 100)
@@ -150,21 +154,21 @@ inline char* u64toa(uint64_t value, char* buffer) {
150154
// value = bbbbcccc
151155
const uint32_t b = v / 10000;
152156
const uint32_t c = v % 10000;
153-
157+
154158
const uint32_t d1 = (b / 100) << 1;
155159
const uint32_t d2 = (b % 100) << 1;
156-
160+
157161
const uint32_t d3 = (c / 100) << 1;
158162
const uint32_t d4 = (c % 100) << 1;
159-
163+
160164
if (value >= 10000000)
161165
*buffer++ = cDigitsLut[d1];
162166
if (value >= 1000000)
163167
*buffer++ = cDigitsLut[d1 + 1];
164168
if (value >= 100000)
165169
*buffer++ = cDigitsLut[d2];
166170
*buffer++ = cDigitsLut[d2 + 1];
167-
171+
168172
*buffer++ = cDigitsLut[d3];
169173
*buffer++ = cDigitsLut[d3 + 1];
170174
*buffer++ = cDigitsLut[d4];
@@ -174,22 +178,22 @@ inline char* u64toa(uint64_t value, char* buffer) {
174178
else if (value < kTen16) {
175179
const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
176180
const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
177-
181+
178182
const uint32_t b0 = v0 / 10000;
179183
const uint32_t c0 = v0 % 10000;
180-
184+
181185
const uint32_t d1 = (b0 / 100) << 1;
182186
const uint32_t d2 = (b0 % 100) << 1;
183-
187+
184188
const uint32_t d3 = (c0 / 100) << 1;
185189
const uint32_t d4 = (c0 % 100) << 1;
186190

187191
const uint32_t b1 = v1 / 10000;
188192
const uint32_t c1 = v1 % 10000;
189-
193+
190194
const uint32_t d5 = (b1 / 100) << 1;
191195
const uint32_t d6 = (b1 % 100) << 1;
192-
196+
193197
const uint32_t d7 = (c1 / 100) << 1;
194198
const uint32_t d8 = (c1 % 100) << 1;
195199

@@ -209,7 +213,7 @@ inline char* u64toa(uint64_t value, char* buffer) {
209213
*buffer++ = cDigitsLut[d4];
210214
if (value >= kTen8)
211215
*buffer++ = cDigitsLut[d4 + 1];
212-
216+
213217
*buffer++ = cDigitsLut[d5];
214218
*buffer++ = cDigitsLut[d5 + 1];
215219
*buffer++ = cDigitsLut[d6];
@@ -222,7 +226,7 @@ inline char* u64toa(uint64_t value, char* buffer) {
222226
else {
223227
const uint32_t a = static_cast<uint32_t>(value / kTen16); // 1 to 1844
224228
value %= kTen16;
225-
229+
226230
if (a < 10)
227231
*buffer++ = static_cast<char>('0' + static_cast<char>(a));
228232
else if (a < 100) {
@@ -232,7 +236,7 @@ inline char* u64toa(uint64_t value, char* buffer) {
232236
}
233237
else if (a < 1000) {
234238
*buffer++ = static_cast<char>('0' + static_cast<char>(a / 100));
235-
239+
236240
const uint32_t i = (a % 100) << 1;
237241
*buffer++ = cDigitsLut[i];
238242
*buffer++ = cDigitsLut[i + 1];
@@ -245,28 +249,28 @@ inline char* u64toa(uint64_t value, char* buffer) {
245249
*buffer++ = cDigitsLut[j];
246250
*buffer++ = cDigitsLut[j + 1];
247251
}
248-
252+
249253
const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
250254
const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
251-
255+
252256
const uint32_t b0 = v0 / 10000;
253257
const uint32_t c0 = v0 % 10000;
254-
258+
255259
const uint32_t d1 = (b0 / 100) << 1;
256260
const uint32_t d2 = (b0 % 100) << 1;
257-
261+
258262
const uint32_t d3 = (c0 / 100) << 1;
259263
const uint32_t d4 = (c0 % 100) << 1;
260-
264+
261265
const uint32_t b1 = v1 / 10000;
262266
const uint32_t c1 = v1 % 10000;
263-
267+
264268
const uint32_t d5 = (b1 / 100) << 1;
265269
const uint32_t d6 = (b1 % 100) << 1;
266-
270+
267271
const uint32_t d7 = (c1 / 100) << 1;
268272
const uint32_t d8 = (c1 % 100) << 1;
269-
273+
270274
*buffer++ = cDigitsLut[d1];
271275
*buffer++ = cDigitsLut[d1 + 1];
272276
*buffer++ = cDigitsLut[d2];
@@ -284,11 +288,12 @@ inline char* u64toa(uint64_t value, char* buffer) {
284288
*buffer++ = cDigitsLut[d8];
285289
*buffer++ = cDigitsLut[d8 + 1];
286290
}
287-
291+
288292
return buffer;
289293
}
290294

291295
inline char* i64toa(int64_t value, char* buffer) {
296+
RAPIDJSON_ASSERT(buffer != 0);
292297
uint64_t u = static_cast<uint64_t>(value);
293298
if (value < 0) {
294299
*buffer++ = '-';

0 commit comments

Comments
 (0)