{"id":810730,"url":"http://patchwork.ozlabs.org/api/1.2/patches/810730/?format=json","web_url":"http://patchwork.ozlabs.org/project/netdev/patch/20170906165142.28599-1-phil@nwl.cc/","project":{"id":7,"url":"http://patchwork.ozlabs.org/api/1.2/projects/7/?format=json","name":"Linux network development","link_name":"netdev","list_id":"netdev.vger.kernel.org","list_email":"netdev@vger.kernel.org","web_url":null,"scm_url":null,"webscm_url":null,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20170906165142.28599-1-phil@nwl.cc>","list_archive_url":null,"date":"2017-09-06T16:51:42","name":"[iproute] utils: Review strlcpy() and strlcat()","commit_ref":null,"pull_url":null,"state":"accepted","archived":true,"hash":"2d552411c6b61569ae244fd3fc04b881c1f6664b","submitter":{"id":4285,"url":"http://patchwork.ozlabs.org/api/1.2/people/4285/?format=json","name":"Phil Sutter","email":"phil@nwl.cc"},"delegate":{"id":389,"url":"http://patchwork.ozlabs.org/api/1.2/users/389/?format=json","username":"shemminger","first_name":"stephen","last_name":"hemminger","email":"shemminger@vyatta.com"},"mbox":"http://patchwork.ozlabs.org/project/netdev/patch/20170906165142.28599-1-phil@nwl.cc/mbox/","series":[{"id":1855,"url":"http://patchwork.ozlabs.org/api/1.2/series/1855/?format=json","web_url":"http://patchwork.ozlabs.org/project/netdev/list/?series=1855","date":"2017-09-06T16:51:42","name":"[iproute] utils: Review strlcpy() and strlcat()","version":1,"mbox":"http://patchwork.ozlabs.org/series/1855/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/810730/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/810730/checks/","tags":{},"related":[],"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 3xnV420BPnz9t3Z\n\tfor <patchwork-incoming@ozlabs.org>;\n\tThu,  7 Sep 2017 02:52:02 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S932068AbdIFQv7 (ORCPT <rfc822;patchwork-incoming@ozlabs.org>);\n\tWed, 6 Sep 2017 12:51:59 -0400","from orbyte.nwl.cc ([151.80.46.58]:60566 \"EHLO orbyte.nwl.cc\"\n\trhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP\n\tid S1754214AbdIFQvy (ORCPT <rfc822;netdev@vger.kernel.org>);\n\tWed, 6 Sep 2017 12:51:54 -0400","from localhost ([::1]:55108 helo=xsao)\n\tby orbyte.nwl.cc with esmtp (Exim 4.89)\n\t(envelope-from <phil@nwl.cc>)\n\tid 1dpdYP-00061S-53; Wed, 06 Sep 2017 18:51:53 +0200"],"From":"Phil Sutter <phil@nwl.cc>","To":"Stephen Hemminger <stephen@networkplumber.org>","Cc":"netdev@vger.kernel.org","Subject":"[iproute PATCH] utils: Review strlcpy() and strlcat()","Date":"Wed,  6 Sep 2017 18:51:42 +0200","Message-Id":"<20170906165142.28599-1-phil@nwl.cc>","X-Mailer":"git-send-email 2.13.1","In-Reply-To":"<063D6719AE5E284EB5DD2968C1650D6DD006EC26@AcuExch.aculab.com>","References":"<063D6719AE5E284EB5DD2968C1650D6DD006EC26@AcuExch.aculab.com>","Sender":"netdev-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<netdev.vger.kernel.org>","X-Mailing-List":"netdev@vger.kernel.org"},"content":"As David Laight correctly pointed out, the first version of strlcpy()\nmodified dst buffer behind the string copied into it. Fix this by\nwriting NUL to the byte immediately following src string instead of to\nthe last byte in dst. Doing so also allows to reduce overhead by using\nmemcpy().\n\nImprove strlcat() by avoiding the call to strlcpy() if dst string is\nalready full, not just as sanity check.\n\nSigned-off-by: Phil Sutter <phil@nwl.cc>\n---\n lib/utils.c | 12 ++++++++----\n 1 file changed, 8 insertions(+), 4 deletions(-)","diff":"diff --git a/lib/utils.c b/lib/utils.c\nindex 330ab073c2068..bbd3cbc46a0e5 100644\n--- a/lib/utils.c\n+++ b/lib/utils.c\n@@ -1233,18 +1233,22 @@ int get_real_family(int rtm_type, int rtm_family)\n \n size_t strlcpy(char *dst, const char *src, size_t size)\n {\n+\tsize_t srclen = strlen(src);\n+\n \tif (size) {\n-\t\tstrncpy(dst, src, size - 1);\n-\t\tdst[size - 1] = '\\0';\n+\t\tsize_t minlen = min(srclen, size - 1);\n+\n+\t\tmemcpy(dst, src, minlen);\n+\t\tdst[minlen] = '\\0';\n \t}\n-\treturn strlen(src);\n+\treturn srclen;\n }\n \n size_t strlcat(char *dst, const char *src, size_t size)\n {\n \tsize_t dlen = strlen(dst);\n \n-\tif (dlen > size)\n+\tif (dlen >= size)\n \t\treturn dlen + strlen(src);\n \n \treturn dlen + strlcpy(dst + dlen, src, size - dlen);\n","prefixes":["iproute"]}