Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759001AbbGHUyQ (ORCPT ); Wed, 8 Jul 2015 16:54:16 -0400 Received: from mail-oi0-f51.google.com ([209.85.218.51]:35282 "EHLO mail-oi0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755529AbbGHUyB (ORCPT ); Wed, 8 Jul 2015 16:54:01 -0400 MIME-Version: 1.0 In-Reply-To: <1436386845-11989-3-git-send-email-cmetcalf@ezchip.com> References: <1436386845-11989-1-git-send-email-cmetcalf@ezchip.com> <1436386845-11989-3-git-send-email-cmetcalf@ezchip.com> Date: Wed, 8 Jul 2015 22:54:00 +0200 X-Google-Sender-Auth: ad9fw43GBGBUHJdQPT0xdu7cHFw Message-ID: Subject: Re: [PATCH v2 2/3] string: provide strscpy() From: Geert Uytterhoeven To: Chris Metcalf Cc: Linus Torvalds , Al Viro , Fabian Frederick , Randy Dunlap , Rickard Strandqvist , Greg KH , Peter Zijlstra , "David S. Miller" , Frederic Weisbecker , Andrew Morton , Sam Ravnborg , Stephen Rothwell , "Theodore Ts'o" , Grant Likely , Dan Carpenter , Michael Ellerman , Linux Kernel Mailing List , Linux-Arch Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2321 Lines: 57 Hi Chris, On Wed, Jul 8, 2015 at 10:20 PM, Chris Metcalf wrote: > + * strscpy - Copy a C-string into a sized buffer > + * @dest: Where to copy the string to > + * @src: Where to copy the string from > + * @count: Size of destination buffer > + * > + * Copy the string, or as much of it as fits, into the dest buffer. > + * The routine returns the number of characters copied (not including > + * the trailing NUL) or -E2BIG if the destination buffer wasn't big enough. > + * The behavior is undefined if the string buffers overlap. > + * If the destination buffer isn't big enough, it is NUL terminated. ... unless count is zero and it's also NUL terminated if it is big enough ;-) Perhaps "The destination buffer is always NUL terminated, unless it's zero-sized." > + * > + * Preferred to strlcpy() since the API doesn't require reading memory > + * from the src string beyond the specified "count" bytes, and since > + * the return value is easier to error-check than strlcpy()'s. > + * In addition, the implementation is robust to the string changing out > + * from underneath it, unlike the current strlcpy() implementation. > + * > + * Preferred to strncpy() since it always returns a valid string, and > + * doesn't unnecessarily force the tail of the destination buffer to be > + * zeroed. If the zeroing is desired, it's likely cleaner to use strscpy() > + * with an overflow test, then just memset() the tail of the dest buffer. > + */ > +ssize_t strscpy(char *dest, const char *src, size_t count) > +{ > + const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; > + size_t max = count; > + long res = 0; > + > + if (count == 0) > + return -E2BIG; Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/