public inbox for linux-kernel@vger.kernel.org
 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, 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@vger.kernel.org
Subject: Re: [PATCH v2 1/7] lib: string: add functions to case-convert strings
Date: Fri, 08 Jul 2016 02:19:13 +0200	[thread overview]
Message-ID: <87oa692d8e.fsf@rasmusvillemoes.dk> (raw)
In-Reply-To: <1467751631-22878-2-git-send-email-mmayer@broadcom.com> (Markus Mayer's message of "Tue, 5 Jul 2016 13:47:05 -0700")

On Tue, Jul 05 2016, Markus Mayer <mmayer@broadcom.com> wrote:

> Add a collection of generic functions to convert strings to lowercase
> or uppercase.
>
> Changing the case of a string (with or without copying it first) seems
> to be a recurring requirement in the kernel that is currently being
> solved by several duplicated implementations doing the same thing. This
> change aims at reducing this code duplication.
>
> +/**
> + * strncpytoupper - Copy a length-limited string and convert to uppercase.
> + * @dst: The buffer to store the result.
> + * @src: The string to convert to uppercase.
> + * @len: Maximum string length. May be 0 to set no limit.
> + *
> + * Returns pointer to terminating '\0' in @dst.
> + */
> +char *strncpytoupper(char *dst, const char *src, size_t len)
> +{
> +	size_t i;
> +
> +	for (i = 0; src[i] != '\0' && (i < len || !len); i++)
> +		dst[i] = toupper(src[i]);
> +	if (i < len || !len)
> +		dst[i] = '\0';
> +
> +	return dst + i;
> +}

Hm, this seems to copy the insane semantics from strncpy of not
guaranteeing '\0'-termination.

Why use 0 as a sentinel, when (size_t)-1 == SIZE_MAX would work just as
well and require a little less code (no || !len)?

I regret suggesting this return semantics and now agree that void would
be better, especially since there doesn't seem to be anyone who can
use this (or any other) return value. How about

if (!len)
   return;

for (i = 0; i < len && src[i]; ++i)
  dst[i] = toupper(src[i]);
dst[i < len ? i : i-1] = '\0';

(I think you must do i < len before testing src[i], since the len
parameter should be an upper bound on the number of bytes to access in
both src and dst).

Rasmus

  parent reply	other threads:[~2016-07-08  0:20 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-05 20:47 [PATCH v2 0/7] lib: string: add functions to case-convert strings Markus Mayer
2016-07-05 20:47 ` [PATCH v2 1/7] " Markus Mayer
2016-07-07 11:04   ` Eric Engestrom
2016-07-08  0:19   ` Rasmus Villemoes [this message]
2016-07-08 18:04     ` Markus Mayer
2016-07-05 20:47 ` [PATCH v2 2/7] drm/nouveau/core: make use of new strncpytolower() function Markus Mayer
2016-07-05 20:47 ` [PATCH v2 3/7] ACPI / device_sysfs: make use of new strtolower() function Markus Mayer
2016-07-05 20:47 ` [PATCH v2 4/7] staging: speakup: replace spk_strlwr() with strncpytolower() Markus Mayer
2016-07-05 20:47 ` [PATCH v2 5/7] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower() Markus Mayer
2016-07-05 20:47 ` [PATCH v2 6/7] drm/nouveau/fifo/gk104: make use of new strcpytoupper() function Markus Mayer
2016-07-05 20:47 ` [PATCH v2 7/7] power_supply: " Markus Mayer
2016-07-05 22:14 ` [PATCH v2 0/7] lib: string: add functions to case-convert strings Joe Perches
2016-07-05 22:36   ` Markus Mayer
2016-07-05 22:56     ` Joe Perches
2016-07-06  4:32       ` Markus Mayer
2016-07-07  5:05       ` [Nouveau] " Alexandre Courbot

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=87oa692d8e.fsf@rasmusvillemoes.dk \
    --to=linux@rasmusvillemoes.dk \
    --cc=akpm@linux-foundation.org \
    --cc=cmetcalf@ezchip.com \
    --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-pm@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