Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1385770pxa; Sat, 15 Aug 2020 18:45:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwlEDsjNW3WuU8orm6HFhh8Xxgd/SCcAMieIZHe4fwkGOTJ3YCy5hPo5JqFsIn11kMySyNN X-Received: by 2002:a17:906:e289:: with SMTP id gg9mr9469851ejb.448.1597542345660; Sat, 15 Aug 2020 18:45:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597542345; cv=none; d=google.com; s=arc-20160816; b=h1YWZPVxOu++MR90hzEuK+lwnGT603iCipXvBuf0fLGYcVJxPE81EALPIOj/GYfwZx VIA7Z2sunw3RepMsK7WQDDOXcP8QBHrLCZERxB/mZAeTO4FSfFHA7h800Rkvz50IcasL EiJ0CSMFx6aCF/XQULM/zfWZTmC7ucwfC8x9IWsQNhJZYGIUTMBoOuZ7k0M92I2YN/NU iln9tNB2lqk/8XmtQEsPcpIBodGYoixXzWHGsfw8Qehgbp0MxbJIowVkhvBN1cTD+J/z lN7KkWBc9UEMCuWW20l3xlQDtzZFwgLRrjyxHysgPqYinAfzHqaFpajibN7SIOFsTUbE +9Zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=54FL2fi4838KNUxBDYGwUIbd4eC5LfouQots9gMJMF0=; b=A3R9EW8BsdvsMwXsOdFcDY5IjoEtrQzZywMZkWJ+hsl/G9QJiw1GD9SLyPSsleh9lg 2CnU0+pRbw4ldwqWnDar011npHJIRYOsyOdYQvVBfwyPOVbVvpqIU7s74zLUjdBQZwm7 4BJcq0ezPAtEqFzxbwFTUrI0CY2kCJrXdTZrELW+3c9t3ktHWK0OI5cT4gw+k7+qTq12 RSuiImAfpbaIbTflX+QTvgLl62PXmN11X6iu9RLQapHPJDr49/daTqAsrpMDxQeH1fgY G85+/CMv0nuNE+4pdXJBn8qK7ZE2wayjQgSLBDVSuKdiQM6EbiEGmXRuzwhxEo8cxVos H3Kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=fPvq6U6A; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rp10si8418635ejb.323.2020.08.15.18.44.48; Sat, 15 Aug 2020 18:45:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=fPvq6U6A; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729322AbgHOVzJ (ORCPT + 99 others); Sat, 15 Aug 2020 17:55:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729080AbgHOVwZ (ORCPT ); Sat, 15 Aug 2020 17:52:25 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B8F0C06135A for ; Fri, 14 Aug 2020 19:09:54 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id b18so6813949pge.8 for ; Fri, 14 Aug 2020 19:09:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=54FL2fi4838KNUxBDYGwUIbd4eC5LfouQots9gMJMF0=; b=fPvq6U6A360rPMQQfY80tv926Cd5O3nR4WPkH6JUn3SS4hp8J9vEeuVc8QeEpFMBTo xXoUqxM6lxDduHfN8MJCfliJLBMFyqGypCTtDhu57I8yZD1gzmRxGPsFsc6DfyxonpwQ 6kCaIoeHLd7JFEftxyEAgBxGUAAjqVFsKV4936IzdButxXfd4XyOFm//dLBHTd/jc5fb LYh0OmapnhdXPgays9Ex8Eqi2F/ZdVQTvlJLIGBgwpscpX8iuzHC6U3vBvl1MbMN/7fb N8pn4/28tirBW60VwYTiFtv+PPo9JsuEa8A16klsOl/e5uOnISACuAIgTXSZbfn2Z4Um Omjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=54FL2fi4838KNUxBDYGwUIbd4eC5LfouQots9gMJMF0=; b=Jm4iSZ4Dn5jWLYGzPjUrKyG7n9XgxzoLXbmSChZPTC//8H0U3ndopbh12X7J38lBX1 nvouylL/m8S+D8uDhOpd9y6jzqup86/zrGbUQ9mkfjPfckxUPmlp9I0WNKSSMPTiQYMh k3yRBrBJCM8ulRa8N6FOlvgh1fWyvBC09xgijNVKZ2iSk14Te552K06AORhjId9i2/e+ tioaFrAjAX9iLe5Hh7jO4cJfOAPm5zziVrs7/0COJMvlzeY61OzMUiiGToJd6LYmTkym GeVeGIfxGD2LVf6fmsNnLtjtoRLvvmdBLnLJ6b2ddsYEBcyDM4xxZZ5GoF4JNZafN4gB zUDA== X-Gm-Message-State: AOAM53293RAe5KSWaNINUmrlkMumvtdZi3OaA87u4erqOwcjr1EBCLeT sEdN0AGTuYd5WqlZzxUPVSvD646U7gKo31eQ4cU= X-Received: by 2002:a63:df54:: with SMTP id h20mr3416349pgj.319.1597457393328; Fri, 14 Aug 2020 19:09:53 -0700 (PDT) Date: Fri, 14 Aug 2020 19:09:44 -0700 In-Reply-To: <20200815014006.GB99152@rani.riverdale.lan> Message-Id: <20200815020946.1538085-1-ndesaulniers@google.com> Mime-Version: 1.0 References: <20200815014006.GB99152@rani.riverdale.lan> X-Mailer: git-send-email 2.28.0.220.ged08abb693-goog Subject: [PATCH v2] lib/string.c: implement stpcpy From: Nick Desaulniers To: Andrew Morton Cc: "=?UTF-8?q?D=C3=A1vid=20Bolvansk=C3=BD?=" , Eli Friedman , Nick Desaulniers , stable@vger.kernel.org, Arvind Sankar , Joe Perches , Sami Tolvanen , Vishal Verma , Dan Williams , Andy Shevchenko , "Joel Fernandes (Google)" , Daniel Axtens , Kees Cook , Ingo Molnar , Yury Norov , Alexandru Ardelean , linux-kernel@vger.kernel.org, clang-built-linux@googlegroups.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org LLVM implemented a recent "libcall optimization" that lowers calls to `sprintf(dest, "%s", str)` where the return value is used to `stpcpy(dest, str) - dest`. This generally avoids the machinery involved in parsing format strings. Calling `sprintf` with overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior. `stpcpy` is just like `strcpy` except it returns the pointer to the new tail of `dest`. This allows you to chain multiple calls to `stpcpy` in one statement. `stpcpy` was first standardized in POSIX.1-2008. Implement this so that we don't observe linkage failures due to missing symbol definitions for `stpcpy`. Similar to last year's fire drill with: commit 5f074f3e192f ("lib/string.c: implement a basic bcmp") This optimization was introduced into clang-12. Cc: stable@vger.kernel.org Link: https://bugs.llvm.org/show_bug.cgi?id=47162 Link: https://github.com/ClangBuiltLinux/linux/issues/1126 Link: https://man7.org/linux/man-pages/man3/stpcpy.3.html Link: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stpcpy.html Link: https://reviews.llvm.org/D85963 Suggested-by: Arvind Sankar Suggested-by: Joe Perches Reported-by: Sami Tolvanen Tested-by: Sami Tolvanen Signed-off-by: Nick Desaulniers --- Changes V2: * Added Sami's Tested by; though the patch changed implementation, the missing symbol at link time was the problem Sami was observing. * Fix __restrict -> __restrict__ typo as per Joe. * Drop note about restrict from commit message as per Arvind. * Fix NULL -> NUL as per Arvind; NUL is ASCII '\0'. TIL * Fix off by one error as per Arvind; I had another off by one error in my test program that was masking this. include/linux/string.h | 3 +++ lib/string.c | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index b1f3894a0a3e..7686dbca8582 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -31,6 +31,9 @@ size_t strlcpy(char *, const char *, size_t); #ifndef __HAVE_ARCH_STRSCPY ssize_t strscpy(char *, const char *, size_t); #endif +#ifndef __HAVE_ARCH_STPCPY +extern char *stpcpy(char *__restrict__, const char *__restrict__); +#endif /* Wraps calls to strscpy()/memset(), no arch specific code required */ ssize_t strscpy_pad(char *dest, const char *src, size_t count); diff --git a/lib/string.c b/lib/string.c index 6012c385fb31..68ddbffbbd58 100644 --- a/lib/string.c +++ b/lib/string.c @@ -272,6 +272,29 @@ ssize_t strscpy_pad(char *dest, const char *src, size_t count) } EXPORT_SYMBOL(strscpy_pad); +#ifndef __HAVE_ARCH_STPCPY +/** + * stpcpy - copy a string from src to dest returning a pointer to the new end + * of dest, including src's NUL terminator. May overrun dest. + * @dest: pointer to end of string being copied into. Must be large enough + * to receive copy. + * @src: pointer to the beginning of string being copied from. Must not overlap + * dest. + * + * stpcpy differs from strcpy in two key ways: + * 1. inputs must not overlap. + * 2. return value is the new NULL terminated character. (for strcpy, the + * return value is a pointer to src. + */ +#undef stpcpy +char *stpcpy(char *__restrict__ dest, const char *__restrict__ src) +{ + while ((*dest++ = *src++) != '\0') + /* nothing */; + return --dest; +} +#endif + #ifndef __HAVE_ARCH_STRCAT /** * strcat - Append one %NUL-terminated string to another -- 2.28.0.220.ged08abb693-goog