Received: by 10.223.185.116 with SMTP id b49csp1140632wrg; Fri, 23 Feb 2018 12:39:26 -0800 (PST) X-Google-Smtp-Source: AH8x225QUUmaR2iGb/G9xHxRc08xwEhzdgJKO5Vg12gifPXxtfhxlebCBoR+rZT6z63llbSGMtqv X-Received: by 10.98.152.205 with SMTP id d74mr2949278pfk.115.1519418366356; Fri, 23 Feb 2018 12:39:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519418366; cv=none; d=google.com; s=arc-20160816; b=eb0bELE9VB+V3rwrV/0yAm6ly9KK86q+ii5qEF4ABiDbPeH1bWn8NcdW4Z2IgkcE6G jlqczIq+NlrfmDQOiA4Kb+wdSHhpF13xsb4rPGbR05l/ZccuvktQhsMbpUJo2Chc/4wM CYfxyGGRkS07gHsUJYsWpPaDYqyt9T2hAkXQM2EZvtPzK5ocU7goncroENpcUHA22Zmg j8whJ+BJAvS2/0tGU4VoQxcJtfXW8v0DwgPkwKwGJcdtVankk9OzP/e2YJhXHuhm7AKM RakVDj07cw0Zvo+za6a3DqqXy7rMtRJodTMrctl7sCvJWoQkGg0ui9TZxMQkaZzQB/wD JN3A== 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=/3EJjkdSfzZ20XZy2GVYRT9SLMmAoJfjeVSxHkMsJds=; b=Lnol7//c6fKBvoZbFsualmivyeBp4Fm2SoBu2ghiaw4m/LKhRZ4fnVvyF/4kWA6F2L It/bvAQIfluE+q/Xn5Xe0VjH6a6Y8ZQ+ADQ+mKgWCwSpJ3fEz8AIyx7qb1jkBSC5J/oj ejSz/WcXsKnzM84NQJE+02Y6+rpjI2jZz/QA1TojAfoxIWjhUO48yHQ/S5k1UpUAACA+ cRi9eTqzP4o9KFvXMGX2i2zzSpNGBOZQYxUWUH8Igb6hpgR1fXc2I762CzR7kj+bLSTB A3kymfKqidcHKvbT2TX6Kx9F6yJn5Wru9NPrrlPe4V+DMbTqgFI1nGs7ScnkDVoQIA4S 1H3A== 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 r6si2311867pfl.304.2018.02.23.12.39.11; Fri, 23 Feb 2018 12:39:26 -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 S1753542AbeBWSe1 (ORCPT + 99 others); Fri, 23 Feb 2018 13:34:27 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:36882 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752492AbeBWSeX (ORCPT ); Fri, 23 Feb 2018 13:34:23 -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 7E063124C; Fri, 23 Feb 2018 18:34:22 +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.4 009/193] Provide a function to create a NUL-terminated string from unterminated data Date: Fri, 23 Feb 2018 19:24:02 +0100 Message-Id: <20180223170327.540076280@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180223170325.997716448@linuxfoundation.org> References: <20180223170325.997716448@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.4-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 @@ -122,6 +122,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