From a9cfb5f477ac1098875039347b13d3a4bc365bb6 Mon Sep 17 00:00:00 2001 From: scottmk Date: Wed, 5 Aug 2009 22:27:04 +0000 Subject: [PATCH] Extended the JSON_INIT_CLEAR macro to avoid segfaults. Scenario: converting a JSON_BOOL, with a value of true, to a JSON_HASH or JSON_ARRAY. The true value (in a union with an osrfHash* and an osrfList*) was being interpreted as a non_NULL pointer and deferenced. Oops. With this change, we clear the boolean value (by nullifying one of the unioned pointers) whenever changing from a JSON_BOOL to anything else. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1733 9efc2488-bf62-4759-914b-345cdb29e865 --- src/libopensrf/osrf_json_object.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/libopensrf/osrf_json_object.c b/src/libopensrf/osrf_json_object.c index c9b6a29..0cab256 100644 --- a/src/libopensrf/osrf_json_object.c +++ b/src/libopensrf/osrf_json_object.c @@ -28,21 +28,23 @@ GNU General Public License for more details. if( _obj_->type == JSON_HASH && newtype != JSON_HASH ) { \ osrfHashFree(_obj_->value.h); \ _obj_->value.h = NULL; \ -} else if( _obj_->type == JSON_ARRAY && newtype != JSON_ARRAY ) { \ + } else if( _obj_->type == JSON_ARRAY && newtype != JSON_ARRAY ) { \ osrfListFree(_obj_->value.l); \ _obj_->value.l = NULL; \ -} else if( _obj_->type == JSON_STRING || _obj_->type == JSON_NUMBER ) { \ - free(_obj_->value.s); \ + } else if( _obj_->type == JSON_STRING || _obj_->type == JSON_NUMBER ) { \ + free(_obj_->value.s); \ _obj_->value.s = NULL; \ -} \ - _obj_->type = newtype;\ + } else if( _obj_->type == JSON_BOOL && newtype != JSON_BOOL ) { \ + _obj_->value.l = NULL; \ + } \ + _obj_->type = newtype; \ if( newtype == JSON_HASH && _obj_->value.h == NULL ) { \ _obj_->value.h = osrfNewHash(); \ osrfHashSetCallback( _obj_->value.h, _jsonFreeHashItem ); \ -} else if( newtype == JSON_ARRAY && _obj_->value.l == NULL ) { \ + } else if( newtype == JSON_ARRAY && _obj_->value.l == NULL ) { \ _obj_->value.l = osrfNewList(); \ _obj_->value.l->freeItem = _jsonFreeListItem;\ -} + } static int unusedObjCapture = 0; static int unusedObjRelease = 0; -- 2.11.0