From 37937178bca6c27d33ddd9ad7df8548dbaef7120 Mon Sep 17 00:00:00 2001 From: rafagafe Date: Wed, 5 Apr 2017 02:25:17 +0200 Subject: [PATCH] Defensive programming is added. --- tiny-json.c | 12 +++++++----- tiny-json.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tiny-json.c b/tiny-json.c index 7e016ee..4401733 100644 --- a/tiny-json.c +++ b/tiny-json.c @@ -40,6 +40,8 @@ json_t const* json_getProperty( json_t const* obj, char const* property ) { char const* json_getPropertyValue( json_t const* obj, char const* property ) { json_t const* field = json_getProperty( obj, property ); if ( !field ) return 0; + jsonType_t type = json_getType( field ); + if ( JSON_ARRAY >= type ) return 0; return json_getValue( field ); } @@ -322,6 +324,10 @@ static char* objValue( char* ptr, json_t* obj, jsonPool_t* pool ) { for(;;) { ptr = goBlank( ptr ); if ( !ptr ) return 0; + if ( *ptr == ',' ) { + ++ptr; + continue; + } char const endchar = ( obj->type == JSON_OBJ )? '}': ']'; if ( *ptr == endchar ) { *ptr = '\0'; @@ -332,10 +338,6 @@ static char* objValue( char* ptr, json_t* obj, jsonPool_t* pool ) { ++ptr; continue; } - if ( *ptr == ',' ) { - ptr = goBlank( ++ptr ); - if ( !ptr ) return 0; - } json_t* property = poolNew( pool ); if ( !property ) return 0; if( obj->type != JSON_ARRAY ) { @@ -365,7 +367,7 @@ static char* objValue( char* ptr, json_t* obj, jsonPool_t* pool ) { case 't': ptr = trueValue( ptr, property ); break; case 'f': ptr = falseValue( ptr, property ); break; case 'n': ptr = nullValue( ptr, property ); break; - default: ptr = numValue( ptr, property ); break; + default: ptr = numValue( ptr, property ); break; } if ( !ptr ) return 0; } diff --git a/tiny-json.h b/tiny-json.h index 20253ad..1bcfbc6 100644 --- a/tiny-json.h +++ b/tiny-json.h @@ -100,7 +100,7 @@ json_t const* json_getProperty( json_t const* obj, char const* property ); * @param obj A valid handler of a json object. Its type must be JSON_OBJ. * @param property The name of property to get. * @retval If found a pointer to null-terminated string with the value. - * @retval Null pointer if not found. */ + * @retval Null pointer if not found or it is an array or an object. */ char const* json_getPropertyValue( json_t const* obj, char const* property ); /** Get the first property of a JSON object or array.