[{"id":1762706,"web_url":"http://patchwork.ozlabs.org/comment/1762706/","msgid":"<063D6719AE5E284EB5DD2968C1650D6DD006D878@AcuExch.aculab.com>","list_archive_url":null,"date":"2017-09-04T14:49:20","subject":"RE: [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()","submitter":{"id":6689,"url":"http://patchwork.ozlabs.org/api/people/6689/","name":"David Laight","email":"David.Laight@ACULAB.COM"},"content":"From: Phil Sutter\n> Sent: 01 September 2017 17:53\n> By making use of strncpy(), both implementations are really simple so\n> there is no need to add libbsd as additional dependency.\n> \n...\n> +\n> +size_t strlcpy(char *dst, const char *src, size_t size)\n> +{\n> +\tif (size) {\n> +\t\tstrncpy(dst, src, size - 1);\n> +\t\tdst[size - 1] = '\\0';\n> +\t}\n> +\treturn strlen(src);\n> +}\n\nExcept that isn't really strlcpy().\nBetter would be:\n\tlen = strlen(src) + 1;\n\tif (len <= size)\n\t\tmemcpy(dst, src, len);\n\telse if (size) {\n\t\tdst[size - 1] = 0;\n\t\tmemcpy(dst, src, size - 1);\n\t}\n\treturn len - 1;\n\nWTF strlcpy() has that return value I don't know.\n\n\tDavid","headers":{"Return-Path":"<netdev-owner@vger.kernel.org>","X-Original-To":"patchwork-incoming@ozlabs.org","Delivered-To":"patchwork-incoming@ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=netdev-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xmCRX6SHKz9t2R\n\tfor <patchwork-incoming@ozlabs.org>;\n\tTue,  5 Sep 2017 00:49:28 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1753749AbdIDOt0 convert rfc822-to-8bit (ORCPT\n\t<rfc822;patchwork-incoming@ozlabs.org>);\n\tMon, 4 Sep 2017 10:49:26 -0400","from smtp-out4.electric.net ([192.162.216.192]:54145 \"EHLO\n\tsmtp-out4.electric.net\" rhost-flags-OK-OK-OK-OK) by vger.kernel.org\n\twith ESMTP id S1753662AbdIDOtZ (ORCPT\n\t<rfc822;netdev@vger.kernel.org>); Mon, 4 Sep 2017 10:49:25 -0400","from 1dosgk-0005s4-VI by out4c.electric.net with emc1-ok (Exim\n\t4.87) (envelope-from <David.Laight@ACULAB.COM>)\n\tid 1dosgl-0005vg-Um; Mon, 04 Sep 2017 07:49:23 -0700","by emcmailer; Mon, 04 Sep 2017 07:49:23 -0700","from [156.67.243.126] (helo=AcuExch.aculab.com)\n\tby out4c.electric.net with esmtps (TLSv1:AES128-SHA:128)\n\t(Exim 4.87) (envelope-from <David.Laight@ACULAB.COM>)\n\tid 1dosgk-0005s4-VI; Mon, 04 Sep 2017 07:49:22 -0700","from ACUEXCH.Aculab.com ([::1]) by AcuExch.aculab.com ([::1]) with\n\tmapi id 14.03.0123.003; Mon, 4 Sep 2017 15:49:21 +0100"],"From":"David Laight <David.Laight@ACULAB.COM>","To":"'Phil Sutter' <phil@nwl.cc>,\n\tStephen Hemminger <stephen@networkplumber.org>","CC":"\"netdev@vger.kernel.org\" <netdev@vger.kernel.org>","Subject":"RE: [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()","Thread-Topic":"[iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()","Thread-Index":"AQHTI0LREKBHvt3L3kuHqHR1fKMkjKKk0iow","Date":"Mon, 4 Sep 2017 14:49:20 +0000","Message-ID":"<063D6719AE5E284EB5DD2968C1650D6DD006D878@AcuExch.aculab.com>","References":"<20170901165256.21459-1-phil@nwl.cc>\n\t<20170901165256.21459-2-phil@nwl.cc>","In-Reply-To":"<20170901165256.21459-2-phil@nwl.cc>","Accept-Language":"en-GB, en-US","Content-Language":"en-US","X-MS-Has-Attach":"","X-MS-TNEF-Correlator":"","x-originating-ip":"[10.202.99.200]","Content-Type":"text/plain; charset=\"Windows-1252\"","Content-Transfer-Encoding":"8BIT","MIME-Version":"1.0","X-Outbound-IP":"156.67.243.126","X-Env-From":"David.Laight@ACULAB.COM","X-Proto":"esmtps","X-Revdns":"","X-HELO":"AcuExch.aculab.com","X-TLS":"TLSv1:AES128-SHA:128","X-Authenticated_ID":"","X-PolicySMART":"3396946, 3397078","X-Virus-Status":["Scanned by VirusSMART (c)","Scanned by VirusSMART (s)"],"Sender":"netdev-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<netdev.vger.kernel.org>","X-Mailing-List":"netdev@vger.kernel.org"}},{"id":1762715,"web_url":"http://patchwork.ozlabs.org/comment/1762715/","msgid":"<20170904150015.GB30364@orbyte.nwl.cc>","list_archive_url":null,"date":"2017-09-04T15:00:15","subject":"Re: [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()","submitter":{"id":4285,"url":"http://patchwork.ozlabs.org/api/people/4285/","name":"Phil Sutter","email":"phil@nwl.cc"},"content":"On Mon, Sep 04, 2017 at 02:49:20PM +0000, David Laight wrote:\n> From: Phil Sutter\n> > Sent: 01 September 2017 17:53\n> > By making use of strncpy(), both implementations are really simple so\n> > there is no need to add libbsd as additional dependency.\n> > \n> ...\n> > +\n> > +size_t strlcpy(char *dst, const char *src, size_t size)\n> > +{\n> > +\tif (size) {\n> > +\t\tstrncpy(dst, src, size - 1);\n> > +\t\tdst[size - 1] = '\\0';\n> > +\t}\n> > +\treturn strlen(src);\n> > +}\n> \n> Except that isn't really strlcpy().\n> Better would be:\n> \tlen = strlen(src) + 1;\n> \tif (len <= size)\n> \t\tmemcpy(dst, src, len);\n> \telse if (size) {\n> \t\tdst[size - 1] = 0;\n> \t\tmemcpy(dst, src, size - 1);\n> \t}\n> \treturn len - 1;\n\nPlease elaborate: Why isn't my version \"really\" strlcpy()? Why is your\nproposed version better?\n\nThanks, Phil","headers":{"Return-Path":"<netdev-owner@vger.kernel.org>","X-Original-To":"patchwork-incoming@ozlabs.org","Delivered-To":"patchwork-incoming@ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=netdev-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xmCh35nxkz9t2W\n\tfor <patchwork-incoming@ozlabs.org>;\n\tTue,  5 Sep 2017 01:00:19 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1753803AbdIDPAR (ORCPT <rfc822;patchwork-incoming@ozlabs.org>);\n\tMon, 4 Sep 2017 11:00:17 -0400","from orbyte.nwl.cc ([151.80.46.58]:57987 \"EHLO mail.nwl.cc\"\n\trhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP\n\tid S1753801AbdIDPAQ (ORCPT <rfc822;netdev@vger.kernel.org>);\n\tMon, 4 Sep 2017 11:00:16 -0400","from mail.nwl.cc (orbyte.nwl.cc [127.0.0.1])\n\tby mail.nwl.cc (Postfix) with ESMTP id 373526385C;\n\tMon,  4 Sep 2017 17:00:15 +0200 (CEST)","by mail.nwl.cc (Postfix, from userid 1000)\n\tid 2615365A5E; Mon,  4 Sep 2017 17:00:15 +0200 (CEST)"],"Date":"Mon, 4 Sep 2017 17:00:15 +0200","From":"Phil Sutter <phil@nwl.cc>","To":"David Laight <David.Laight@ACULAB.COM>","Cc":"Stephen Hemminger <stephen@networkplumber.org>,\n\t\"netdev@vger.kernel.org\" <netdev@vger.kernel.org>","Subject":"Re: [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()","Message-ID":"<20170904150015.GB30364@orbyte.nwl.cc>","Mail-Followup-To":"Phil Sutter <phil@nwl.cc>,\n\tDavid Laight <David.Laight@ACULAB.COM>,\n\tStephen Hemminger <stephen@networkplumber.org>,\n\t\"netdev@vger.kernel.org\" <netdev@vger.kernel.org>","References":"<20170901165256.21459-1-phil@nwl.cc>\n\t<20170901165256.21459-2-phil@nwl.cc>\n\t<063D6719AE5E284EB5DD2968C1650D6DD006D878@AcuExch.aculab.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<063D6719AE5E284EB5DD2968C1650D6DD006D878@AcuExch.aculab.com>","User-Agent":"Mutt/1.7.2 (2016-11-26)","X-Virus-Scanned":"ClamAV using ClamSMTP","Sender":"netdev-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<netdev.vger.kernel.org>","X-Mailing-List":"netdev@vger.kernel.org"}},{"id":1762833,"web_url":"http://patchwork.ozlabs.org/comment/1762833/","msgid":"<20170904112525.04bc66d5@xeon-e3>","list_archive_url":null,"date":"2017-09-04T18:25:25","subject":"Re: [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()","submitter":{"id":21389,"url":"http://patchwork.ozlabs.org/api/people/21389/","name":"Stephen Hemminger","email":"stephen@networkplumber.org"},"content":"On Mon, 4 Sep 2017 17:00:15 +0200\nPhil Sutter <phil@nwl.cc> wrote:\n\n> On Mon, Sep 04, 2017 at 02:49:20PM +0000, David Laight wrote:\n> > From: Phil Sutter  \n> > > Sent: 01 September 2017 17:53\n> > > By making use of strncpy(), both implementations are really simple so\n> > > there is no need to add libbsd as additional dependency.\n> > >   \n> > ...  \n> > > +\n> > > +size_t strlcpy(char *dst, const char *src, size_t size)\n> > > +{\n> > > +\tif (size) {\n> > > +\t\tstrncpy(dst, src, size - 1);\n> > > +\t\tdst[size - 1] = '\\0';\n> > > +\t}\n> > > +\treturn strlen(src);\n> > > +}  \n> > \n> > Except that isn't really strlcpy().\n> > Better would be:\n> > \tlen = strlen(src) + 1;\n> > \tif (len <= size)\n> > \t\tmemcpy(dst, src, len);\n> > \telse if (size) {\n> > \t\tdst[size - 1] = 0;\n> > \t\tmemcpy(dst, src, size - 1);\n> > \t}\n> > \treturn len - 1;  \n> \n> Please elaborate: Why isn't my version \"really\" strlcpy()? Why is your\n> proposed version better?\n> \n> Thanks, Phil\n\nLinux kernel:\nsize_t strlcpy(char *dest, const char *src, size_t size)\n{\n\tsize_t ret = strlen(src);\n\n\tif (size) {\n\t\tsize_t len = (ret >= size) ? size - 1 : ret;\n\t\tmemcpy(dest, src, len);\n\t\tdest[len] = '\\0';\n\t}\n\treturn ret;\n}\n\nFreeBSD:\nsize_t\nstrlcpy(char * __restrict dst, const char * __restrict src, size_t dsize)\n{\n\tconst char *osrc = src;\n\tsize_t nleft = dsize;\n\n\t/* Copy as many bytes as will fit. */\n\tif (nleft != 0) {\n\t\twhile (--nleft != 0) {\n\t\t\tif ((*dst++ = *src++) == '\\0')\n\t\t\t\tbreak;\n\t\t}\n\t}\n\n\t/* Not enough room in dst, add NUL and traverse rest of src. */\n\tif (nleft == 0) {\n\t\tif (dsize != 0)\n\t\t\t*dst = '\\0';\t\t/* NUL-terminate dst */\n\t\twhile (*src++)\n\t\t\t;\n\t}\n\n\treturn(src - osrc - 1);\t/* count does not include NUL */\n}\n\n\nThey all give the same results for some basic tests.\nTest\t\t\tFreeBSD\t\tLinux\t\tIproute2\n\"\",0:           \t0 \"JUNK\"      \t0 \"JUNK\"      \t0 \"JUNK\"      \n\"\",1:           \t0 \"\"          \t0 \"\"          \t0 \"\"          \n\"\",8:           \t0 \"\"          \t0 \"\"          \t0 \"\"          \n\"foo\",0:        \t3 \"JUNK\"      \t3 \"JUNK\"      \t3 \"JUNK\"      \n\"foo\",3:        \t3 \"fo\"        \t3 \"fo\"        \t3 \"fo\"        \n\"foo\",4:        \t3 \"foo\"       \t3 \"foo\"       \t3 \"foo\"       \n\"foo\",8:        \t3 \"foo\"       \t3 \"foo\"       \t3 \"foo\"       \n\"longstring\",0: \t10 \"JUNK\"     \t10 \"JUNK\"     \t10 \"JUNK\"     \n\"longstring\",8: \t10 \"longstr\"  \t10 \"longstr\"  \t10 \"longstr\"","headers":{"Return-Path":"<netdev-owner@vger.kernel.org>","X-Original-To":"patchwork-incoming@ozlabs.org","Delivered-To":"patchwork-incoming@ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=netdev-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","ozlabs.org; dkim=pass (2048-bit key;\n\tunprotected) header.d=networkplumber-org.20150623.gappssmtp.com\n\theader.i=@networkplumber-org.20150623.gappssmtp.com\n\theader.b=\"uL7uDggv\"; dkim-atps=neutral"],"Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xmJF26jMLz9sP3\n\tfor <patchwork-incoming@ozlabs.org>;\n\tTue,  5 Sep 2017 04:25:38 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1753958AbdIDSZe (ORCPT <rfc822;patchwork-incoming@ozlabs.org>);\n\tMon, 4 Sep 2017 14:25:34 -0400","from mail-pf0-f169.google.com ([209.85.192.169]:33119 \"EHLO\n\tmail-pf0-f169.google.com\" rhost-flags-OK-OK-OK-OK) by vger.kernel.org\n\twith ESMTP id S1753932AbdIDSZd (ORCPT\n\t<rfc822;netdev@vger.kernel.org>); Mon, 4 Sep 2017 14:25:33 -0400","by mail-pf0-f169.google.com with SMTP id y68so2504040pfd.0\n\tfor <netdev@vger.kernel.org>; Mon, 04 Sep 2017 11:25:33 -0700 (PDT)","from xeon-e3 (76-14-207-240.or.wavecable.com. [76.14.207.240])\n\tby smtp.gmail.com with ESMTPSA id\n\th27sm11740329pgn.18.2017.09.04.11.25.32\n\t(version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256);\n\tMon, 04 Sep 2017 11:25:32 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=networkplumber-org.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=7LhYNsw5Rvga+zUXLD2qZsA4+UeqsCAuRcLou7BtxhU=;\n\tb=uL7uDggvIEL6CMugVzay16BqT50W2IsoWfsY8RSU1+WCKEU/oGWlVubN+OZb/m7JUR\n\t8mkjr9Oyyn9Dis+v8UFjeh6mMX9VeGAkf2ebv+Q7jGbukDRv7oWo1cfAStNySnLNUh2c\n\twzZQihwy8p/Vtl46gxAFTntkP9Es55NCfwNuXZ9x4v09ybzk7mU43kHIP7j0IIz02Dbk\n\t0vAvLF13sZBpvn/+d7NXs0E1SAg0A93GpiBcHLm9qnpWW1iP4U1UZvpjFsEyrFwB+82z\n\tdHql6HoxiK6AdTNzxrJSs90TMcZXfVPypYLSdMq0Kbc3I4dYb/u1WwuBO+HKRjM650qX\n\t/xig==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=7LhYNsw5Rvga+zUXLD2qZsA4+UeqsCAuRcLou7BtxhU=;\n\tb=XBvcmrG5nsc6HOjO53qsp2ZPBPYAEQ7B9aK+RDNhRX+1I6dGkDvaOmQQvo7DrrT3kR\n\tKm8M+J0E1peq3hibAEtoYUJ6RsSufMJeVmVrf1pWsbGKTGX9IhR9qIib/5XfJFKYZWMI\n\t4r8PwUFYZOUJCYeTs9DypiGk4pY+YsaFqZS8Tmidz8/dY8h+AfhG2zwsoLyU7M1hHPlO\n\tJXWrWvtuZ4WNRyk0Md2ySbGMHilyOUjkTyf5Q104mRMS3eJl5SFMZfmYM6FyfM+2rcsn\n\t3DIByPGOaBwOh+903t8qNiPRUp8ZbaI4LkCYxWnZRxPZkYFDMQk7GejLalQYsovXMxI5\n\tzz8A==","X-Gm-Message-State":"AHPjjUjxDlVCg6NYbAGU+kMgwM6ZIUP7suBs2Tv/otlOCaJ26q4eXxQg\n\t+cHI9EyP5Q/rbeQo","X-Google-Smtp-Source":"ADKCNb5H17gcG3RV+0ZUF6Os4vRjqqakwjcyU3SXrOUcwWKNH3iSzrSPfZaP0WohYhiNXX5HhSGBTQ==","X-Received":"by 10.98.138.88 with SMTP id y85mr1232882pfd.224.1504549532963; \n\tMon, 04 Sep 2017 11:25:32 -0700 (PDT)","Date":"Mon, 4 Sep 2017 11:25:25 -0700","From":"Stephen Hemminger <stephen@networkplumber.org>","To":"Phil Sutter <phil@nwl.cc>","Cc":"David Laight <David.Laight@ACULAB.COM>,\n\t\"netdev@vger.kernel.org\" <netdev@vger.kernel.org>","Subject":"Re: [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()","Message-ID":"<20170904112525.04bc66d5@xeon-e3>","In-Reply-To":"<20170904150015.GB30364@orbyte.nwl.cc>","References":"<20170901165256.21459-1-phil@nwl.cc>\n\t<20170901165256.21459-2-phil@nwl.cc>\n\t<063D6719AE5E284EB5DD2968C1650D6DD006D878@AcuExch.aculab.com>\n\t<20170904150015.GB30364@orbyte.nwl.cc>","MIME-Version":"1.0","Content-Type":"text/plain; charset=US-ASCII","Content-Transfer-Encoding":"7bit","Sender":"netdev-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<netdev.vger.kernel.org>","X-Mailing-List":"netdev@vger.kernel.org"}},{"id":1764125,"web_url":"http://patchwork.ozlabs.org/comment/1764125/","msgid":"<063D6719AE5E284EB5DD2968C1650D6DD006EC26@AcuExch.aculab.com>","list_archive_url":null,"date":"2017-09-06T13:59:27","subject":"RE: [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()","submitter":{"id":6689,"url":"http://patchwork.ozlabs.org/api/people/6689/","name":"David Laight","email":"David.Laight@ACULAB.COM"},"content":"From: Stephen Hemminger [mailto:stephen@networkplumber.org]\n> Sent: 04 September 2017 19:25\n> On Mon, 4 Sep 2017 17:00:15 +0200\n> Phil Sutter <phil@nwl.cc> wrote:\n> \n> > On Mon, Sep 04, 2017 at 02:49:20PM +0000, David Laight wrote:\n> > > From: Phil Sutter\n> > > > Sent: 01 September 2017 17:53\n> > > > By making use of strncpy(), both implementations are really simple so\n> > > > there is no need to add libbsd as additional dependency.\n> > > >\n> > > ...\n> > > > +\n> > > > +size_t strlcpy(char *dst, const char *src, size_t size)\n> > > > +{\n> > > > +\tif (size) {\n> > > > +\t\tstrncpy(dst, src, size - 1);\n> > > > +\t\tdst[size - 1] = '\\0';\n> > > > +\t}\n> > > > +\treturn strlen(src);\n> > > > +}\n> > >\n> > > Except that isn't really strlcpy().\n> > > Better would be:\n> > > \tlen = strlen(src) + 1;\n> > > \tif (len <= size)\n> > > \t\tmemcpy(dst, src, len);\n> > > \telse if (size) {\n> > > \t\tdst[size - 1] = 0;\n> > > \t\tmemcpy(dst, src, size - 1);\n> > > \t}\n> > > \treturn len - 1;\n> >\n> > Please elaborate: Why isn't my version \"really\" strlcpy()? Why is your\n> > proposed version better?\n> >\n> > Thanks, Phil\n> \n> Linux kernel:\n> size_t strlcpy(char *dest, const char *src, size_t size)\n> {\n> \tsize_t ret = strlen(src);\n> \n> \tif (size) {\n> \t\tsize_t len = (ret >= size) ? size - 1 : ret;\n> \t\tmemcpy(dest, src, len);\n> \t\tdest[len] = '\\0';\n> \t}\n> \treturn ret;\n> }\n> \n> FreeBSD:\n> size_t\n> strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize)\n> {\n> \tconst char *osrc = src;\n> \tsize_t nleft = dsize;\n> \n> \t/* Copy as many bytes as will fit. */\n> \tif (nleft != 0) {\n> \t\twhile (--nleft != 0) {\n> \t\t\tif ((*dst++ = *src++) == '\\0')\n> \t\t\t\tbreak;\n> \t\t}\n> \t}\n> \n> \t/* Not enough room in dst, add NUL and traverse rest of src. */\n> \tif (nleft == 0) {\n> \t\tif (dsize != 0)\n> \t\t\t*dst = '\\0';\t\t/* NUL-terminate dst */\n> \t\twhile (*src++)\n> \t\t\t;\n> \t}\n> \n> \treturn(src - osrc - 1);\t/* count does not include NUL */\n> }\n> \n> \n> They all give the same results for some basic tests.\n> Test\t\t\tFreeBSD\t\tLinux\t\tIproute2\n> \"\",0:           \t0 \"JUNK\"      \t0 \"JUNK\"      \t0 \"JUNK\"\n> \"\",1:           \t0 \"\"          \t0 \"\"          \t0 \"\"\n> \"\",8:           \t0 \"\"          \t0 \"\"          \t0 \"\"\n> \"foo\",0:        \t3 \"JUNK\"      \t3 \"JUNK\"      \t3 \"JUNK\"\n> \"foo\",3:        \t3 \"fo\"        \t3 \"fo\"        \t3 \"fo\"\n> \"foo\",4:        \t3 \"foo\"       \t3 \"foo\"       \t3 \"foo\"\n> \"foo\",8:        \t3 \"foo\"       \t3 \"foo\"       \t3 \"foo\"\n> \"longstring\",0: \t10 \"JUNK\"     \t10 \"JUNK\"     \t10 \"JUNK\"\n> \"longstring\",8: \t10 \"longstr\"  \t10 \"longstr\"  \t10 \"longstr\"\n\nYou need to look at the contents of the destination buffer after the\nfirst '\\0'.\nstrlcpy() shouldn't change it.\n\n\tDavid","headers":{"Return-Path":"<netdev-owner@vger.kernel.org>","X-Original-To":"patchwork-incoming@ozlabs.org","Delivered-To":"patchwork-incoming@ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=netdev-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xnQF564Vgz9s9Y\n\tfor <patchwork-incoming@ozlabs.org>;\n\tWed,  6 Sep 2017 23:59:37 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S932322AbdIFN7f convert rfc822-to-8bit (ORCPT\n\t<rfc822;patchwork-incoming@ozlabs.org>);\n\tWed, 6 Sep 2017 09:59:35 -0400","from smtp-out4.electric.net ([192.162.216.182]:64689 \"EHLO\n\tsmtp-out4.electric.net\" rhost-flags-OK-OK-OK-OK) by vger.kernel.org\n\twith ESMTP id S932307AbdIFN7c (ORCPT\n\t<rfc822;netdev@vger.kernel.org>); Wed, 6 Sep 2017 09:59:32 -0400","from 1dparY-0006vd-W6 by out4b.electric.net with emc1-ok (Exim\n\t4.87) (envelope-from <David.Laight@ACULAB.COM>)\n\tid 1dparZ-0006yj-V1; Wed, 06 Sep 2017 06:59:29 -0700","by emcmailer; Wed, 06 Sep 2017 06:59:29 -0700","from [156.67.243.126] (helo=AcuExch.aculab.com)\n\tby out4b.electric.net with esmtps (TLSv1:AES128-SHA:128)\n\t(Exim 4.87) (envelope-from <David.Laight@ACULAB.COM>)\n\tid 1dparY-0006vd-W6; Wed, 06 Sep 2017 06:59:28 -0700","from ACUEXCH.Aculab.com ([::1]) by AcuExch.aculab.com ([::1]) with\n\tmapi id 14.03.0123.003; Wed, 6 Sep 2017 14:59:27 +0100"],"From":"David Laight <David.Laight@ACULAB.COM>","To":"'Stephen Hemminger' <stephen@networkplumber.org>,\n\tPhil Sutter <phil@nwl.cc>","CC":"\"netdev@vger.kernel.org\" <netdev@vger.kernel.org>","Subject":"RE: [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()","Thread-Topic":"[iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()","Thread-Index":"AQHTJastEKBHvt3L3kuHqHR1fKMkjKKn5LXA","Date":"Wed, 6 Sep 2017 13:59:27 +0000","Message-ID":"<063D6719AE5E284EB5DD2968C1650D6DD006EC26@AcuExch.aculab.com>","References":"<20170901165256.21459-1-phil@nwl.cc>\n\t<20170901165256.21459-2-phil@nwl.cc>\n\t<063D6719AE5E284EB5DD2968C1650D6DD006D878@AcuExch.aculab.com>\n\t<20170904150015.GB30364@orbyte.nwl.cc>\n\t<20170904112525.04bc66d5@xeon-e3>","In-Reply-To":"<20170904112525.04bc66d5@xeon-e3>","Accept-Language":"en-GB, en-US","Content-Language":"en-US","X-MS-Has-Attach":"","X-MS-TNEF-Correlator":"","x-originating-ip":"[10.202.99.200]","Content-Type":"text/plain; charset=\"Windows-1252\"","Content-Transfer-Encoding":"8BIT","MIME-Version":"1.0","X-Outbound-IP":"156.67.243.126","X-Env-From":"David.Laight@ACULAB.COM","X-Proto":"esmtps","X-Revdns":"","X-HELO":"AcuExch.aculab.com","X-TLS":"TLSv1:AES128-SHA:128","X-Authenticated_ID":"","X-PolicySMART":"3396946, 3397078","X-Virus-Status":["Scanned by VirusSMART (c)","Scanned by VirusSMART (s)"],"Sender":"netdev-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<netdev.vger.kernel.org>","X-Mailing-List":"netdev@vger.kernel.org"}},{"id":1764194,"web_url":"http://patchwork.ozlabs.org/comment/1764194/","msgid":"<20170906082555.516720d4@xeon-e3>","list_archive_url":null,"date":"2017-09-06T15:25:55","subject":"Re: [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()","submitter":{"id":21389,"url":"http://patchwork.ozlabs.org/api/people/21389/","name":"Stephen Hemminger","email":"stephen@networkplumber.org"},"content":"On Wed, 6 Sep 2017 13:59:27 +0000\nDavid Laight <David.Laight@ACULAB.COM> wrote:\n\n> From: Stephen Hemminger [mailto:stephen@networkplumber.org]\n> > Sent: 04 September 2017 19:25\n> > On Mon, 4 Sep 2017 17:00:15 +0200\n> > Phil Sutter <phil@nwl.cc> wrote:\n> >   \n> > > On Mon, Sep 04, 2017 at 02:49:20PM +0000, David Laight wrote:  \n> > > > From: Phil Sutter  \n> > > > > Sent: 01 September 2017 17:53\n> > > > > By making use of strncpy(), both implementations are really simple so\n> > > > > there is no need to add libbsd as additional dependency.\n> > > > >  \n> > > > ...  \n> > > > > +\n> > > > > +size_t strlcpy(char *dst, const char *src, size_t size)\n> > > > > +{\n> > > > > +\tif (size) {\n> > > > > +\t\tstrncpy(dst, src, size - 1);\n> > > > > +\t\tdst[size - 1] = '\\0';\n> > > > > +\t}\n> > > > > +\treturn strlen(src);\n> > > > > +}  \n> > > >\n> > > > Except that isn't really strlcpy().\n> > > > Better would be:\n> > > > \tlen = strlen(src) + 1;\n> > > > \tif (len <= size)\n> > > > \t\tmemcpy(dst, src, len);\n> > > > \telse if (size) {\n> > > > \t\tdst[size - 1] = 0;\n> > > > \t\tmemcpy(dst, src, size - 1);\n> > > > \t}\n> > > > \treturn len - 1;  \n> > >\n> > > Please elaborate: Why isn't my version \"really\" strlcpy()? Why is your\n> > > proposed version better?\n> > >\n> > > Thanks, Phil  \n> > \n> > Linux kernel:\n> > size_t strlcpy(char *dest, const char *src, size_t size)\n> > {\n> > \tsize_t ret = strlen(src);\n> > \n> > \tif (size) {\n> > \t\tsize_t len = (ret >= size) ? size - 1 : ret;\n> > \t\tmemcpy(dest, src, len);\n> > \t\tdest[len] = '\\0';\n> > \t}\n> > \treturn ret;\n> > }\n> > \n> > FreeBSD:\n> > size_t\n> > strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize)\n> > {\n> > \tconst char *osrc = src;\n> > \tsize_t nleft = dsize;\n> > \n> > \t/* Copy as many bytes as will fit. */\n> > \tif (nleft != 0) {\n> > \t\twhile (--nleft != 0) {\n> > \t\t\tif ((*dst++ = *src++) == '\\0')\n> > \t\t\t\tbreak;\n> > \t\t}\n> > \t}\n> > \n> > \t/* Not enough room in dst, add NUL and traverse rest of src. */\n> > \tif (nleft == 0) {\n> > \t\tif (dsize != 0)\n> > \t\t\t*dst = '\\0';\t\t/* NUL-terminate dst */\n> > \t\twhile (*src++)\n> > \t\t\t;\n> > \t}\n> > \n> > \treturn(src - osrc - 1);\t/* count does not include NUL */\n> > }\n> > \n> > \n> > They all give the same results for some basic tests.\n> > Test\t\t\tFreeBSD\t\tLinux\t\tIproute2\n> > \"\",0:           \t0 \"JUNK\"      \t0 \"JUNK\"      \t0 \"JUNK\"\n> > \"\",1:           \t0 \"\"          \t0 \"\"          \t0 \"\"\n> > \"\",8:           \t0 \"\"          \t0 \"\"          \t0 \"\"\n> > \"foo\",0:        \t3 \"JUNK\"      \t3 \"JUNK\"      \t3 \"JUNK\"\n> > \"foo\",3:        \t3 \"fo\"        \t3 \"fo\"        \t3 \"fo\"\n> > \"foo\",4:        \t3 \"foo\"       \t3 \"foo\"       \t3 \"foo\"\n> > \"foo\",8:        \t3 \"foo\"       \t3 \"foo\"       \t3 \"foo\"\n> > \"longstring\",0: \t10 \"JUNK\"     \t10 \"JUNK\"     \t10 \"JUNK\"\n> > \"longstring\",8: \t10 \"longstr\"  \t10 \"longstr\"  \t10 \"longstr\"  \n> \n> You need to look at the contents of the destination buffer after the\n> first '\\0'.\n> strlcpy() shouldn't change it.\n> \n> \tDavid\n\nZeroing the bytes after the first null character should not be a big issue\nother than a few nanoseconds extra work.","headers":{"Return-Path":"<netdev-owner@vger.kernel.org>","X-Original-To":"patchwork-incoming@ozlabs.org","Delivered-To":"patchwork-incoming@ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=netdev-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","ozlabs.org; dkim=pass (2048-bit key;\n\tunprotected) header.d=networkplumber-org.20150623.gappssmtp.com\n\theader.i=@networkplumber-org.20150623.gappssmtp.com\n\theader.b=\"BZWPOaGU\"; dkim-atps=neutral"],"Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xnS8p2KL6z9s03\n\tfor <patchwork-incoming@ozlabs.org>;\n\tThu,  7 Sep 2017 01:26:02 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S932863AbdIFP0A (ORCPT <rfc822;patchwork-incoming@ozlabs.org>);\n\tWed, 6 Sep 2017 11:26:00 -0400","from mail-pf0-f177.google.com ([209.85.192.177]:36184 \"EHLO\n\tmail-pf0-f177.google.com\" rhost-flags-OK-OK-OK-OK) by vger.kernel.org\n\twith ESMTP id S932263AbdIFPZ6 (ORCPT\n\t<rfc822;netdev@vger.kernel.org>); Wed, 6 Sep 2017 11:25:58 -0400","by mail-pf0-f177.google.com with SMTP id e199so13286051pfh.3\n\tfor <netdev@vger.kernel.org>; Wed, 06 Sep 2017 08:25:58 -0700 (PDT)","from xeon-e3 (76-14-207-240.or.wavecable.com. [76.14.207.240])\n\tby smtp.gmail.com with ESMTPSA id\n\ti17sm99425pfi.143.2017.09.06.08.25.57\n\t(version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256);\n\tWed, 06 Sep 2017 08:25:58 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=networkplumber-org.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=bNH33yYLPWBUhv2HILms0BuFHRXuT8WJlahOGlF3wtE=;\n\tb=BZWPOaGUmWthyJIzHUCArjJLHi6vwcQLXlNB6gpuWUY7Zne6iF1JU4WGGSTrwbawmQ\n\tb/YZChbQQwkmZWzL81Egp+AgFvS0yZdHubuTuW3gYoOzhUIa5+XPP+sLLrad21W/e4AQ\n\tOFAmrqK0UJayFP7QtdxqGtFgfhVo9Nzw+gcFHzQ+d92QvTs6UJvjVhdaS3kiuGUuPNT3\n\tEBImdrbRUxcZ2Sh85vrKF/UKJaFySlBPg6RJP08IGcsnDasoE0K7WWbqzqwN/KC4ePDM\n\tRM0QbNeiTBAqWJZSPC5U3rncKRZMPqMJs1PN0YoblljZkSiUq35uVRfQNLeGo82+DR4i\n\tCsvw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=bNH33yYLPWBUhv2HILms0BuFHRXuT8WJlahOGlF3wtE=;\n\tb=KAZ/7oKr0bxx9Pz00NhDI/1feTEBCSgbPC4KcWEokfJ5SDgyaPcKKy2qt7vHiRsoTU\n\tScJy0+M+w+vrEtctgabZl8C1rzX/2+yDdDQMtiX8nsmJx7SJmPkaArvhDMZY6LV/xnNh\n\tIecjaqdhAwqEti1OzUFfwVZkpMJqw5fmBt+Bx6B+7AB20mJObK/vyj7QCQ7KMbiDojMp\n\t8YHyV1mhauujI4xQBbOsQTcVjs3RoDKm7vq2I0c14UbKm/b9z1CXFcyF4b33hjzjExIY\n\t/VAsgZp8kdlvvanufQKLJiis1012fdyIFN/GkPSS4XMLxnAVeii6qnh78+Mx/OfDQUFw\n\t7zKA==","X-Gm-Message-State":"AHPjjUgcvjixArWhXxsMvSWzGCSpXg7a13rWKIinR/qvW8qrR/7cNTdz\n\tq0LCMb70CrqfCnS3","X-Google-Smtp-Source":"ADKCNb6b9IQDK7fpknzSuhAxngQhTZ5o58+/qTboSRzhe+y+yYWrZifzVYO+cIs2lOlqLuY7YhhjqQ==","X-Received":"by 10.84.150.130 with SMTP id h2mr5504020plh.47.1504711558257;\n\tWed, 06 Sep 2017 08:25:58 -0700 (PDT)","Date":"Wed, 6 Sep 2017 08:25:55 -0700","From":"Stephen Hemminger <stephen@networkplumber.org>","To":"David Laight <David.Laight@ACULAB.COM>","Cc":"Phil Sutter <phil@nwl.cc>,\n\t\"netdev@vger.kernel.org\" <netdev@vger.kernel.org>","Subject":"Re: [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()","Message-ID":"<20170906082555.516720d4@xeon-e3>","In-Reply-To":"<063D6719AE5E284EB5DD2968C1650D6DD006EC26@AcuExch.aculab.com>","References":"<20170901165256.21459-1-phil@nwl.cc>\n\t<20170901165256.21459-2-phil@nwl.cc>\n\t<063D6719AE5E284EB5DD2968C1650D6DD006D878@AcuExch.aculab.com>\n\t<20170904150015.GB30364@orbyte.nwl.cc>\n\t<20170904112525.04bc66d5@xeon-e3>\n\t<063D6719AE5E284EB5DD2968C1650D6DD006EC26@AcuExch.aculab.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=US-ASCII","Content-Transfer-Encoding":"7bit","Sender":"netdev-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<netdev.vger.kernel.org>","X-Mailing-List":"netdev@vger.kernel.org"}}]