From nobody Tue Apr 7 14:55:35 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53E16ECAAD5 for ; Sat, 27 Aug 2022 02:08:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345304AbiH0CIZ (ORCPT ); Fri, 26 Aug 2022 22:08:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232135AbiH0CIO (ORCPT ); Fri, 26 Aug 2022 22:08:14 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20925C0B64 for ; Fri, 26 Aug 2022 19:08:13 -0700 (PDT) Received: from localhost.localdomain (unknown [80.240.223.29]) by mail.ispras.ru (Postfix) with ESMTPSA id 6BDAE407625D; Sat, 27 Aug 2022 02:08:11 +0000 (UTC) From: Evgeniy Baskov To: Borislav Petkov Cc: Evgeniy Baskov , Dave Hansen , Ingo Molnar , Thomas Gleixner , linux-kernel@vger.kernel.org, x86@kernel.org, Alexey Khoroshilov Subject: [PATCH v6 1/5] x86/boot: Add strlcat() and strscpy() to compressed kernel Date: Sat, 27 Aug 2022 05:08:06 +0300 Message-Id: <147105a124819e88032387d2979fa57e01b2b7d2.1661565218.git.baskov@ispras.ru> X-Mailer: git-send-email 2.37.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" These functions simplify the code of command line concatenation helper and reduce the probability of mistakes. Use simpler implementation of strscpy than used in it kernel itself to avoid code bloat in compressed kernel. Signed-off-by: Evgeniy Baskov --- arch/x86/boot/compressed/string.c | 50 +++++++++++++++++++++++++++++++ arch/x86/purgatory/purgatory.c | 1 + 2 files changed, 51 insertions(+) diff --git a/arch/x86/boot/compressed/string.c b/arch/x86/boot/compressed/s= tring.c index 81fc1eaa3229..5c193fa0a09b 100644 --- a/arch/x86/boot/compressed/string.c +++ b/arch/x86/boot/compressed/string.c @@ -40,6 +40,56 @@ static void *____memcpy(void *dest, const void *src, siz= e_t n) } #endif =20 +size_t strlcat(char *dest, const char *src, size_t count) +{ + size_t dsize =3D strlen(dest); + size_t len =3D strlen(src); + size_t res =3D dsize + len; + + /* This would be a bug */ + if (dsize >=3D count) + error("strlcat(): destination too big\n"); + + dest +=3D dsize; + count -=3D dsize; + if (len >=3D count) + len =3D count-1; + memcpy(dest, src, len); + dest[len] =3D 0; + return res; +} + +/* Don't include word-at-a-time code path in compressed kernel for simplic= ity */ +size_t strscpy(char *dest, const char *src, size_t count) +{ + long res =3D 0; + + if (count =3D=3D 0) + return -E2BIG; + + if (count > INT_MAX) { + warn("strscpy(): Count is too big"); + return -E2BIG; + } + + while (count) { + char c; + + c =3D src[res]; + dest[res] =3D c; + if (!c) + return res; + res++; + count--; + } + + /* Hit buffer length without finding a NUL; force NUL-termination. */ + if (res) + dest[res-1] =3D '\0'; + + return -E2BIG; +} + void *memset(void *s, int c, size_t n) { int i; diff --git a/arch/x86/purgatory/purgatory.c b/arch/x86/purgatory/purgatory.c index 7558139920f8..65f0cedb65ae 100644 --- a/arch/x86/purgatory/purgatory.c +++ b/arch/x86/purgatory/purgatory.c @@ -57,3 +57,4 @@ void purgatory(void) * arch/x86/boot/compressed/string.c */ void warn(const char *msg) {} +void error(char *m) {} --=20 2.37.2