From a3199e8efe32df24e68c4a9f6f9ecc04c24511ca Mon Sep 17 00:00:00 2001 From: Ken Zangelin Date: Thu, 15 Dec 2022 13:36:04 +0100 Subject: [PATCH] Fixed a crash --- CHANGES_NEXT_RELEASE | 3 +- .../serviceRoutines/orionldPatchEntity2.cpp | 7 + ...ntity2-attr-with-empty-array-as-value.test | 156 ++++++++++++++++++ 3 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 test/functionalTest/cases/0000_ngsild/ngsild_patch_entity2-attr-with-empty-array-as-value.test diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 7695d8f56f..4d57f06b4e 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,2 +1,3 @@ Fixed Issues: -* Issue #1227 Case insensitive matching for http request header keys +* Issue #280 Crash when an attributed is PATCHed to have an empty array as value + diff --git a/src/lib/orionld/serviceRoutines/orionldPatchEntity2.cpp b/src/lib/orionld/serviceRoutines/orionldPatchEntity2.cpp index 7d2ae69537..123a90ed49 100644 --- a/src/lib/orionld/serviceRoutines/orionldPatchEntity2.cpp +++ b/src/lib/orionld/serviceRoutines/orionldPatchEntity2.cpp @@ -325,6 +325,13 @@ bool kjValuesDiffer(KjNode* leftAttr, KjNode* rightAttr) if (type == KjBoolean) return (left->value.b == right->value.b)? false : true; // Compound values ... let's just render the values and do a strcmp on the rendered buffers + // However, might be an empty array/object (kjFastRenderSize crashes if the parameter is NULL) + // + if ((left->value.firstChildP == NULL) && (right->value.firstChildP == NULL)) + return false; + else if ((left->value.firstChildP == NULL) || (right->value.firstChildP == NULL)) + return true; + int leftBufSize = kjFastRenderSize(left->value.firstChildP); int rightBufSize = kjFastRenderSize(right->value.firstChildP); char* leftBuf = kaAlloc(&orionldState.kalloc, leftBufSize); diff --git a/test/functionalTest/cases/0000_ngsild/ngsild_patch_entity2-attr-with-empty-array-as-value.test b/test/functionalTest/cases/0000_ngsild/ngsild_patch_entity2-attr-with-empty-array-as-value.test new file mode 100644 index 0000000000..15b54621a1 --- /dev/null +++ b/test/functionalTest/cases/0000_ngsild/ngsild_patch_entity2-attr-with-empty-array-as-value.test @@ -0,0 +1,156 @@ +# Copyright 2022 FIWARE Foundation e.V. +# +# This file is part of Orion-LD Context Broker. +# +# Orion-LD Context Broker is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Orion-LD Context Broker is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/. +# +# For those usages not covered by this license please contact with +# orionld at fiware dot org + +# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh + +--NAME-- +Update an entity attribute using PATCH + +--SHELL-INIT-- +export BROKER=orionld +dbInit CB +brokerStart CB 0 IPv4 -experimental + +--SHELL-- +# +# 01. Create an entity E1 with an attribute P1=[] +# 02. Patch E1, setting P1=[] - make sure there's no crash +# 03. GET E1 +# 04. Patch E1, setting P1=[] - again +# 05. GET E1 +# + +echo "01. Create an entity E1 with an attribute P1=1" +echo "==============================================" +payload='{ + "id": "urn:E1", + "type": "T", + "@context": [ "https://fiware.github.io/NGSI-LD_TestSuite/ldContext/testContext.jsonld" ] +}' +orionCurl --url /ngsi-ld/v1/entities --payload "$payload" -H "Content-Type: application/ld+json" +echo +echo + + +echo "02. Patch E1, setting P1=[] - make sure there's no crash" +echo "========================================================" +payload='{ + "@context": [ "https://fiware.github.io/NGSI-LD_TestSuite/ldContext/testContext.jsonld" ], + "P1": { + "type": "Property", + "value": [] + } +}' +orionCurl --url /ngsi-ld/v1/entities/urn:E1 -X PATCH --payload "$payload" -H "Content-Type: application/ld+json" +echo +echo + + +echo "03. GET E1" +echo "==========" +orionCurl --url /ngsi-ld/v1/entities/urn:E1 +echo +echo + + +echo "04. Patch E1, setting P1=[] - again" +echo "===================================" +payload='{ + "@context": [ "https://fiware.github.io/NGSI-LD_TestSuite/ldContext/testContext.jsonld" ], + "P1": { + "type": "Property", + "value": [] + } +}' +orionCurl --url /ngsi-ld/v1/entities/urn:E1 -X PATCH --payload "$payload" -H "Content-Type: application/ld+json" +echo +echo + + +echo "05. GET E1" +echo "==========" +orionCurl --url /ngsi-ld/v1/entities/urn:E1 +echo +echo + + +--REGEXPECT-- +01. Create an entity E1 with an attribute P1=1 +============================================== +HTTP/1.1 201 Created +Content-Length: 0 +Date: REGEX(.*) +Location: /ngsi-ld/v1/entities/urn:E1 + + + +02. Patch E1, setting P1=[] - make sure there's no crash +======================================================== +HTTP/1.1 204 No Content +Date: REGEX(.*) + + + +03. GET E1 +========== +HTTP/1.1 200 OK +Content-Length: 100 +Content-Type: application/json +Date: REGEX(.*) +Link: ; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json" + +{ + "http://example.org/P1": { + "type": "Property", + "value": [] + }, + "id": "urn:E1", + "type": "http://example.org/T" +} + + +04. Patch E1, setting P1=[] - again +=================================== +HTTP/1.1 204 No Content +Date: REGEX(.*) + + + +05. GET E1 +========== +HTTP/1.1 200 OK +Content-Length: 100 +Content-Type: application/json +Date: REGEX(.*) +Link: ; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json" + +{ + "http://example.org/P1": { + "type": "Property", + "value": [] + }, + "id": "urn:E1", + "type": "http://example.org/T" +} + + +--TEARDOWN-- +brokerStop CB +dbDrop CB