Received: by 10.223.185.116 with SMTP id b49csp1113162wrg; Fri, 23 Feb 2018 12:09:15 -0800 (PST) X-Google-Smtp-Source: AH8x227eXfxYyoJsbIxNK2b+uvudBzdBhNgccM++8l/2PgchyVEuCqJNFiedYseiEV9EAloo9qFH X-Received: by 10.98.159.85 with SMTP id g82mr2843737pfe.15.1519416555179; Fri, 23 Feb 2018 12:09:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519416555; cv=none; d=google.com; s=arc-20160816; b=GMswKqJypGgMECfRcrL0/qu8vbJRvuHpLyjxQCsF775kmx/i5w4mB6edStlhplWM+a xFadERWwXHaDgZ7qU/JsH5HJLQsiHXQSecfZsFswPLVieJLO+AYD9cPvneM109NYOyW3 5NGGBCpoBNbDxgEoj6UMbM640cWbHGHqtYRd+shV+fGPMCzSK2eUlUQcE8xBIux+a6jT VAtAJWOX69qNnoqxMkOFHldmjYfAreYF9WhIpWwhDuXB5vMtjzaLlYmlxqHHg/3I83ST uCYWD59fSaoBYox1O5D7miUVMp2Y6FDglAYtKSiYGjcM0BhOcsttOQWlhEiQqsnXBif1 ckPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=+p/qhO6FyKgUVPO87X2h/RaGo9UVlFtz+WlC2t+g2Ow=; b=b2HGRh847C+6BUbFW+hP4yfDPNxfKzG7ER5rwjHhGHoJDfbWy2J/eTeW1VrKJeFcd3 Reg8/93NyDAmdNOEdTBEKw5crxCI/I1deSN3i0/7vO+ofI0iEDIqV2Q0j50lVZQEojRy tFzmUBWPJLqTf/LsqLc5npJlU68A1MWneFA2AFbXDBmL9qDucLx89kTBc3ledrpm5G5d CCXD0m5BQxGOm0czFjlfVJ46U5uc7NKy+UEZNV8bkPYlxf+mq9KQdmr/IcLbYnFneonS Sc9E0xhV3wIvjKyvH6xyZLk8llqHJyNXXPB250zJhpy92DHcFiyYVPX6BLDFPnt0sdEa B9fQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x3-v6si1309184plo.608.2018.02.23.12.09.00; Fri, 23 Feb 2018 12:09:15 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933197AbeBWUIY (ORCPT + 99 others); Fri, 23 Feb 2018 15:08:24 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:41610 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933054AbeBWSm3 (ORCPT ); Fri, 23 Feb 2018 13:42:29 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 3BEFA1011; Fri, 23 Feb 2018 18:42:28 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Howells , Al Viro Subject: [PATCH 4.9 013/145] Provide a function to create a NUL-terminated string from unterminated data Date: Fri, 23 Feb 2018 19:25:19 +0100 Message-Id: <20180223170726.377426584@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180223170724.669759283@linuxfoundation.org> References: <20180223170724.669759283@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Howells commit f35157417215ec138c920320c746fdb3e04ef1d5 upstream. Provide a function, kmemdup_nul(), that will create a NUL-terminated string from an unterminated character array where the length is known in advance. This is better than kstrndup() in situations where we already know the string length as the strnlen() in kstrndup() is superfluous. Signed-off-by: David Howells Signed-off-by: Al Viro Signed-off-by: Greg Kroah-Hartman --- include/linux/string.h | 1 + mm/util.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) --- a/include/linux/string.h +++ b/include/linux/string.h @@ -123,6 +123,7 @@ extern char *kstrdup(const char *s, gfp_ extern const char *kstrdup_const(const char *s, gfp_t gfp); extern char *kstrndup(const char *s, size_t len, gfp_t gfp); extern void *kmemdup(const void *src, size_t len, gfp_t gfp); +extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp); extern char **argv_split(gfp_t gfp, const char *str, int *argcp); extern void argv_free(char **argv); --- a/mm/util.c +++ b/mm/util.c @@ -80,6 +80,8 @@ EXPORT_SYMBOL(kstrdup_const); * @s: the string to duplicate * @max: read at most @max chars from @s * @gfp: the GFP mask used in the kmalloc() call when allocating memory + * + * Note: Use kmemdup_nul() instead if the size is known exactly. */ char *kstrndup(const char *s, size_t max, gfp_t gfp) { @@ -118,6 +120,28 @@ void *kmemdup(const void *src, size_t le EXPORT_SYMBOL(kmemdup); /** + * kmemdup_nul - Create a NUL-terminated string from unterminated data + * @s: The data to stringify + * @len: The size of the data + * @gfp: the GFP mask used in the kmalloc() call when allocating memory + */ +char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) +{ + char *buf; + + if (!s) + return NULL; + + buf = kmalloc_track_caller(len + 1, gfp); + if (buf) { + memcpy(buf, s, len); + buf[len] = '\0'; + } + return buf; +} +EXPORT_SYMBOL(kmemdup_nul); + +/** * memdup_user - duplicate memory region from user space * * @src: source address in user space