linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Markus Mayer <mmayer@broadcom.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Al Viro <viro@zeniv.linux.org.uk>,
	Chris Metcalf <cmetcalf@ezchip.com>,
	Kees Cook <keescook@chromium.org>,
	dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org,
	linux-acpi@vger.kernel.org, devel@acpica.org,
	speakup@linux-speakup.org, devel@driverdev.osuosl.org,
	linux-scsi@vger.kernel.org, target-devel@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/6] lib: string: add function strtolower()
Date: Fri, 01 Jul 2016 23:08:35 +0200	[thread overview]
Message-ID: <8737nt83sc.fsf@rasmusvillemoes.dk> (raw)
In-Reply-To: <1467330612-26242-2-git-send-email-mmayer@broadcom.com> (Markus Mayer's message of "Thu, 30 Jun 2016 16:50:07 -0700")

On Fri, Jul 01 2016, Markus Mayer <mmayer@broadcom.com> wrote:

> Add a function called strtolower() to convert strings to lower case
> in-place, overwriting the original string.
>
> This seems to be a recurring requirement in the kernel that is
> currently being solved by several duplicated implementations doing the
> same thing.
>
> Signed-off-by: Markus Mayer <mmayer@broadcom.com>
> ---
>  include/linux/string.h |  1 +
>  lib/string.c           | 14 ++++++++++++++
>  2 files changed, 15 insertions(+)
>
> diff --git a/include/linux/string.h b/include/linux/string.h
> index 26b6f6a..aad605e 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -116,6 +116,7 @@ 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);
> +char *strtolower(char *s);
>  
>  extern void kfree_const(const void *x);
>  
> diff --git a/lib/string.c b/lib/string.c
> index ed83562..6e3b560 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -952,3 +952,17 @@ char *strreplace(char *s, char old, char new)
>  	return s;
>  }
>  EXPORT_SYMBOL(strreplace);
> +
> +char *strtolower(char *s)
> +{
> +	char *p;
> +
> +        if (unlikely(!s))
> +                return NULL;
> +
> +	for (p = s; *p; p++)
> +		*p = tolower(*p);
> +
> +	return s;
> +}
> +EXPORT_SYMBOL(strtolower);

A few suggestions: 

- Make the function take separate src and dst parameters, making it explicitly
  allowed to pass the same value (but not other kinds of overlap, of
  course). That way one can avoid "strcpy(dst, src); strtolower(dst);".

- Drop the NULL check. If someone does "foo->bar = something;
  strtolower(foo->bar); put foo in a global data structure...", the
  dereference of foo->bar may happen much later. Doing the NULL deref
  sooner means it's much easier to find and fix the bug. (Also, other
  str* and mem* functions don't usually check for NULL).

- While it's true that strcpy and memcpy by definition return dst, that's
  mostly useless. If you want it to return anything, please make it
  something that might be used - for example, having stpcpy semantics
  (returning a pointer to dst's terminating \0) means a caller might avoid
  a strlen call.

- Maybe do strtoupper while you're at it. Quick grepping didn't find any
  use for the copy-while-lowercasing, but copy-while-uppercasing can at
  least be used in drivers/acpi/acpica/nsrepair2.c,
  drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c,
  drivers/power/power_supply_sysfs.c along with a bunch of inplace
  uppercasing.


Rasmus

  parent reply	other threads:[~2016-07-01 21:09 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-30 23:50 [PATCH 0/6] lib: string: add function strtolower() Markus Mayer
2016-06-30 23:50 ` [PATCH 1/6] " Markus Mayer
2016-07-01 10:52   ` Jani Nikula
2016-07-01 17:14     ` Markus Mayer
2016-07-01 17:33       ` Jani Nikula
2016-07-01 21:08   ` Rasmus Villemoes [this message]
2016-07-04 20:18     ` Markus Mayer
2016-06-30 23:50 ` [PATCH 2/6] drm/nouveau/core: make use of new strtolower() function Markus Mayer
2016-07-02  1:18   ` [Nouveau] " Alexandre Courbot
2016-07-02 15:21     ` Markus Mayer
2016-07-04  1:37       ` Alexandre Courbot
2016-07-04  3:39         ` Alexandre Courbot
2016-06-30 23:50 ` [PATCH 3/6] ACPICA: " Markus Mayer
2016-07-01  1:11   ` Moore, Robert
     [not found]     ` <CAGt4E5uqbjaubPWE4rq-T7MvqAmwpUhvakM+jv+Sen8est9U5g@mail.gmail.com>
     [not found]       ` <94F2FBAB4432B54E8AACC7DFDE6C92E37E4C381C@ORSMSX110.amr.corp.intel.com>
2016-07-01  4:13         ` Markus Mayer
2016-07-01  4:32           ` Moore, Robert
2016-06-30 23:50 ` [PATCH 4/6] ACPI / device_sysfs: " Markus Mayer
2016-07-01 20:44   ` Rafael J. Wysocki
2016-06-30 23:50 ` [PATCH 5/6] staging: speakup: replace spk_strlwr() with strtolower() Markus Mayer
2016-06-30 23:53   ` Samuel Thibault
2016-06-30 23:50 ` [PATCH 6/6] iscsi-target: replace iscsi_initiatorname_tolower() " Markus Mayer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8737nt83sc.fsf@rasmusvillemoes.dk \
    --to=linux@rasmusvillemoes.dk \
    --cc=akpm@linux-foundation.org \
    --cc=cmetcalf@ezchip.com \
    --cc=devel@acpica.org \
    --cc=devel@driverdev.osuosl.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=keescook@chromium.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=mmayer@broadcom.com \
    --cc=nouveau@lists.freedesktop.org \
    --cc=speakup@linux-speakup.org \
    --cc=target-devel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).