From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stephen Hemminger Subject: Re: [PATCH] test: fix sprintf with snprintf Date: Fri, 8 Feb 2019 08:21:39 -0800 Message-ID: <20190208082139.57e8e060@hermes.lan> References: <1549632457-15892-1-git-send-email-pallantlax.poornima@intel.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: dev@dpdk.org, reshma.pattan@intel.com, ferruh.yigit@intel.com, stable@dpdk.org To: Pallantla Poornima Return-path: Received: from mail-pf1-f195.google.com (mail-pf1-f195.google.com [209.85.210.195]) by dpdk.org (Postfix) with ESMTP id 678301B627 for ; Fri, 8 Feb 2019 17:21:48 +0100 (CET) Received: by mail-pf1-f195.google.com with SMTP id n74so625269pfi.9 for ; Fri, 08 Feb 2019 08:21:48 -0800 (PST) In-Reply-To: <1549632457-15892-1-git-send-email-pallantlax.poornima@intel.com> 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 Fri, 8 Feb 2019 13:27:37 +0000 Pallantla Poornima wrote: > diff --git a/test/test/commands.c b/test/test/commands.c > index 94fbc310e..5aeb35498 100644 > --- a/test/test/commands.c > +++ b/test/test/commands.c > @@ -367,6 +367,8 @@ int commands_init(void) > struct test_command *t; > char *commands, *ptr; > int commands_len = 0; > + int total_written = 0; > + int count = 0; > > TAILQ_FOREACH(t, &commands_list, next) { > commands_len += strlen(t->command) + 1; > @@ -378,7 +380,10 @@ int commands_init(void) > > ptr = commands; > TAILQ_FOREACH(t, &commands_list, next) { > - ptr += sprintf(ptr, "%s#", t->command); > + count = snprintf(ptr, commands_len - total_written - 1, "%s#", > + t->command); > + ptr += count; > + total_written += count; You know snprintf is dangerous in this case as well. It returns the number of bytes that would have been written. That is why the linux kernel introduced scnprintf.