From patchwork Wed Jul 13 22:52:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Mayer X-Patchwork-Id: 9228603 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6D1926075D for ; Wed, 13 Jul 2016 22:53:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5FE8C26419 for ; Wed, 13 Jul 2016 22:53:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 54B7527FB6; Wed, 13 Jul 2016 22:53:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CAFB926419 for ; Wed, 13 Jul 2016 22:53:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751515AbcGMWxj (ORCPT ); Wed, 13 Jul 2016 18:53:39 -0400 Received: from mail-pf0-f174.google.com ([209.85.192.174]:33091 "EHLO mail-pf0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751433AbcGMWx1 (ORCPT ); Wed, 13 Jul 2016 18:53:27 -0400 Received: by mail-pf0-f174.google.com with SMTP id i123so22631814pfg.0 for ; Wed, 13 Jul 2016 15:53:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=TlWtg289vOGmTJXads3GAGI9B5eizIWp3X0lwseNq6w=; b=P4BAYWGtbvQ9TmT2mTdbI1sAKsN0EnkJ7dkCxSEqJFKlratT+904ssR5UCxh9FMwFj BaWCqzvEwLBy75O91uSfZqJZzupizjwgxg/4JKu+kzqxWG2Ef2mepoo0J2MiPCqOUCHq ejZQ8liSGUyDpkMM3gX+T+WAKCrRYNfV+L6WA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=TlWtg289vOGmTJXads3GAGI9B5eizIWp3X0lwseNq6w=; b=eu686n5GtZm45aXynMChDucdD3RYfyo6hA2ui0bYF+VA/aB6PwFE/cxDh3oKKs7YpE OEXR6R/zj/NkpPt2gU98kMULEeRmAUA2xXkJzKXADlydO3rfxzHdo6maYKr3lhUvrg48 JBUAajdE8nm5iAZWQN+jyRjTguh35Wnjm52XYigUclTAVpD6jPBwTumYI/GjOdJGxiTe WcUUvZ58txwX6nGazXTsuylBZ1RKCKInHvpd4FjCpz1aOiblI4L2T4W+50S4QtL5C+26 5EWzxhJmOLwXlQz13yf7Vm0SeOrBhHKnQjjAFPUwY8mlbkkCAb/WPlV88NMYw0m0JPJU /JRg== X-Gm-Message-State: ALyK8tJtVEoicMIu1ih4Ph1FQnIzIrZeunfsmWzSmb7BLYSWLvOqJlLvRmV58uM2Pma0DECf X-Received: by 10.98.157.154 with SMTP id a26mr7587496pfk.68.1468450400690; Wed, 13 Jul 2016 15:53:20 -0700 (PDT) Received: from lbrmn-mmayer.ric.broadcom.com ([216.31.219.19]) by smtp.gmail.com with ESMTPSA id h4sm5972561pfk.47.2016.07.13.15.53.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Jul 2016 15:53:20 -0700 (PDT) Received: by lbrmn-mmayer.ric.broadcom.com (Postfix, from userid 1000) id 59E86780648; Wed, 13 Jul 2016 15:52:39 -0700 (PDT) Date: Wed, 13 Jul 2016 15:52:38 -0700 From: Markus Mayer To: Chris Metcalf , Rasmus Villemoes Cc: Andrew Morton , Al Viro , Kees Cook , dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-acpi@vger.kernel.org, speakup@linux-speakup.org, devel@driverdev.osuosl.org, linux-scsi@vger.kernel.org, target-devel@vger.kernel.org, linux-pm@vger.kernel.org, Linux Kernel Subject: Re: [PATCH v3 0/7] lib: string: add functions to case-convert strings Message-ID: <20160713225236.GA30571@lbrmn-mmayer.ric.broadcom.com> References: <1468017794-4818-1-git-send-email-mmayer@broadcom.com> <9b88baed-a067-fcfd-d087-66e36f3060d7@mellanox.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On Sat, Jul 09, 2016 at 09:11:05PM -0700, Markus Mayer wrote: > On 9 July 2016 at 20:13, Chris Metcalf wrote: > > On 7/8/2016 6:43 PM, Markus Mayer wrote: > >> > >> This series introduces a family of generic string case conversion > >> functions. This kind of functionality is needed in several places in > >> the kernel. Right now, everybody seems to be implementing their own > >> copy of this functionality. > >> > >> Based on the discussion of the previous version of this series[1] and > >> the use cases found in the kernel, it does look like having several > >> flavours of case conversion functions is beneficial. The use cases fall > >> into three categories: > >> - copying a string and converting the case while specifying a > >> maximum length to mimic strlcpy() > >> - copying a string and converting the case without specifying a > >> length to mimic strcpy() > >> - converting the case of a string in-place (i.e. modifying the > >> string that was passed in) > >> > >> Consequently, I am proposing these new functions: > >> void strlcpytoupper(char *dst, const char *src, size_t len); > >> void strlcpytolower(char *dst, const char *src, size_t len); > >> void strcpytoupper(char *dst, const char *src); > >> void strcpytolower(char *dst, const char *src); > >> void strtoupper(char *s); > >> void strtolower(char *s); > > > > > > You may want to read the article here: > > > > https://lwn.net/Articles/659214/ > > I'll read that. Thanks. It doesn't look like there is going to be the danger of "mass changes". So far, I have two ACKs (one where the semantics doesn't change, because it's using strtolower()) and the other in a driver in staging. But I understand the concern and will keep an eye out if there are other ACKs. > > and follow up some of the discussion threads on LKML about the best > > semantics to advertise for the strlcpy/strscpy variants. It might > > be helpful to return some kind of overflow/truncation error from > > your copy functions so people can error-check the result. > > I am inclined to agree. However, everybody has been telling me that > these functions should be void. Originally they weren't. What about something like this? It might also work to keep the four static inline functions as "void" (since they won't ever return E2BIG anyway) and just have strlcpyto* return an integer (since that's where the buffer could be too small). Rasmus, what's your take? --- To unsubscribe from this list: send the line "unsubscribe linux-pm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/include/linux/string.h b/include/linux/string.h index ae82d13..6cc85dc 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -116,8 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t); #endif void *memchr_inv(const void *s, int c, size_t n); char *strreplace(char *s, char old, char new); -extern void strlcpytoupper(char *dst, const char *src, size_t len); -extern void strlcpytolower(char *dst, const char *src, size_t len); +extern int strlcpytoupper(char *dst, const char *src, size_t len); +extern int strlcpytolower(char *dst, const char *src, size_t len); extern void kfree_const(const void *x); @@ -175,38 +175,46 @@ static inline const char *kbasename(const char *path) * strcpytoupper - Copy string and convert to uppercase. * @dst: The buffer to store the result. * @src: The string to convert to uppercase. + * + * Returns the number of characters copied. */ -static inline void strcpytoupper(char *dst, const char *src) +static inline int strcpytoupper(char *dst, const char *src) { - strlcpytoupper(dst, src, ~(size_t)0); + return strlcpytoupper(dst, src, ~(size_t)0); } /** * strcpytolower - Copy string and convert to lowercase. * @dst: The buffer to store the result. * @src: The string to convert to lowercase. + * + * Returns the number of characters copied. */ -static inline void strcpytolower(char *dst, const char *src) +static inline int strcpytolower(char *dst, const char *src) { - strlcpytolower(dst, src, ~(size_t)0); + return strlcpytolower(dst, src, ~(size_t)0); } /** * strtoupper - Convert string to uppercase. * @s: The string to operate on. + * + * Returns the number of characters copied. */ -static inline void strtoupper(char *s) +static inline int strtoupper(char *s) { - strlcpytoupper(s, s, ~(size_t)0); + return strlcpytoupper(s, s, ~(size_t)0); } /** * strtolower - Convert string to lowercase. * @s: The string to operate on. + * + * Returns the number of characters copied. */ -static inline void strtolower(char *s) +static inline int strtolower(char *s) { - strlcpytolower(s, s, ~(size_t)0); + return strlcpytolower(s, s, ~(size_t)0); } #endif /* _LINUX_STRING_H_ */ diff --git a/lib/string.c b/lib/string.c index 7903e10..d36d5fb2 100644 --- a/lib/string.c +++ b/lib/string.c @@ -958,17 +958,21 @@ EXPORT_SYMBOL(strreplace); * @dst: The buffer to store the result. * @src: The string to convert to uppercase. * @len: Maximum string length. May be SIZE_MAX to set no limit. + * + * Returns the number of characters copied or -E2BIG if @dst wasn't big enough. */ -void strlcpytoupper(char *dst, const char *src, size_t len) +int strlcpytoupper(char *dst, const char *src, size_t len) { size_t i; if (!len) - return; + return -E2BIG; for (i = 0; i < len && src[i]; ++i) dst[i] = toupper(src[i]); dst[i < len ? i : i - 1] = '\0'; + + return (i < len) ? i : -E2BIG; } EXPORT_SYMBOL(strlcpytoupper); @@ -977,16 +981,20 @@ EXPORT_SYMBOL(strlcpytoupper); * @dst: The buffer to store the result. * @src: The string to convert to lowercase. * @len: Maximum string length. May be SIZE_MAX to set no limit. + * + * Returns the number of characters copied or -E2BIG if @dst wasn't big enough. */ -void strlcpytolower(char *dst, const char *src, size_t len) +int strlcpytolower(char *dst, const char *src, size_t len) { size_t i; if (!len) - return; + return -E2BIG; for (i = 0; i < len && src[i]; ++i) dst[i] = tolower(src[i]); dst[i < len ? i : i - 1] = '\0'; + + return (i < len) ? i : -E2BIG; } EXPORT_SYMBOL(strlcpytolower);