Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp1335351pxb; Thu, 7 Oct 2021 05:53:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxANdM6VLkyJ/QHf+/N3G7bo+QJU45YW03z3+fIJ3pHj901+wHSDayVtVTygy9riSIzy8cp X-Received: by 2002:a17:90a:dc81:: with SMTP id j1mr4596512pjv.155.1633611192716; Thu, 07 Oct 2021 05:53:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633611192; cv=none; d=google.com; s=arc-20160816; b=cFq1OJIS+lFMos5x87u17u3Y36Q0aWVd70vJw24Pg1yGv7g4WudPBQX1dYqtqUfo2P xA4FLSg4fP77XHymyBHNyKU8mmjndZ3zN6lh2rBtisW1tvrMRU608Aa3b4kJW2owYDIA h0lzapnnJeBZzn73qo2X7rewhWjIBJLG+DCiNDkvD8mjmlc5SZy/RTnQma7f3wWMh5o0 UKVoW5V8yq4aDZYl4wLLcox1cZx9kt4zXn17oULG24FM/B80jI/G8uPjNEZQeGW562QF RjmmO2bEF9WwI+uHKJU0eSXMCutzyoyCl0ST8DI0eVT4h1KPpbJSe2/s7az39dFpe3tw wzPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=28x7xNvDreg6PcqsSO52124hMeZf5rBiU19sjhCHadU=; b=rclYsuvJBGPXPKqQ/hkLCq9eI2CP59LqGNl5OOckfww9w3ISMQNmglxydIgzOoN/Ih apwC2+aV02g8xyH9mEzvXlJaJ+j6bKZ0uBlu2JyfaZRtC4S3Kn1lohJBK0YvHEVz3CPb 3nv7hE0RpoMz19/ntKLVBJsQpuJkYou2rWiDZtRfI9g2ZBzMaTXoZWZhUUhY4KLJauiI OoioPTONkzWde2i529QG/dLb4lnK3WV1/dJgqoDHblofI4qH02uCxxkt+HrQ6ZD0EzGU OuXqkKwZkbAi9Jazxili3h4UIKxVVUKFgzl8+DtyK5m54Zxy0haOGUz4twZqgTjSSIvd n2UQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b5si29024945pls.376.2021.10.07.05.52.58; Thu, 07 Oct 2021 05:53:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240942AbhJGLIF (ORCPT + 99 others); Thu, 7 Oct 2021 07:08:05 -0400 Received: from foss.arm.com ([217.140.110.172]:45870 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240919AbhJGLIF (ORCPT ); Thu, 7 Oct 2021 07:08:05 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 5587D113E; Thu, 7 Oct 2021 04:06:11 -0700 (PDT) Received: from e121896.Emea.Arm.com (e121896.Emea.Arm.com [10.32.36.22]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 99DBB3F70D; Thu, 7 Oct 2021 04:06:07 -0700 (PDT) From: James Clark To: acme@kernel.org, john.garry@huawei.com, ak@linux.intel.com, linux-perf-users@vger.kernel.org Cc: Nick.Forrington@arm.com, Andrew.Kilroy@arm.com, James Clark , Will Deacon , Mathieu Poirier , Leo Yan , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/3] perf tools: Make the JSON parser more conformant when in strict mode Date: Thu, 7 Oct 2021 12:05:41 +0100 Message-Id: <20211007110543.564963-3-james.clark@arm.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20211007110543.564963-1-james.clark@arm.com> References: <20211007110543.564963-1-james.clark@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return an error when a trailing comma is found or a new item is encountered before a comma or an opening brace. This ensures that the perf json files conform more closely to the spec at https://www.json.org Signed-off-by: James Clark --- tools/perf/pmu-events/jsmn.c | 42 ++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/tools/perf/pmu-events/jsmn.c b/tools/perf/pmu-events/jsmn.c index 11d1fa18bfa5..8124d2d3ff0c 100644 --- a/tools/perf/pmu-events/jsmn.c +++ b/tools/perf/pmu-events/jsmn.c @@ -176,6 +176,14 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, jsmnerr_t r; int i; jsmntok_t *token; +#ifdef JSMN_STRICT + /* + * Keeps track of whether a new object/list/primitive is expected. New items are only + * allowed after an opening brace, comma or colon. A closing brace after a comma is not + * valid JSON. + */ + int expecting_item = 1; +#endif for (; parser->pos < len; parser->pos++) { char c; @@ -185,6 +193,10 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, switch (c) { case '{': case '[': +#ifdef JSMN_STRICT + if (!expecting_item) + return JSMN_ERROR_INVAL; +#endif token = jsmn_alloc_token(parser, tokens, num_tokens); if (token == NULL) return JSMN_ERROR_NOMEM; @@ -196,6 +208,10 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, break; case '}': case ']': +#ifdef JSMN_STRICT + if (expecting_item) + return JSMN_ERROR_INVAL; +#endif type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY); for (i = parser->toknext - 1; i >= 0; i--) { token = &tokens[i]; @@ -219,6 +235,11 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, } break; case '\"': +#ifdef JSMN_STRICT + if (!expecting_item) + return JSMN_ERROR_INVAL; + expecting_item = 0; +#endif r = jsmn_parse_string(parser, js, len, tokens, num_tokens); if (r < 0) @@ -229,11 +250,15 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, case '\t': case '\r': case '\n': - case ':': - case ',': case ' ': break; #ifdef JSMN_STRICT + case ':': + case ',': + if (expecting_item) + return JSMN_ERROR_INVAL; + expecting_item = 1; + break; /* * In strict mode primitives are: * numbers and booleans. @@ -253,6 +278,9 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, case 'f': case 'n': #else + case ':': + case ',': + break; /* * In non-strict mode every unquoted value * is a primitive. @@ -260,6 +288,12 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, /*FALL THROUGH */ default: #endif + +#ifdef JSMN_STRICT + if (!expecting_item) + return JSMN_ERROR_INVAL; + expecting_item = 0; +#endif r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens); if (r < 0) @@ -282,7 +316,11 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, return JSMN_ERROR_PART; } +#ifdef JSMN_STRICT + return expecting_item ? JSMN_ERROR_INVAL : JSMN_SUCCESS; +#else return JSMN_SUCCESS; +#endif } /* -- 2.28.0