From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 04EFDA0597; Thu, 9 Apr 2020 10:19:30 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 1E37E1C1B6; Thu, 9 Apr 2020 10:19:30 +0200 (CEST) Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by dpdk.org (Postfix) with ESMTP id E67F81C1B4 for ; Thu, 9 Apr 2020 10:19:27 +0200 (CEST) IronPort-SDR: JWLlK0/hpa9mejT/Z7bifW9mCtEOvzYU3ojyrNXwgtsGYWRik1qzFV24GkgKF9wxDvkqf32waN t11WziYaczig== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Apr 2020 01:19:26 -0700 IronPort-SDR: WQUxWv8eBB3HDl86JgcPlXxpUlBoEzBKeEtk9kj+psk5jvrhPd46C70/7vqdxnBrVzxFmiFbeJ BeNM+1gLmdzA== X-IronPort-AV: E=Sophos;i="5.72,362,1580803200"; d="scan'208";a="240554540" Received: from ndavidso-mobl4.ger.corp.intel.com (HELO bricha3-MOBL.ger.corp.intel.com) ([10.214.222.217]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-SHA; 09 Apr 2020 01:19:23 -0700 Date: Thu, 9 Apr 2020 09:19:18 +0100 From: Bruce Richardson To: "Wiles, Keith" Cc: "Power, Ciara" , dev , "Laatz, Kevin" , "Pattan, Reshma" , "jerinjacobk@gmail.com" , "david.marchand@redhat.com" , "mb@smartsharesystems.com" , "thomas@monjalon.net" Message-ID: <20200409081918.GA605@bricha3-MOBL.ger.corp.intel.com> References: <20200319171907.60891-1-ciara.power@intel.com> <20200408164956.47864-1-ciara.power@intel.com> <20200408164956.47864-7-ciara.power@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Subject: Re: [dpdk-dev] [PATCH v2 06/16] telemetry: add utility functions for creating json X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" On Wed, Apr 08, 2020 at 07:12:57PM +0100, Wiles, Keith wrote: > > > > On Apr 8, 2020, at 11:49 AM, Power, Ciara wrote: > > > > From: Bruce Richardson > > > > The functions added in this patch will make it easier for applications > > to build up correct JSON responses to telemetry requests. > > > > Signed-off-by: Bruce Richardson > > --- > > lib/librte_telemetry/Makefile | 1 + > > lib/librte_telemetry/meson.build | 2 +- > > lib/librte_telemetry/rte_telemetry.h | 1 + > > lib/librte_telemetry/rte_telemetry_json.h | 205 ++++++++++++++++++++++ > > 4 files changed, 208 insertions(+), 1 deletion(-) > > create mode 100644 lib/librte_telemetry/rte_telemetry_json.h > > +/** > > + * @internal > > + * > > + * Copies a value into a buffer if the buffer has enough available space. > > + * Nothing written to buffer if an overflow ocurs. > > + * This function is not for use for values larger than 1k. > > + * > > + * @param buf > > + * Buffer for data to be appended to. > > + * @param len > > + * Length of buffer. > > + * @param format > > + * Format string. > > + * @param ... > > + * Optional arguments that may be required by the format string. > > + * > > + * @return > > + * Number of characters added to buffer > > + */ > > +__attribute__((__format__(__printf__, 3, 4))) > > +static inline int > > +__json_snprintf(char *buf, const int len, const char *format, ...) > > +{ > > +char tmp[1024]; > > +va_list ap; > > +int ret; > > + > > +va_start(ap, format); > > +ret = vsnprintf(tmp, sizeof(tmp), format, ap); > > +va_end(ap); > > Would strlcpy(buf, tmp, len); reduce the test below? vsnprintf() writes at most size bytes including the ‘\0’ to the buffer. Maybe able to reduce the code to this: > > return strlcpy(buf, tmp, len);// strlcpy returns the total length of buf. > > This means the ret is not really required, unless I missed something. > > > +if (ret > 0 && ret < (int)sizeof(tmp) && ret < len) { > > +strcpy(buf, tmp); > > +return ret; > > +} > > +return 0; /* nothing written or modified */ > > +} > > + The intent here is that the buffer remains unmodified and the function returns zero in the case that the printf fails to work. Truncation would likely leave us with invalid json, so it's all or nothing, which is why we can't just take the snprintf or strlcpy. [On overflow they still modify the buffer, which means any closing "]" or "}" in the json would be overwritten.] /Bruce