Skip to content

Commit e0e1fd3

Browse files
committed
- Bug #3200841: removed "warning C4127: conditional expression is constant" concerning infinite loop by replacing while (true) with for (;;). Added new JSON_FAIL macro. Commented unused parameters.
1 parent d0a9f3d commit e0e1fd3

3 files changed

Lines changed: 146 additions & 11 deletions

File tree

src/lib_json/json_reader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ Reader::readString()
449449

450450

451451
bool
452-
Reader::readObject( Token &tokenStart )
452+
Reader::readObject( Token &/*tokenStart*/ )
453453
{
454454
Token tokenName;
455455
std::string name;
@@ -508,7 +508,7 @@ Reader::readObject( Token &tokenStart )
508508

509509

510510
bool
511-
Reader::readArray( Token &tokenStart )
511+
Reader::readArray( Token &/*tokenStart*/ )
512512
{
513513
currentValue() = Value( arrayValue );
514514
skipSpaces();

src/lib_json/json_value.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
#define JSON_ASSERT_UNREACHABLE assert( false )
2424
#define JSON_ASSERT( condition ) assert( condition ); // @todo <= change this into an exception throw
25-
#define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) throw std::runtime_error( message );
25+
#define JSON_FAIL_MESSAGE( message ) throw std::runtime_error( message );
26+
#define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) JSON_FAIL_MESSAGE( message )
2627

2728
namespace Json {
2829

@@ -39,7 +40,7 @@ const LargestUInt Value::maxLargestUInt = LargestUInt(-1);
3940

4041

4142
/// Unknown size marker
42-
enum { unknown = (unsigned)-1 };
43+
static const unsigned int unknown = (unsigned)-1;
4344

4445

4546
/** Duplicates the specified string value.
@@ -688,7 +689,7 @@ Value::asString() const
688689
case realValue:
689690
case arrayValue:
690691
case objectValue:
691-
JSON_ASSERT_MESSAGE( false, "Type is not convertible to string" );
692+
JSON_FAIL_MESSAGE( "Type is not convertible to string" );
692693
default:
693694
JSON_ASSERT_UNREACHABLE;
694695
}
@@ -725,7 +726,7 @@ Value::asInt() const
725726
case stringValue:
726727
case arrayValue:
727728
case objectValue:
728-
JSON_ASSERT_MESSAGE( false, "Type is not convertible to int" );
729+
JSON_FAIL_MESSAGE( "Type is not convertible to int" );
729730
default:
730731
JSON_ASSERT_UNREACHABLE;
731732
}
@@ -755,7 +756,7 @@ Value::asUInt() const
755756
case stringValue:
756757
case arrayValue:
757758
case objectValue:
758-
JSON_ASSERT_MESSAGE( false, "Type is not convertible to uint" );
759+
JSON_FAIL_MESSAGE( "Type is not convertible to uint" );
759760
default:
760761
JSON_ASSERT_UNREACHABLE;
761762
}
@@ -785,7 +786,7 @@ Value::asInt64() const
785786
case stringValue:
786787
case arrayValue:
787788
case objectValue:
788-
JSON_ASSERT_MESSAGE( false, "Type is not convertible to Int64" );
789+
JSON_FAIL_MESSAGE( "Type is not convertible to Int64" );
789790
default:
790791
JSON_ASSERT_UNREACHABLE;
791792
}
@@ -813,7 +814,7 @@ Value::asUInt64() const
813814
case stringValue:
814815
case arrayValue:
815816
case objectValue:
816-
JSON_ASSERT_MESSAGE( false, "Type is not convertible to UInt64" );
817+
JSON_FAIL_MESSAGE( "Type is not convertible to UInt64" );
817818
default:
818819
JSON_ASSERT_UNREACHABLE;
819820
}
@@ -866,7 +867,7 @@ Value::asDouble() const
866867
case stringValue:
867868
case arrayValue:
868869
case objectValue:
869-
JSON_ASSERT_MESSAGE( false, "Type is not convertible to double" );
870+
JSON_FAIL_MESSAGE( "Type is not convertible to double" );
870871
default:
871872
JSON_ASSERT_UNREACHABLE;
872873
}
@@ -895,7 +896,7 @@ Value::asFloat() const
895896
case stringValue:
896897
case arrayValue:
897898
case objectValue:
898-
JSON_ASSERT_MESSAGE( false, "Type is not convertible to float" );
899+
JSON_FAIL_MESSAGE( "Type is not convertible to float" );
899900
default:
900901
JSON_ASSERT_UNREACHABLE;
901902
}

src/test_lib_json/main.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct ValueTest : JsonTest::TestCase
3737
Json::Value true_;
3838
Json::Value false_;
3939

40+
4041
ValueTest()
4142
: emptyArray_( Json::arrayValue )
4243
, emptyObject_( Json::objectValue )
@@ -77,6 +78,10 @@ struct ValueTest : JsonTest::TestCase
7778
void checkMemberCount( Json::Value &value, unsigned int expectedCount );
7879

7980
void checkIs( const Json::Value &value, const IsCheck &check );
81+
82+
void checkIsLess( const Json::Value &x, const Json::Value &y );
83+
84+
void checkIsEqual( const Json::Value &x, const Json::Value &y );
8085
};
8186

8287

@@ -251,6 +256,128 @@ ValueTest::checkIs( const Json::Value &value, const IsCheck &check )
251256
}
252257

253258

259+
JSONTEST_FIXTURE( ValueTest, compareInt )
260+
{
261+
JSONTEST_ASSERT_PRED( checkIsLess( 0, 10 ) );
262+
JSONTEST_ASSERT_PRED( checkIsEqual( 10, 10 ) );
263+
JSONTEST_ASSERT_PRED( checkIsEqual( -10, -10 ) );
264+
JSONTEST_ASSERT_PRED( checkIsLess( -10, 0 ) );
265+
}
266+
267+
268+
JSONTEST_FIXTURE( ValueTest, compareUInt )
269+
{
270+
JSONTEST_ASSERT_PRED( checkIsLess( 0u, 10u ) );
271+
JSONTEST_ASSERT_PRED( checkIsLess( 0u, Json::Value::maxUInt ) );
272+
JSONTEST_ASSERT_PRED( checkIsEqual( 10u, 10u ) );
273+
}
274+
275+
276+
JSONTEST_FIXTURE( ValueTest, compareDouble )
277+
{
278+
JSONTEST_ASSERT_PRED( checkIsLess( 0.0, 10.0 ) );
279+
JSONTEST_ASSERT_PRED( checkIsEqual( 10.0, 10.0 ) );
280+
JSONTEST_ASSERT_PRED( checkIsEqual( -10.0, -10.0 ) );
281+
JSONTEST_ASSERT_PRED( checkIsLess( -10.0, 0.0 ) );
282+
}
283+
284+
285+
JSONTEST_FIXTURE( ValueTest, compareString )
286+
{
287+
JSONTEST_ASSERT_PRED( checkIsLess( "", " " ) );
288+
JSONTEST_ASSERT_PRED( checkIsLess( "", "a" ) );
289+
JSONTEST_ASSERT_PRED( checkIsLess( "abcd", "zyui" ) );
290+
JSONTEST_ASSERT_PRED( checkIsLess( "abc", "abcd" ) );
291+
JSONTEST_ASSERT_PRED( checkIsEqual( "abcd", "abcd" ) );
292+
JSONTEST_ASSERT_PRED( checkIsEqual( " ", " " ) );
293+
JSONTEST_ASSERT_PRED( checkIsLess( "ABCD", "abcd" ) );
294+
JSONTEST_ASSERT_PRED( checkIsEqual( "ABCD", "ABCD" ) );
295+
}
296+
297+
298+
JSONTEST_FIXTURE( ValueTest, compareBoolean )
299+
{
300+
JSONTEST_ASSERT_PRED( checkIsLess( false, true ) );
301+
JSONTEST_ASSERT_PRED( checkIsEqual( false, false ) );
302+
JSONTEST_ASSERT_PRED( checkIsEqual( true, true ) );
303+
}
304+
305+
306+
JSONTEST_FIXTURE( ValueTest, compareArray )
307+
{
308+
// array compare size then content
309+
Json::Value emptyArray(Json::arrayValue);
310+
Json::Value l1aArray;
311+
l1aArray.append( 0 );
312+
Json::Value l1bArray;
313+
l1bArray.append( 10 );
314+
Json::Value l2aArray;
315+
l2aArray.append( 0 );
316+
l2aArray.append( 0 );
317+
Json::Value l2bArray;
318+
l2bArray.append( 0 );
319+
l2bArray.append( 10 );
320+
JSONTEST_ASSERT_PRED( checkIsLess( emptyArray, l1aArray ) );
321+
JSONTEST_ASSERT_PRED( checkIsLess( emptyArray, l2aArray ) );
322+
JSONTEST_ASSERT_PRED( checkIsLess( l1aArray, l2aArray ) );
323+
JSONTEST_ASSERT_PRED( checkIsLess( l2aArray, l2bArray ) );
324+
JSONTEST_ASSERT_PRED( checkIsEqual( emptyArray, Json::Value( emptyArray ) ) );
325+
JSONTEST_ASSERT_PRED( checkIsEqual( l1aArray, Json::Value( l1aArray) ) );
326+
JSONTEST_ASSERT_PRED( checkIsEqual( l2bArray, Json::Value( l2bArray) ) );
327+
}
328+
329+
330+
JSONTEST_FIXTURE( ValueTest, compareObject )
331+
{
332+
// object compare size then content
333+
Json::Value emptyObject(Json::objectValue);
334+
Json::Value l1aObject;
335+
l1aObject["key1"] = 0;
336+
Json::Value l1bObject;
337+
l1aObject["key1"] = 10;
338+
Json::Value l2aObject;
339+
l2aObject["key1"] = 0;
340+
l2aObject["key2"] = 0;
341+
JSONTEST_ASSERT_PRED( checkIsLess( emptyObject, l1aObject ) );
342+
JSONTEST_ASSERT_PRED( checkIsLess( emptyObject, l2aObject ) );
343+
JSONTEST_ASSERT_PRED( checkIsLess( l1aObject, l2aObject ) );
344+
JSONTEST_ASSERT_PRED( checkIsEqual( emptyObject, Json::Value( emptyObject ) ) );
345+
JSONTEST_ASSERT_PRED( checkIsEqual( l1aObject, Json::Value( l1aObject ) ) );
346+
JSONTEST_ASSERT_PRED( checkIsEqual( l2aObject, Json::Value( l2aObject ) ) );
347+
}
348+
349+
350+
void
351+
ValueTest::checkIsLess( const Json::Value &x, const Json::Value &y )
352+
{
353+
JSONTEST_ASSERT( x < y );
354+
JSONTEST_ASSERT( y > x );
355+
JSONTEST_ASSERT( x <= y );
356+
JSONTEST_ASSERT( y >= x );
357+
JSONTEST_ASSERT( !(x == y) );
358+
JSONTEST_ASSERT( !(y == x) );
359+
JSONTEST_ASSERT( !(x >= y) );
360+
JSONTEST_ASSERT( !(y <= x) );
361+
JSONTEST_ASSERT( !(x > y) );
362+
JSONTEST_ASSERT( !(y < x) );
363+
}
364+
365+
366+
void
367+
ValueTest::checkIsEqual( const Json::Value &x, const Json::Value &y )
368+
{
369+
JSONTEST_ASSERT( x == y );
370+
JSONTEST_ASSERT( y == x );
371+
JSONTEST_ASSERT( x <= y );
372+
JSONTEST_ASSERT( y <= x );
373+
JSONTEST_ASSERT( x >= y );
374+
JSONTEST_ASSERT( y >= x );
375+
JSONTEST_ASSERT( !(x < y) );
376+
JSONTEST_ASSERT( !(y < x) );
377+
JSONTEST_ASSERT( !(x > y) );
378+
JSONTEST_ASSERT( !(y > x) );
379+
}
380+
254381

255382
int main( int argc, const char *argv[] )
256383
{
@@ -267,5 +394,12 @@ int main( int argc, const char *argv[] )
267394
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isNull );
268395
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, accessArray );
269396
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, asFloat );
397+
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareInt );
398+
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareUInt );
399+
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareDouble );
400+
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareString );
401+
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareBoolean );
402+
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareArray );
403+
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareObject );
270404
return runner.runCommandLine( argc, argv );
271405
}

0 commit comments

Comments
 (0)