Skip to content

Commit cb7b177

Browse files
authored
Swap bool_ enum for stdbool.h (#72)
1 parent 9b6b87e commit cb7b177

File tree

22 files changed

+214
-177
lines changed

22 files changed

+214
-177
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,17 @@ jobs:
146146
run: |
147147
git-secrets --register-aws
148148
git-secrets --scan
149+
custom-standard-c-headers:
150+
runs-on: ubuntu-latest
151+
steps:
152+
- name: Clone This Repo
153+
uses: actions/checkout@v2
154+
- name: Build
155+
run: |
156+
mkdir -p override-include
157+
cp source/include/stdbool.readme override-include/stdbool.h
158+
cmake -S test -B build/ \
159+
-G "Unix Makefiles" \
160+
-DBUILD_CLONE_SUBMODULES=ON \
161+
-DCMAKE_C_FLAGS='-Wall -Wextra -I../override-include'
162+
make -C build/ coverity_analysis

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ A search may descend through nested objects when the `queryKey` contains matchin
5353

5454
A compiler that supports **C90 or later** such as *gcc* is required to build the library.
5555

56+
The library uses a header file introduced in ISO C99, `stdbool.h`. For compilers that do not provide this header file, the [source/include](source/include) directory contains [stdbool.readme](source/include/stdbool.readme), which can be renamed to `stdbool.h`.
57+
5658
For instance, if the example above is copied to a file named `example.c`, *gcc* can be used like so:
5759
```bash
5860
gcc -I source/include example.c source/core_json.c -o example

source/core_json.c

Lines changed: 93 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@
3131
#include "core_json.h"
3232

3333
/** @cond DO_NOT_DOCUMENT */
34-
typedef enum
35-
{
36-
true = 1,
37-
false = 0
38-
} bool_;
3934

4035
/* A compromise to satisfy both MISRA and CBMC */
4136
typedef union
@@ -124,10 +119,10 @@ static size_t countHighBits( uint8_t c )
124119
*
125120
* @note Disallow ASCII, as this is called only for multibyte sequences.
126121
*/
127-
static bool_ shortestUTF8( size_t length,
128-
uint32_t value )
122+
static bool shortestUTF8( size_t length,
123+
uint32_t value )
129124
{
130-
bool_ ret = false;
125+
bool ret = false;
131126
uint32_t min, max;
132127

133128
assert( ( length >= 2U ) && ( length <= 4U ) );
@@ -182,11 +177,11 @@ static bool_ shortestUTF8( size_t length,
182177
* would introduce a non-shortest sequence, and F5 or above would
183178
* introduce a value greater than the last code point, 0x10FFFF.
184179
*/
185-
static bool_ skipUTF8MultiByte( const char * buf,
186-
size_t * start,
187-
size_t max )
180+
static bool skipUTF8MultiByte( const char * buf,
181+
size_t * start,
182+
size_t max )
188183
{
189-
bool_ ret = false;
184+
bool ret = false;
190185
size_t i, bitCount, j;
191186
uint32_t value = 0;
192187
char_ c;
@@ -246,11 +241,11 @@ static bool_ skipUTF8MultiByte( const char * buf,
246241
* @return true if a valid code point was present;
247242
* false otherwise.
248243
*/
249-
static bool_ skipUTF8( const char * buf,
250-
size_t * start,
251-
size_t max )
244+
static bool skipUTF8( const char * buf,
245+
size_t * start,
246+
size_t max )
252247
{
253-
bool_ ret = false;
248+
bool ret = false;
254249

255250
assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
256251

@@ -321,12 +316,12 @@ static uint8_t hexToInt( char c )
321316
*
322317
* @note For the sake of security, \u0000 is disallowed.
323318
*/
324-
static bool_ skipOneHexEscape( const char * buf,
325-
size_t * start,
326-
size_t max,
327-
uint16_t * outValue )
319+
static bool skipOneHexEscape( const char * buf,
320+
size_t * start,
321+
size_t max,
322+
uint16_t * outValue )
328323
{
329-
bool_ ret = false;
324+
bool ret = false;
330325
size_t i, end;
331326
uint16_t value = 0;
332327

@@ -382,11 +377,11 @@ static bool_ skipOneHexEscape( const char * buf,
382377
#define isHighSurrogate( x ) ( ( ( x ) >= 0xD800U ) && ( ( x ) <= 0xDBFFU ) )
383378
#define isLowSurrogate( x ) ( ( ( x ) >= 0xDC00U ) && ( ( x ) <= 0xDFFFU ) )
384379

385-
static bool_ skipHexEscape( const char * buf,
386-
size_t * start,
387-
size_t max )
380+
static bool skipHexEscape( const char * buf,
381+
size_t * start,
382+
size_t max )
388383
{
389-
bool_ ret = false;
384+
bool ret = false;
390385
size_t i;
391386
uint16_t value;
392387

@@ -434,11 +429,11 @@ static bool_ skipHexEscape( const char * buf,
434429
*
435430
* @note For the sake of security, \NUL is disallowed.
436431
*/
437-
static bool_ skipEscape( const char * buf,
438-
size_t * start,
439-
size_t max )
432+
static bool skipEscape( const char * buf,
433+
size_t * start,
434+
size_t max )
440435
{
441-
bool_ ret = false;
436+
bool ret = false;
442437
size_t i;
443438

444439
assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
@@ -501,11 +496,11 @@ static bool_ skipEscape( const char * buf,
501496
* @return true if a valid string was present;
502497
* false otherwise.
503498
*/
504-
static bool_ skipString( const char * buf,
505-
size_t * start,
506-
size_t max )
499+
static bool skipString( const char * buf,
500+
size_t * start,
501+
size_t max )
507502
{
508-
bool_ ret = false;
503+
bool ret = false;
509504
size_t i;
510505

511506
assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
@@ -566,9 +561,9 @@ static bool_ skipString( const char * buf,
566561
* @return true if the sequences are the same;
567562
* false otherwise
568563
*/
569-
static bool_ strnEq( const char * a,
570-
const char * b,
571-
size_t n )
564+
static bool strnEq( const char * a,
565+
const char * b,
566+
size_t n )
572567
{
573568
size_t i;
574569

@@ -597,13 +592,13 @@ static bool_ strnEq( const char * a,
597592
* @return true if the literal was present;
598593
* false otherwise.
599594
*/
600-
static bool_ skipLiteral( const char * buf,
601-
size_t * start,
602-
size_t max,
603-
const char * literal,
604-
size_t length )
595+
static bool skipLiteral( const char * buf,
596+
size_t * start,
597+
size_t max,
598+
const char * literal,
599+
size_t length )
605600
{
606-
bool_ ret = false;
601+
bool ret = false;
607602

608603
assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
609604
assert( literal != NULL );
@@ -631,14 +626,14 @@ static bool_ skipLiteral( const char * buf,
631626
* @return true if a valid literal was present;
632627
* false otherwise.
633628
*/
634-
static bool_ skipAnyLiteral( const char * buf,
635-
size_t * start,
636-
size_t max )
629+
static bool skipAnyLiteral( const char * buf,
630+
size_t * start,
631+
size_t max )
637632
{
638-
bool_ ret = false;
633+
bool ret = false;
639634

640635
#define skipLit_( x ) \
641-
( skipLiteral( buf, start, max, ( x ), ( sizeof( x ) - 1U ) ) == true )
636+
( skipLiteral( buf, start, max, ( x ), ( sizeof( x ) - 1UL ) ) == true )
642637

643638
if( skipLit_( "true" ) || skipLit_( "false" ) || skipLit_( "null" ) )
644639
{
@@ -664,12 +659,12 @@ static bool_ skipAnyLiteral( const char * buf,
664659
* false otherwise.
665660
*/
666661
#define MAX_FACTOR ( MAX_INDEX_VALUE / 10 )
667-
static bool_ skipDigits( const char * buf,
668-
size_t * start,
669-
size_t max,
670-
int32_t * outValue )
662+
static bool skipDigits( const char * buf,
663+
size_t * start,
664+
size_t max,
665+
int32_t * outValue )
671666
{
672-
bool_ ret = false;
667+
bool ret = false;
673668
size_t i, saveStart;
674669
int32_t value = 0;
675670

@@ -784,11 +779,11 @@ static void skipExponent( const char * buf,
784779
* @return true if a valid number was present;
785780
* false otherwise.
786781
*/
787-
static bool_ skipNumber( const char * buf,
788-
size_t * start,
789-
size_t max )
782+
static bool skipNumber( const char * buf,
783+
size_t * start,
784+
size_t max )
790785
{
791-
bool_ ret = false;
786+
bool ret = false;
792787
size_t i;
793788

794789
assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
@@ -840,11 +835,11 @@ static bool_ skipNumber( const char * buf,
840835
* @return true if a scalar value was present;
841836
* false otherwise.
842837
*/
843-
static bool_ skipAnyScalar( const char * buf,
844-
size_t * start,
845-
size_t max )
838+
static bool skipAnyScalar( const char * buf,
839+
size_t * start,
840+
size_t max )
846841
{
847-
bool_ ret = false;
842+
bool ret = false;
848843

849844
if( ( skipString( buf, start, max ) == true ) ||
850845
( skipAnyLiteral( buf, start, max ) == true ) ||
@@ -870,11 +865,11 @@ static bool_ skipAnyScalar( const char * buf,
870865
* @return true if a non-terminal comma was present;
871866
* false otherwise.
872867
*/
873-
static bool_ skipSpaceAndComma( const char * buf,
874-
size_t * start,
875-
size_t max )
868+
static bool skipSpaceAndComma( const char * buf,
869+
size_t * start,
870+
size_t max )
876871
{
877-
bool_ ret = false;
872+
bool ret = false;
878873
size_t i;
879874

880875
assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
@@ -952,7 +947,7 @@ static void skipObjectScalars( const char * buf,
952947
size_t max )
953948
{
954949
size_t i;
955-
bool_ comma;
950+
bool comma;
956951

957952
assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
958953

@@ -1183,13 +1178,13 @@ JSONStatus_t JSON_Validate( const char * buf,
11831178
* @return true if a value was present;
11841179
* false otherwise.
11851180
*/
1186-
static bool_ nextValue( const char * buf,
1187-
size_t * start,
1188-
size_t max,
1189-
size_t * value,
1190-
size_t * valueLength )
1181+
static bool nextValue( const char * buf,
1182+
size_t * start,
1183+
size_t max,
1184+
size_t * value,
1185+
size_t * valueLength )
11911186
{
1192-
bool_ ret = true;
1187+
bool ret = true;
11931188
size_t i, valueStart;
11941189

11951190
assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
@@ -1234,15 +1229,15 @@ static bool_ nextValue( const char * buf,
12341229
* @return true if a key-value pair was present;
12351230
* false otherwise.
12361231
*/
1237-
static bool_ nextKeyValuePair( const char * buf,
1238-
size_t * start,
1239-
size_t max,
1240-
size_t * key,
1241-
size_t * keyLength,
1242-
size_t * value,
1243-
size_t * valueLength )
1232+
static bool nextKeyValuePair( const char * buf,
1233+
size_t * start,
1234+
size_t max,
1235+
size_t * key,
1236+
size_t * keyLength,
1237+
size_t * value,
1238+
size_t * valueLength )
12441239
{
1245-
bool_ ret = true;
1240+
bool ret = true;
12461241
size_t i, keyStart;
12471242

12481243
assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
@@ -1307,14 +1302,14 @@ static bool_ nextKeyValuePair( const char * buf,
13071302
*
13081303
* @note Parsing stops upon finding a match.
13091304
*/
1310-
static bool_ objectSearch( const char * buf,
1311-
size_t max,
1312-
const char * query,
1313-
size_t queryLength,
1314-
size_t * outValue,
1315-
size_t * outValueLength )
1305+
static bool objectSearch( const char * buf,
1306+
size_t max,
1307+
const char * query,
1308+
size_t queryLength,
1309+
size_t * outValue,
1310+
size_t * outValueLength )
13161311
{
1317-
bool_ ret = false;
1312+
bool ret = false;
13181313

13191314
size_t i = 0, key, keyLength, value = 0, valueLength = 0;
13201315

@@ -1375,13 +1370,13 @@ static bool_ objectSearch( const char * buf,
13751370
*
13761371
* @note Parsing stops upon finding a match.
13771372
*/
1378-
static bool_ arraySearch( const char * buf,
1379-
size_t max,
1380-
uint32_t queryIndex,
1381-
size_t * outValue,
1382-
size_t * outValueLength )
1373+
static bool arraySearch( const char * buf,
1374+
size_t max,
1375+
uint32_t queryIndex,
1376+
size_t * outValue,
1377+
size_t * outValueLength )
13831378
{
1384-
bool_ ret = false;
1379+
bool ret = false;
13851380
size_t i = 0, value = 0, valueLength = 0;
13861381
uint32_t currentIndex = 0;
13871382

@@ -1442,12 +1437,12 @@ static bool_ arraySearch( const char * buf,
14421437
*/
14431438
#define JSON_QUERY_KEY_SEPARATOR '.'
14441439
#define isSeparator_( x ) ( ( x ) == JSON_QUERY_KEY_SEPARATOR )
1445-
static bool_ skipQueryPart( const char * buf,
1446-
size_t * start,
1447-
size_t max,
1448-
size_t * outLength )
1440+
static bool skipQueryPart( const char * buf,
1441+
size_t * start,
1442+
size_t max,
1443+
size_t * outLength )
14491444
{
1450-
bool_ ret = false;
1445+
bool ret = false;
14511446
size_t i;
14521447

14531448
assert( ( buf != NULL ) && ( start != NULL ) && ( outLength != NULL ) );
@@ -1505,7 +1500,7 @@ static JSONStatus_t multiSearch( const char * buf,
15051500

15061501
while( i < queryLength )
15071502
{
1508-
bool_ found = false;
1503+
bool found = false;
15091504

15101505
if( isSquareOpen_( query[ i ] ) )
15111506
{

source/include/core_json.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#ifndef CORE_JSON_H_
2929
#define CORE_JSON_H_
3030

31+
#include <stdbool.h>
3132
#include <stddef.h>
3233

3334
/**

0 commit comments

Comments
 (0)