From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR02-HE1-obe.outbound.protection.outlook.com (mail-eopbgr10048.outbound.protection.outlook.com [40.107.1.48]) by dpdk.org (Postfix) with ESMTP id D79724C74 for ; Tue, 11 Sep 2018 12:17:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=coriant.onmicrosoft.com; s=selector1-coriant-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SLVxX7lPKre1ZoV2etkHQxL3zRi/s8C/GxDXcMM3jDk=; b=o6fTFKx+e01ZpTBgtBZ9mCdbDxghaOIx9G7ziMvJK2kUMEY4cLUZHPRg7KV4wYYjkTdhQAE2f0HrTSywb12EOXXgMRS7HEUoBB2mcdjZcPUDGKTLR+AJcRF2MEXt78/a8u/Chhlli+eYzO7iEm3+8Zl2X7GGWoSpY1BUowvzBb8= Received: from VI1PR0402MB2925.eurprd04.prod.outlook.com (10.175.24.15) by VI1PR0402MB3581.eurprd04.prod.outlook.com (52.134.4.154) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1122.16; Tue, 11 Sep 2018 10:17:54 +0000 Received: from VI1PR0402MB2925.eurprd04.prod.outlook.com ([fe80::f048:5ab:3a7f:91c4]) by VI1PR0402MB2925.eurprd04.prod.outlook.com ([fe80::f048:5ab:3a7f:91c4%5]) with mapi id 15.20.1122.018; Tue, 11 Sep 2018 10:17:54 +0000 From: "Kuusisaari, Juhamatti (Coriant - FI/Espoo)" To: Gaetan Rivet , "dev@dpdk.org" Thread-Topic: [dpdk-dev] [PATCH v1] eal: add strscpy function Thread-Index: AQHUSbbglHR7U0Tqqk2tqZefBZRbX6Tq3azw Date: Tue, 11 Sep 2018 10:17:53 +0000 Message-ID: References: <20180911100419.19168-1-gaetan.rivet@6wind.com> In-Reply-To: <20180911100419.19168-1-gaetan.rivet@6wind.com> Accept-Language: fi-FI, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=juhamatti.kuusisaari@coriant.com; x-originating-ip: [138.111.134.175] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; VI1PR0402MB3581; 6:1SzeiM1EhtaTHDYgz9bIqJfRBNdq3WAoHABzaEtwqMLrNX6BDORtvZw/BtAtzqryCo/h3HVs5H8zvDeOm4VmqndUAgwChVhS/sBvV2V7FKAQhWfOdoQHAoc8k4p6LZEPvX7uK1cJEga1Dn6QZ48oBZenrW4tAz2z31dAPkINW7R0z+0qHe+2P2f9MGdZPDtL9/kiz1WpCsuddqMvxtwGUCYE8gPTYab54/NKUdao/lNDoMgzJSgcdfX2ZWyVQIc37BvejkzCEdXILw9DdIf+ux4u2NhoAg0X6tGwqobcHlFXWgsuXCismgFr0bggNyW9gEGP3zH+VCsDvUHC42nxh4s1qsgIdMwVsbegUb8tuVv/OcTZM0aJW8eXNug5wegaj4LM0BpqtDuNALWnGYJWxxQqxIsr+4JqG+VFROfI7HEo+LEEJWTNBo1o1K2KJAgHu6nZSViTLe3TSMtjnVFn1A==; 5:ylFc7rNyZtrd5hxk4NMBubTKVdXdbBmc5O6tCNQ3YcON/COqeSVEvizF59LUOIEbbpaXiVOLiOFDsuSgpi6g8/iuYT2Q6rcbbdCuKlWmQcU93cpi0WE4f1uLD3OaeP+7M28GMyMf8yVlviqaXVGoQcONL8Hp+8x//MJ79GnZ7yg=; 7:xbvxR6G3RS46jpJwmah37YktRIgekAFj9LFENVrpb//WrXkc2uoaOA19ayYfmBH8UXItAhWYKWjJYs1NokvQQCJ6NAWNv7VhsbcypWLPMuR0GF5WgGUN3yI+ZQhXQSaU1pePkAA9kSIPTW5uoNdLX9DWqUthMyqWrcknCsf3l1RAJviRtCYTLuQkBAKawJC5RYNfbt1FZOWC7cZT60EAU2SBJVK39rN7PQXy0tRo6TeS1MHPBnoc5CiJtBOeifC5 x-ms-exchange-antispam-srfa-diagnostics: SOS; x-ms-office365-filtering-correlation-id: e3ebec61-3233-4e44-8cc7-08d617cfd65d x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989137)(4534165)(4627221)(201703031133081)(201702281549075)(8990107)(5600074)(711020)(4618075)(2017052603328)(7153060)(7193020); SRVR:VI1PR0402MB3581; x-ms-traffictypediagnostic: VI1PR0402MB3581: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(51653755401839)(84791874153150); x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040522)(2401047)(8121501046)(5005006)(3002001)(93006095)(93001095)(3231311)(944501410)(52105095)(10201501046)(6055026)(149027)(150027)(6041310)(20161123562045)(20161123558120)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(201708071742011)(7699050); SRVR:VI1PR0402MB3581; BCL:0; PCL:0; RULEID:; SRVR:VI1PR0402MB3581; x-forefront-prvs: 0792DBEAD0 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(39860400002)(346002)(376002)(136003)(366004)(396003)(189003)(13464003)(199004)(99286004)(86362001)(25786009)(6436002)(446003)(6116002)(55016002)(6306002)(9686003)(110136005)(81166006)(14454004)(11346002)(81156014)(53546011)(6506007)(229853002)(6246003)(26005)(2906002)(68736007)(7736002)(316002)(305945005)(102836004)(476003)(3846002)(5660300001)(186003)(2900100001)(8676002)(2501003)(106356001)(8936002)(5250100002)(478600001)(105586002)(53936002)(486006)(966005)(66066001)(76176011)(14444005)(256004)(97736004)(74316002)(7696005)(33656002); DIR:OUT; SFP:1101; SCL:1; SRVR:VI1PR0402MB3581; H:VI1PR0402MB2925.eurprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; received-spf: None (protection.outlook.com: coriant.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: T2lA03L7zhujAFr/UVjoonov9VrLs1Ne4lJ+/AnNT1zSv8DL6inoUF2og/a9vFzcBYDGIaG67+Rm4RITmcMJtZt9h5k25gwLAYoSunsczw1WjeuUCp7kKrxXUHRuvtISnkWljAns3OzW/VcAum28j6baGX7ztAyZMHXUayRPBgM+LP8a2QV++ssYi/UT8H8LzqwsDJtpzhMQhuMknMm/X3/C+qVeopCIMXbhUx60cSbxSNxGe123jw6VwS8I3dlYDcBfOOKJIenYOrX2U6BcOsCDW5TTXXk+lU/JNuhi5m8P3g+Fn5KzBFl+tpJyure4CA2mGF7ZF/rDIrOnveVickjiti2CK2dNTpxHSMSH3FE= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: coriant.com X-MS-Exchange-CrossTenant-Network-Message-Id: e3ebec61-3233-4e44-8cc7-08d617cfd65d X-MS-Exchange-CrossTenant-originalarrivaltime: 11 Sep 2018 10:17:54.0690 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 76595477-907e-4695-988b-a6b39087332d X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0402MB3581 Subject: Re: [dpdk-dev] [PATCH v1] eal: add strscpy function 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: , X-List-Received-Date: Tue, 11 Sep 2018 10:17:56 -0000 > -----Original Message----- > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Gaetan Rivet > Sent: Tuesday, September 11, 2018 1:04 PM > To: dev@dpdk.org > Cc: Gaetan Rivet > Subject: [dpdk-dev] [PATCH v1] eal: add strscpy function >=20 > The strncpy function has long been deemed unsafe for use, > in favor of strlcpy or snprintf. >=20 > While snprintf is standard and strlcpy is still largely available, > they both have issues regarding error checking and performance. >=20 > Both will force reading the source buffer past the requested size > if the input is not a proper c-string, and will return the expected > number of bytes copied, meaning that error checking needs to verify > that the number of bytes copied is not superior to the destination > size. >=20 > This contributes to awkward code flow, unclear error checking and > potential issues with malformed input. >=20 > The function strscpy has been discussed for some time already and > has been made available in the linux kernel[1]. >=20 > Propose this new function as a safe alternative. >=20 > [1]: > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit= /?i > d=3D30c44659f4a3e7e1f9f47e895591b4b40bf62671 >=20 > Signed-off-by: Gaetan Rivet > --- >=20 > I agree with the original email, here is a proposed implementation. > I have added the function as part of 18.11 API proper, because this API > is definitely not meant to change. >=20 > This is not meant to be enforced on existing code, or even on new code. > But I think it is better to have it available. >=20 > lib/librte_eal/common/eal_common_string_fns.c | 30 > +++++++++++++++++++ > .../common/include/rte_string_fns.h | 23 ++++++++++++++ > lib/librte_eal/rte_eal_version.map | 7 +++++ > 3 files changed, 60 insertions(+) >=20 > diff --git a/lib/librte_eal/common/eal_common_string_fns.c > b/lib/librte_eal/common/eal_common_string_fns.c > index 6ac5f8289..8a34d2422 100644 > --- a/lib/librte_eal/common/eal_common_string_fns.c > +++ b/lib/librte_eal/common/eal_common_string_fns.c > @@ -38,3 +38,33 @@ rte_strsplit(char *string, int stringlen, > errno =3D EINVAL; > return -1; > } > + > +/* Copy src string into dst. > + * > + * Return negative value and NUL-terminate if dst is too short, > + * Otherwise return number of bytes copied. > + */ > +ssize_t > +strscpy(char *dst, const char *src, size_t dsize) > +{ > + const char *osrc =3D src; > + size_t nleft =3D dsize; > + > + /* Copy as many bytes as will fit. */ > + if (nleft !=3D 0) { > + while (--nleft !=3D 0) { > + if ((*dst++ =3D *src++) =3D=3D '\0') > + break; > + } > + } > + > + /* Not enough room in dst, add NUL and return error. */ > + if (nleft =3D=3D 0) { > + if (dsize !=3D 0) > + *dst =3D '\0'; > + return -E2BIG; > + } > + > + /* count does not include NUL */ > + return (src - osrc - 1); > +} > diff --git a/lib/librte_eal/common/include/rte_string_fns.h > b/lib/librte_eal/common/include/rte_string_fns.h > index 97597a148..46dd919b4 100644 > --- a/lib/librte_eal/common/include/rte_string_fns.h > +++ b/lib/librte_eal/common/include/rte_string_fns.h > @@ -76,6 +76,29 @@ rte_strlcpy(char *dst, const char *src, size_t size) > #endif /* RTE_USE_LIBBSD */ > #endif /* BSDAPP */ >=20 > +/** > + * Copy string src to buffer dst of size dsize. > + * At most dsize-1 chars will be copied. > + * Always NUL-terminates, unless (dsize =3D=3D 0). > + * Returns number of bytes copied (terminating NUL-byte excluded) on > success. > + * Negative errno on error. > + * > + * @param dst > + * The destination string. > + * > + * @param src > + * The input string to be copied. > + * > + * @param dsize > + * Length in bytes of the destination buffer. > + * > + * @return > + * The number of bytes copied on success > + * -E2BIG if the destination buffer is too small. > + */ > +ssize_t > +strscpy(char *dst, const char *src, size_t dsize); > + > #ifdef __cplusplus > } > #endif > diff --git a/lib/librte_eal/rte_eal_version.map > b/lib/librte_eal/rte_eal_version.map > index 344a43d32..fc7b50669 100644 > --- a/lib/librte_eal/rte_eal_version.map > +++ b/lib/librte_eal/rte_eal_version.map > @@ -262,6 +262,13 @@ DPDK_18.08 { >=20 > } DPDK_18.05; >=20 > +DPDK_18.11 { > + global: > + > + strscpy; > + > +} DPDK_18.08; > + > EXPERIMENTAL { > global: Acked-by: Juhamatti Kuusisaari > -- > 2.18.0