Skip to content

Commit 2158205

Browse files
committed
Use internal, locale-independent isspace(), isdigit() implementations.
Fixes bitcoin#17
1 parent 2ab9ad4 commit 2158205

File tree

7 files changed

+37
-15
lines changed

7 files changed

+37
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ univalue-config.h*
1818
test-driver
1919
libtool
2020
ltmain.sh
21+
test-suite.log
2122

2223
*.a
2324
*.la

gen/gen.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// $ ./gen > univalue_escapes.h
99
//
1010

11-
#include <ctype.h>
1211
#include <stdio.h>
1312
#include <string.h>
1413
#include "univalue.h"

include/univalue.h

+17-1
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,24 @@ extern enum jtokentype getJsonToken(std::string& tokenVal,
243243
unsigned int& consumed, const char *raw);
244244
extern const char *uvTypeName(UniValue::VType t);
245245

246+
static inline bool json_isspace(int ch)
247+
{
248+
switch (ch) {
249+
case 0x20:
250+
case 0x09:
251+
case 0x0a:
252+
case 0x0d:
253+
return true;
254+
255+
default:
256+
return false;
257+
}
258+
259+
// not reached
260+
}
261+
246262
extern const UniValue NullUniValue;
247263

248264
const UniValue& find_value( const UniValue& obj, const std::string& name);
249265

250-
#endif // __UNIVALUE_H__
266+
#endif // __UNIVALUE_H__

lib/univalue.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <stdint.h>
7-
#include <ctype.h>
87
#include <errno.h>
98
#include <iomanip>
109
#include <limits>
@@ -21,7 +20,7 @@ static bool ParsePrechecks(const std::string& str)
2120
{
2221
if (str.empty()) // No empty string allowed
2322
return false;
24-
if (str.size() >= 1 && (isspace(str[0]) || isspace(str[str.size()-1]))) // No padding allowed
23+
if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1]))) // No padding allowed
2524
return false;
2625
if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
2726
return false;

lib/univalue_read.cpp

+15-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
using namespace std;
1111

12+
static bool json_isdigit(int ch)
13+
{
14+
return ((ch >= '0') && (ch <= '9'));
15+
}
16+
1217
// convert hexadecimal string to unsigned integer
1318
static const char *hatoui(const char *first, const char *last,
1419
unsigned int& out)
@@ -17,7 +22,7 @@ static const char *hatoui(const char *first, const char *last,
1722
for (; first != last; ++first)
1823
{
1924
int digit;
20-
if (isdigit(*first))
25+
if (json_isdigit(*first))
2126
digit = *first - '0';
2227

2328
else if (*first >= 'a' && *first <= 'f')
@@ -44,7 +49,7 @@ enum jtokentype getJsonToken(string& tokenVal, unsigned int& consumed,
4449

4550
const char *rawStart = raw;
4651

47-
while ((*raw) && (isspace(*raw))) // skip whitespace
52+
while ((*raw) && (json_isspace(*raw))) // skip whitespace
4853
raw++;
4954

5055
switch (*raw) {
@@ -113,18 +118,18 @@ enum jtokentype getJsonToken(string& tokenVal, unsigned int& consumed,
113118
const char *first = raw;
114119

115120
const char *firstDigit = first;
116-
if (!isdigit(*firstDigit))
121+
if (!json_isdigit(*firstDigit))
117122
firstDigit++;
118-
if ((*firstDigit == '0') && isdigit(firstDigit[1]))
123+
if ((*firstDigit == '0') && json_isdigit(firstDigit[1]))
119124
return JTOK_ERR;
120125

121126
numStr += *raw; // copy first char
122127
raw++;
123128

124-
if ((*first == '-') && (!isdigit(*raw)))
129+
if ((*first == '-') && (!json_isdigit(*raw)))
125130
return JTOK_ERR;
126131

127-
while ((*raw) && isdigit(*raw)) { // copy digits
132+
while ((*raw) && json_isdigit(*raw)) { // copy digits
128133
numStr += *raw;
129134
raw++;
130135
}
@@ -134,9 +139,9 @@ enum jtokentype getJsonToken(string& tokenVal, unsigned int& consumed,
134139
numStr += *raw; // copy .
135140
raw++;
136141

137-
if (!isdigit(*raw))
142+
if (!json_isdigit(*raw))
138143
return JTOK_ERR;
139-
while ((*raw) && isdigit(*raw)) { // copy digits
144+
while ((*raw) && json_isdigit(*raw)) { // copy digits
140145
numStr += *raw;
141146
raw++;
142147
}
@@ -152,9 +157,9 @@ enum jtokentype getJsonToken(string& tokenVal, unsigned int& consumed,
152157
raw++;
153158
}
154159

155-
if (!isdigit(*raw))
160+
if (!json_isdigit(*raw))
156161
return JTOK_ERR;
157-
while ((*raw) && isdigit(*raw)) { // copy digits
162+
while ((*raw) && json_isdigit(*raw)) { // copy digits
158163
numStr += *raw;
159164
raw++;
160165
}

lib/univalue_write.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <ctype.h>
65
#include <iomanip>
76
#include <sstream>
87
#include <stdio.h>

test/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
unitester
2+
3+
*.trs
4+
*.log

0 commit comments

Comments
 (0)