From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932289AbcGHAUG (ORCPT ); Thu, 7 Jul 2016 20:20:06 -0400 Received: from mail-wm0-f52.google.com ([74.125.82.52]:36643 "EHLO mail-wm0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753715AbcGHATR (ORCPT ); Thu, 7 Jul 2016 20:19:17 -0400 From: Rasmus Villemoes To: Markus Mayer Cc: Andrew Morton , Al Viro , Chris Metcalf , 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@vger.kernel.org Subject: Re: [PATCH v2 1/7] lib: string: add functions to case-convert strings Organization: D03 References: <1467751631-22878-1-git-send-email-mmayer@broadcom.com> <1467751631-22878-2-git-send-email-mmayer@broadcom.com> X-Hashcash: 1:20:160708:target-devel@vger.kernel.org::tDRyMyvDdC49Tli4:00000000000000000000000000000000006mv X-Hashcash: 1:20:160708:viro@zeniv.linux.org.uk::e9vKwb4UPFGx90Aq:000000000000000000000000000000000000000J8H X-Hashcash: 1:20:160708:akpm@linux-foundation.org::rt5drd+EsB+PWDJa:00000000000000000000000000000000000002/F X-Hashcash: 1:20:160708:mmayer@broadcom.com::64oAPiMTKzfgIhrn:0000000000000000000000000000000000000000000uQd X-Hashcash: 1:20:160708:linux-kernel@vger.kernel.org::lS7tQlSHV8t8/Sgp:0000000000000000000000000000000000laW X-Hashcash: 1:20:160708:dri-devel@lists.freedesktop.org::nbr0drbIe6oEFjh+:0000000000000000000000000000001ASG X-Hashcash: 1:20:160708:cmetcalf@ezchip.com::m1IAN9zFN5nEg9CS:0000000000000000000000000000000000000000001m3E X-Hashcash: 1:20:160708:linux-acpi@vger.kernel.org::qEj3llvEDpdLeXx9:000000000000000000000000000000000001Xz2 X-Hashcash: 1:20:160708:keescook@chromium.org::WcWKigQ3StioTBxS:00000000000000000000000000000000000000001xez X-Hashcash: 1:20:160708:nouveau@lists.freedesktop.org::xLkBm2JCHmT8OKM8:000000000000000000000000000000002hKO X-Hashcash: 1:20:160708:linux-pm@vger.kernel.org::KPmD8Yo8maZpkLWK:00000000000000000000000000000000000002RpW X-Hashcash: 1:20:160708:linux-scsi@vger.kernel.org::zxIApkhtaTSbbksO:000000000000000000000000000000000003hZF X-Hashcash: 1:20:160708:speakup@linux-speakup.org::eDeNHeDgBRo/XTCa:0000000000000000000000000000000000003uVJ X-Hashcash: 1:20:160708:devel@driverdev.osuosl.org::x7QGb/bYDM0DPWB0:000000000000000000000000000000000005PGI Date: Fri, 08 Jul 2016 02:19:13 +0200 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") Message-ID: <87oa692d8e.fsf@rasmusvillemoes.dk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jul 05 2016, Markus Mayer 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