From patchwork Mon Apr 3 16:30:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tyler Retzlaff X-Patchwork-Id: 125727 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 9992B428C0; Mon, 3 Apr 2023 18:30:33 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 03CBA41153; Mon, 3 Apr 2023 18:30:29 +0200 (CEST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mails.dpdk.org (Postfix) with ESMTP id 6958F40A7E for ; Mon, 3 Apr 2023 18:30:26 +0200 (CEST) Received: by linux.microsoft.com (Postfix, from userid 1086) id 860C1210CBE7; Mon, 3 Apr 2023 09:30:25 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 860C1210CBE7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680539425; bh=Kur5UyEDHBWRLVlPWbiwpXa9H/ovNYVZ6BSYmwanHV8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kRdlUm9K+ngEvLivZlpMXZrH2aar7HuwVBs8SRMABhQ5DlAYI+/qTVItql7ywc8dK jgvaZh5z0Rj396DL08JjFJjwDNe2JhgjeZoFyaaXPm90OM0qjugtP2qhvHjkpoQfBH DmMWhiuvI1ThktI/2ay8MRFC2L3HqWWjvm8J4PLU= From: Tyler Retzlaff To: dev@dpdk.org Cc: ciara.power@intel.com, bruce.richardson@intel.com, david.marchand@redhat.com, thomas@monjalon.net, Tyler Retzlaff Subject: [PATCH 1/2] telemetry: use malloc instead of variable length array Date: Mon, 3 Apr 2023 09:30:23 -0700 Message-Id: <1680539424-20255-2-git-send-email-roretzla@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1680539424-20255-1-git-send-email-roretzla@linux.microsoft.com> References: <1680539424-20255-1-git-send-email-roretzla@linux.microsoft.com> X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Replace use of variable length array optional standard feature to improve portability. Signed-off-by: Tyler Retzlaff --- lib/telemetry/telemetry_json.h | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/telemetry/telemetry_json.h b/lib/telemetry/telemetry_json.h index 744bbfe..c375e97 100644 --- a/lib/telemetry/telemetry_json.h +++ b/lib/telemetry/telemetry_json.h @@ -30,18 +30,23 @@ static inline int __json_snprintf(char *buf, const int len, const char *format, ...) { - char tmp[len]; + char *tmp = malloc(len); va_list ap; - int ret; + int ret = 0; + + if (tmp == NULL) + return ret; va_start(ap, format); ret = vsnprintf(tmp, sizeof(tmp), format, ap); va_end(ap); if (ret > 0 && ret < (int)sizeof(tmp) && ret < len) { strcpy(buf, tmp); - return ret; } - return 0; /* nothing written or modified */ + + free(tmp); + + return ret; } static const char control_chars[0x20] = { @@ -60,13 +65,17 @@ static inline int __json_format_str(char *buf, const int len, const char *prefix, const char *str, const char *suffix) { - char tmp[len]; int tmpidx = 0; + int ret = 0; + char *tmp = malloc(len); + + if (tmp == NULL) + return ret; while (*prefix != '\0' && tmpidx < len) tmp[tmpidx++] = *prefix++; if (tmpidx >= len) - return 0; + goto cleanup; while (*str != '\0') { if (*str < (int)RTE_DIM(control_chars)) { @@ -85,19 +94,24 @@ * escaped character like "\n" without overflowing */ if (tmpidx > len - 2) - return 0; + goto cleanup; str++; } while (*suffix != '\0' && tmpidx < len) tmp[tmpidx++] = *suffix++; if (tmpidx >= len) - return 0; + goto cleanup; tmp[tmpidx] = '\0'; strcpy(buf, tmp); - return tmpidx; + ret = tmpidx; + +cleanup: + free(tmp); + + return ret; } /* Copies an empty array into the provided buffer. */ From patchwork Mon Apr 3 16:30:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tyler Retzlaff X-Patchwork-Id: 125728 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id F344C428C0; Mon, 3 Apr 2023 18:30:39 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 17DEE42B71; Mon, 3 Apr 2023 18:30:30 +0200 (CEST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mails.dpdk.org (Postfix) with ESMTP id 7495440ED7 for ; Mon, 3 Apr 2023 18:30:26 +0200 (CEST) Received: by linux.microsoft.com (Postfix, from userid 1086) id 925B5210CBEC; Mon, 3 Apr 2023 09:30:25 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 925B5210CBEC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680539425; bh=J/w0juR/Qh7aR/O/9S5k+flJ8crTbWVTzb74CNRWMsM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SZHW0yPprAR0VfvL3FWslUKvWgQ4CRMEvpu+r+LCwc728g/anqga92r7V9hFrLk/0 6I/gcHo5c9pvJHaSHIbjaWZJBLii10l7hQVcPpS3alduxU2TzUz+jtBBCyF55SZA2h G3/dQ/15YvN2QmrKXNk8g48fttxeJkvv2PaECR0I= From: Tyler Retzlaff To: dev@dpdk.org Cc: ciara.power@intel.com, bruce.richardson@intel.com, david.marchand@redhat.com, thomas@monjalon.net, Tyler Retzlaff Subject: [PATCH 2/2] telemetry: use portable syntax to initialize array Date: Mon, 3 Apr 2023 09:30:24 -0700 Message-Id: <1680539424-20255-3-git-send-email-roretzla@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1680539424-20255-1-git-send-email-roretzla@linux.microsoft.com> References: <1680539424-20255-1-git-send-email-roretzla@linux.microsoft.com> X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Use of ranges in designated initialization are a non-standard gcc extension. Manually expand the ranges used in designated initialization to eliminate the use of non-standard features. Signed-off-by: Tyler Retzlaff --- lib/telemetry/telemetry_data.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/telemetry/telemetry_data.c b/lib/telemetry/telemetry_data.c index 2bac2de..87e5f18 100644 --- a/lib/telemetry/telemetry_data.c +++ b/lib/telemetry/telemetry_data.c @@ -152,12 +152,20 @@ static bool valid_name(const char *name) { - char allowed[128] = { - ['0' ... '9'] = 1, - ['A' ... 'Z'] = 1, - ['a' ... 'z'] = 1, - ['_'] = 1, - ['/'] = 1, + static const char allowed[128] = { + ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, + ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, + ['A'] = 1, ['B'] = 1, ['C'] = 1, ['D'] = 1, ['E'] = 1, + ['F'] = 1, ['G'] = 1, ['H'] = 1, ['I'] = 1, ['J'] = 1, + ['K'] = 1, ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, + ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1, + ['U'] = 1, ['V'] = 1, ['W'] = 1, ['X'] = 1, ['Y'] = 1, + ['Z'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, ['d'] = 1, + ['e'] = 1, ['f'] = 1, ['g'] = 1, ['h'] = 1, ['i'] = 1, + ['j'] = 1, ['k'] = 1, ['l'] = 1, ['m'] = 1, ['n'] = 1, + ['o'] = 1, ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, + ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, ['x'] = 1, + ['y'] = 1, ['z'] = 1, ['_'] = 1, ['/'] = 1, }; while (*name != '\0') { if ((size_t)*name >= RTE_DIM(allowed) || allowed[(int)*name] == 0)