From patchwork Sun Feb 18 19:51:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodrigo Campos X-Patchwork-Id: 202875 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp928929dyc; Sun, 18 Feb 2024 11:52:18 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVH9Ia3L2KC81VlQJrcdOKJiiPW0BLP6ydB8j63HTzcuecc+Mfr4c2wgLuI2tTE3eraLekxq9/f7NOZQbc505KW2wdAvA== X-Google-Smtp-Source: AGHT+IHV6hTl644GqgD7teovueixj0+iiJLP0GYtZV0XtKjpNrlAF50/Q2g37d7q6fHuq+l8o3B8 X-Received: by 2002:a05:6358:d18:b0:176:40fb:e123 with SMTP id v24-20020a0563580d1800b0017640fbe123mr11976650rwj.5.1708285938483; Sun, 18 Feb 2024 11:52:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708285938; cv=pass; d=google.com; s=arc-20160816; b=hCO4zT+6Azv7avjskJftww9Kpdis2VOEWi/2VUcR1GODP+mu66IFEvO4OtALwy71qC BvdTQX7B/XHtJsWXsG9jQu4UM2C9H5qTv94E9PCQkwu+HWswLAarF+XaiS+7HsHnJgTk SW1OurnMsI4UfaoxWVuUC72s6PXM+DeKy2CJMe6VKQK+6ROxfwnNw4HoNenYvYNvS/GI TdEiOMAveYBdS2nyIvZH4KbKNSiYV+xqsBz9bfumHOiwEWlF7J43z7bcNjuwx4ehn+6I 4uO4YQnoFRHLVQ5KWh8IV+lyC10eTEHhI3SpaCgwwa0uUX07CwFGYua1tdKG2PJ0Hy1e mWwA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=HmbJMSA+rDrUIPzrZfECGQfunbWYzs60GG/4g7pvyeo=; fh=1pO/fnqlbA1DlSshLp2Kp23nBXobkeZ558bQEuNU3Z8=; b=RmmYAFVrPVuUbOGjKbFlbTuEsa24dKWt7oMe3m/vmhgjS1Vwmo8v7Ilez/+Q/blmYr ANVDV3jaD4CF9inherrz3qQo3V5ccG1EpPAGiCDqOgjDfo9APyDUiJBxj4or7fqzqfBV SF560IiQKrspkfh9R/XfUgOH+FR3x5kKOtII7nxYmbsq/Dj+2Fdb6kN4/f8U6ZMLcco0 YKC4dum1yynbHcNUOgQx6MXPZjyuBjd66qr+ot7MiPvn0hRiY2GOwLsv6Ww0SR6PBdPK gDkJXXEhYBsv5biD5Vy5kliLtffnYRpGXN7yml0RGVR9jbKbTChLQAheV5NZiykmbI4Q pslw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-70558-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70558-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id a18-20020a17090ad81200b00296a72882c4si3311947pjv.74.2024.02.18.11.52.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 11:52:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70558-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-70558-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70558-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 9F541B212EA for ; Sun, 18 Feb 2024 19:52:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4BFE17317E; Sun, 18 Feb 2024 19:51:37 +0000 (UTC) Received: from alerce.blitiri.com.ar (alerce.blitiri.com.ar [49.12.208.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8366A6F53E for ; Sun, 18 Feb 2024 19:51:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=49.12.208.134 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285895; cv=none; b=UVoghnq1iGkqkB884zZRC3xMY+PrcHV4ZKOyWvAGk/CGlMmzJlMLSuoZSVC4yFWMMKG2bJbmd8FJrNsMbLh2qn6xMV0xNfjVBIgeItFPp7027ST0F5B2mR9yyv/zca7f0OoKglqFSrVZHl0fL2Ky3lfFRWpImkC6iKrn+E+sBnk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285895; c=relaxed/simple; bh=yW/KTPvl1LdQSw2p4A5TjI0BdCEzeZVKTkHAyd72DpQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fy7oajpEZ+ggijQDl4MUE9/+W8Ir4XcYPGwkCybtYpivfD2quvroAihH7dBHxJjyzctajASarJozLUVQuozLrIR+F9y6iLSOHrtfBa6FmF1fG3TXbEmPAYNB2RjUVE5GO/dHtsrAXKw2zkQGvel4RZ+RVQQyd5AI7IsNoXV0/hg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sdfg.com.ar; spf=pass smtp.mailfrom=sdfg.com.ar; arc=none smtp.client-ip=49.12.208.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sdfg.com.ar Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sdfg.com.ar Received: from lindsay.fibertel.com.ar by sdfg.com.ar (chasquid) with ESMTPSA tls TLS_AES_128_GCM_SHA256 (over submission, TLS-1.3, envelope from "rodrigo@sdfg.com.ar") ; Sun, 18 Feb 2024 19:51:31 +0000 From: Rodrigo Campos To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Cc: linux-kernel@vger.kernel.org, Rodrigo Campos Subject: [PATCH v3 1/4] tools/nolibc/string: export strlen() Date: Sun, 18 Feb 2024 16:51:03 -0300 Message-ID: <20240218195110.1386840-2-rodrigo@sdfg.com.ar> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240218195110.1386840-1-rodrigo@sdfg.com.ar> References: <20240218195110.1386840-1-rodrigo@sdfg.com.ar> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791267636109897266 X-GMAIL-MSGID: 1791267636109897266 As with commit 8d304a374023, "tools/nolibc/string: export memset() and memmove()", gcc -Os without -ffreestanding may fail to compile with: cc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib -lgcc -static -o test test.c /usr/bin/ld: /tmp/cccIasKL.o: in function `main': test.c:(.text.startup+0x1e): undefined reference to `strlen' collect2: error: ld returned 1 exit status As on the aforementioned commit, this patch adds a section to export this function so compilation works on those cases too. Signed-off-by: Rodrigo Campos --- tools/include/nolibc/string.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index a01c69dd495f..ed15c22b1b2a 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -123,7 +123,7 @@ char *strcpy(char *dst, const char *src) * thus itself, hence the asm() statement below that's meant to disable this * confusing practice. */ -static __attribute__((unused)) +__attribute__((weak,unused,section(".text.nolibc_strlen"))) size_t strlen(const char *str) { size_t len; From patchwork Sun Feb 18 19:51:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodrigo Campos X-Patchwork-Id: 202874 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp928921dyc; Sun, 18 Feb 2024 11:52:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU22vDPHJymzmF1SKj407SEQFxOFgnYR+/98bz5Hvm5jKIlpgI/oyroVMMPCMP9CXNBxQmJHpAIbfKRln6KFEw3Ny0Egg== X-Google-Smtp-Source: AGHT+IEcK/ZgH3lkwAVykITUAw3qEltYrL34/k1Ti4H3uudypJ/P3/R5UaSl85nwEr0pQHoKNsBU X-Received: by 2002:a17:906:3888:b0:a3e:abb4:d452 with SMTP id q8-20020a170906388800b00a3eabb4d452mr9406ejd.34.1708285936392; Sun, 18 Feb 2024 11:52:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708285936; cv=pass; d=google.com; s=arc-20160816; b=b8wfjWKsl822rbchr1d4kXXAw6/LCsNiUPR6bqJAoq7zj05SRlo7D+cwacK77N5SbW D6vfZHbKDtJQ7BwjBwjJmJz0DazpnqF0TPzluEna+slC6pHjoMWupoc+Tan/ACHgpUiY 22pj5Ifvt4HMKXxJ/To3m7WVKfqWCCTgq/VDmjeecxEPhWN1ICo0XsYn5WGEsS4OJonh 81jxLGRkP/elCLod7j9kmO6Qa/Fht7vA30+virqm1GYkLpyY+Npz1VRWNV0EpJH1nf/F Gw2XjNfuCM6PLJvR7pmFbvUAZHSnloWBU36MF8f1sGL5k1eC5z24DIAxAUQezLCw4y8g ZxiA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=/Jj/dTfBMxiOSprWgnxYhcQdbzES3oh6wRnpL+NDXaU=; fh=1pO/fnqlbA1DlSshLp2Kp23nBXobkeZ558bQEuNU3Z8=; b=E6OdLQHL7NDrubTh1w2rtkMEZnvgCLNpvZ3iybqEWLFE7X0+u5LAmjvM9Ey13ClQIm U3radwFbBil4f0PfNJN+HU6R/yhqsSo7DqFfcp01zY85L+cHawIm9QRAAxBU3nQFhzJB hYA11Pw55NLUT9zzT/fvi1Kh2L3m9B5bkZoF4BtSeHlefK/Sa7hsy8NTXOMSNNWxHtIV ZLVBs5WfFZ/CGko3IvALizof0LOHzFNGv14+VhGQRHbj6RHtsKLv3S0S2/wErlECrLoD UET2s6C8goinseFrrZ7po0M2StwDJ6bHENEDNeTtI02WSysgbWzK4d02763t2cn/0Nrh DeEQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-70559-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70559-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id r25-20020a170906351900b00a3ea73794f6si47490eja.412.2024.02.18.11.52.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 11:52:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70559-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-70559-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70559-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0232F1F21373 for ; Sun, 18 Feb 2024 19:52:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E35B074262; Sun, 18 Feb 2024 19:51:38 +0000 (UTC) Received: from alerce.blitiri.com.ar (alerce.blitiri.com.ar [49.12.208.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D7DD71B57 for ; Sun, 18 Feb 2024 19:51:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=49.12.208.134 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285897; cv=none; b=GlwdBe3qukoy7h3uSTauG8J4ujiECaP2B9JMnh94WZFg/HwLX6eJhsU50Sv2O8Zj2K3L0IUBQgf+kiBE5ZaDsLKzSmklikfmL3ufVx9Q1eVaThyRAbxe34jkCaLPjF6w60wRtIvVbiZkJi8Qq/HcESUcEyouPuzNbTYRkyulxuA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285897; c=relaxed/simple; bh=M6eSb0oeupi57UE5O2+bBR18+q2i2okEZ18XgE2QKuM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qW0UwJMoQFow3JLlC0O4jHZOjXFxngqO/docVF3hjhODOCEYBjFkZbH3DT8gtzMtXXCC/stIEYPZ168FA3K9Z77/iKWJRCgU05DGTjvOIwVU5+1xMvgcB5qU58I1aNuAlCU9YQNa83SV5PGLfEDOCgI6rNmCkHAnSJaOybveUb8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sdfg.com.ar; spf=pass smtp.mailfrom=sdfg.com.ar; arc=none smtp.client-ip=49.12.208.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sdfg.com.ar Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sdfg.com.ar Received: from lindsay.fibertel.com.ar by sdfg.com.ar (chasquid) with ESMTPSA tls TLS_AES_128_GCM_SHA256 (over submission, TLS-1.3, envelope from "rodrigo@sdfg.com.ar") ; Sun, 18 Feb 2024 19:51:33 +0000 From: Rodrigo Campos To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Cc: linux-kernel@vger.kernel.org, Rodrigo Campos Subject: [PATCH v3 2/4] tools/nolibc: Fix strlcat() return code and size usage Date: Sun, 18 Feb 2024 16:51:04 -0300 Message-ID: <20240218195110.1386840-3-rodrigo@sdfg.com.ar> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240218195110.1386840-1-rodrigo@sdfg.com.ar> References: <20240218195110.1386840-1-rodrigo@sdfg.com.ar> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791267634281150520 X-GMAIL-MSGID: 1791267634281150520 The return code should always be strlen(src) + strnlen(dst, size). Let's make sure to copy at most size-1 bytes from src and null-terminate the dst buffer if we did copied something. While we can use strnlen() and strncpy() to implement strlcat(), this is simple enough and results in shorter code when compiled. Signed-off-by: Rodrigo Campos --- tools/include/nolibc/string.h | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index ed15c22b1b2a..cc51fd6b63d0 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -187,22 +187,31 @@ char *strndup(const char *str, size_t maxlen) static __attribute__((unused)) size_t strlcat(char *dst, const char *src, size_t size) { - size_t len; - char c; + size_t len = 0; - for (len = 0; dst[len]; len++) - ; + for (; len < size; len++) { + if (dst[len] == '\0') + break; + } - for (;;) { - c = *src; - if (len < size) - dst[len] = c; - if (!c) + /* + * We want len < size-1. But as size is unsigned and can wrap + * around, we use len + 1 instead. + */ + while (len + 1 < size) { + dst[len] = *src; + if (*src == '\0') break; len++; src++; } + if (len < size) + dst[len] = '\0'; + + while (*src++) + len++; + return len; } From patchwork Sun Feb 18 19:51:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodrigo Campos X-Patchwork-Id: 202876 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp929069dyc; Sun, 18 Feb 2024 11:52:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUiNak7KHI7RrYawmfF+aJtvwGBWjy//FO/faSTw7DqiG4/T+c9bVel53ObNMOB6ZpIK2tpYeDiU1GQYwq44O1IxdeOOA== X-Google-Smtp-Source: AGHT+IHXJDD6eo6qU+0PWrDAJqb8wRxagxwMSCCwbnpHwa2WyrCOSkLT3rG9E+qW40T4zrrXPA1o X-Received: by 2002:a17:903:8c3:b0:1db:895d:6bfb with SMTP id lk3-20020a17090308c300b001db895d6bfbmr13678980plb.8.1708285975583; Sun, 18 Feb 2024 11:52:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708285975; cv=pass; d=google.com; s=arc-20160816; b=aytUh5gf6Q3jwtBkCCpMc870VsIqPV2SMv7ywhTkXElBKis0vCJUiCl0hAgU/Kz82A he/cxOQLq62K6Xk6zWUHJfeQc5W8DDCEkJ52jsZs1K41s0XDzjrRvN+sfTXSTkWF3Dxx j001E+BtcbaA1btuvSgfzuE8aeV1G1IwFtIBMEv/YN7/bAtrstQ0xnjonKjhNkydnAZe ATIn5DECxZFUL4rG7My7F14wtKDMY+9UiQPSNWahJBXM7octJJUD2ZTp/AmzMaom1c/J alhN/kscrRDVftHjw1Vj3cZPHFNg9svVGL7shlP6yNP644DEEFyzqZWuZ+vm0lgcjOOf WKtQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=TFi0Tkjn4ecYIlq7T2/4l5bOXLFPCOOo7gC0cg+Tcp0=; fh=1pO/fnqlbA1DlSshLp2Kp23nBXobkeZ558bQEuNU3Z8=; b=YM11AyFtjTxp3Xunpew95+bLcOXae8jJO1FqXMirW9/alQUjnq4pJ5nqJ7EYhLUyrZ qZHCiq5TwiHwa12Ui0DJqos5nfoBsIVP8rWIMF9Y4xfb83mQK+MBe3pMVX5lso/BKJke hP5hjR3V9OD3G0uoyfkMaAAj7gpCrfH5RInZXkRp4AQfdJa6XtpjnXbYJm3PFdaKbE9c YcFQBtgKaglLqp9/oXhImWnX0Pmvti9HT5APg5wSxsrfcIrHsnsKzYR0eZifbnitLa24 kMptK99nvwNGsg7Ixo4hGdygPSt3QoS0FR+O8VRDOfYRlMrk6Bj6lFTRePDyL5VGO57c uLQQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-70560-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70560-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id h10-20020a170902f7ca00b001dbe26ae64asi1469517plw.38.2024.02.18.11.52.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 11:52:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70560-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-70560-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70560-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id C40B0B212F7 for ; Sun, 18 Feb 2024 19:52:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5EF3A745D0; Sun, 18 Feb 2024 19:51:40 +0000 (UTC) Received: from alerce.blitiri.com.ar (alerce.blitiri.com.ar [49.12.208.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D86EB73176 for ; Sun, 18 Feb 2024 19:51:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=49.12.208.134 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285898; cv=none; b=ivEvE3lZ4UFPSWFV6lNRzfstBkdGODtr77roOmJ6KP41fujFOqPYoju23pVYlgxcJediYfRh78680FLhsIDO8If8TxZtgfQzmaUQLHP9xpr++BDvKFRrLWCqPjjQS6393zYPdBirb83pjmCC+/ic1YXIWEi23XTR3zrsqMU6/FY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285898; c=relaxed/simple; bh=it0xpWdUNAJBa027kuEXaW7NIG4qOxDWBerpqeL8YXc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B1yxVmrIBp9M2ESTNr5NSQbT27V2L2LqCykpybLCYHTrXRHu4dl5lSKKhkUAGUk558LLGTr3LDLTrV9V+blqp6ciO31VIkq597N5Gb+nv+KfjkgNk7yfAM9PIE9PVe5KbBsgvoG984VErkhVvGT5pZ4pN1bpDK1uwrEwWQwDFEU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sdfg.com.ar; spf=pass smtp.mailfrom=sdfg.com.ar; arc=none smtp.client-ip=49.12.208.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sdfg.com.ar Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sdfg.com.ar Received: from lindsay.fibertel.com.ar by sdfg.com.ar (chasquid) with ESMTPSA tls TLS_AES_128_GCM_SHA256 (over submission, TLS-1.3, envelope from "rodrigo@sdfg.com.ar") ; Sun, 18 Feb 2024 19:51:35 +0000 From: Rodrigo Campos To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Cc: linux-kernel@vger.kernel.org, Rodrigo Campos Subject: [PATCH v3 3/4] tools/nolibc: Fix strlcpy() return code and size usage Date: Sun, 18 Feb 2024 16:51:05 -0300 Message-ID: <20240218195110.1386840-4-rodrigo@sdfg.com.ar> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240218195110.1386840-1-rodrigo@sdfg.com.ar> References: <20240218195110.1386840-1-rodrigo@sdfg.com.ar> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791267675627135804 X-GMAIL-MSGID: 1791267675627135804 The return code should always be strlen(src), and we should copy at most size-1 bytes. While we are there, make sure to null-terminate the dst buffer if we copied something. Signed-off-by: Rodrigo Campos --- tools/include/nolibc/string.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index cc51fd6b63d0..565230a4ad47 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -219,16 +219,18 @@ static __attribute__((unused)) size_t strlcpy(char *dst, const char *src, size_t size) { size_t len; - char c; - for (len = 0;;) { - c = src[len]; - if (len < size) - dst[len] = c; - if (!c) - break; - len++; + for (len = 0; len < size; len++) { + dst[len] = src[len]; + if (!dst[len]) + return len; } + if (size) + dst[size-1] = '\0'; + + while (src[len]) + len++; + return len; } From patchwork Sun Feb 18 19:51:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodrigo Campos X-Patchwork-Id: 202877 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp929141dyc; Sun, 18 Feb 2024 11:53:17 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWrXwliovfIykRqLgJeJzQOUGzcQJDF/Z4Qk87ZGUbhYQli97FWhNq/Udipy+5wogV6CDl34ks6wfVj4/XPzKnBenJvpg== X-Google-Smtp-Source: AGHT+IE4aJUgKl/BuKGUnl4LPoiDqklpe3SfOB5KTuv8Pi4cJPfhIdJO36+pyZywEwDgNCaguXt3 X-Received: by 2002:a05:6870:e315:b0:21e:877d:78d5 with SMTP id z21-20020a056870e31500b0021e877d78d5mr6087619oad.50.1708285997176; Sun, 18 Feb 2024 11:53:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708285997; cv=pass; d=google.com; s=arc-20160816; b=BY9WQ79NVoNDvrLIYMXTQhbbbjuaHMMJoYASeUbfdEvZRqlitekn5iGiq3p2DYB3b1 Dj2rhml5LhMWft8KuM3o0xTBT6gA5EyLG4AEm+FxYX0YFQOP0SpA2eJkh1G8LEGyjWmK LsZbtnY4iLCNtg7fOryseJ2xOw7gPnsjtuXHHeTIXzkzVzTJ917a0FCjAJSIB0ebeOn+ mH64tzVH0QIYf6ujR7b/y2XZtQ2ejW3IDpTPWjuV47+nK/XbjrL6AyM2eA+09xVNpCWL zBKCn+/ouVzkKgjLMH7prrCeS48g3Y9BSc96EIINBrhKlE2aYIMyIq6aV8u4O4ovsue5 fHhA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=a23UA1u2pf2eSJPG9lDzi7mpsnwH/OTJWisWXIBouwo=; fh=1pO/fnqlbA1DlSshLp2Kp23nBXobkeZ558bQEuNU3Z8=; b=0XnbpmtTnwaAq3e8nzfybROIxQVmd1A1Vy1wR9uNhbjpHLGZb/KBMlxXely3uDXcrU BwGfG68EU/A/ZYmuH0WCgXqa/bEp/CXPtCFK/wFToDjwJMie1uguFiUSemYtdLpT8Fpx ce1aXb3LAe3B1mOvdFLAhSQAsSsyXggMQGZkX/tE/1kj4uzptqU434bfgw9ENJ25jGqs M8ATeN0kUaWgesNSFZga2u6FlOqJfGVnE4wabwiCVNmoS7HJQkyH0zp7Bx2/eoD1Ev6N ZRwzicGhxi1IGktJC88O+AFlGoXsAQrKGDzLuXLK2i0DZvBzs+ixW9KP6PXJGjyI+6Uo 1gZg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-70561-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70561-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id a18-20020a17090ad81200b00296a72882c4si3311947pjv.74.2024.02.18.11.53.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 11:53:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70561-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=sdfg.com.ar); spf=pass (google.com: domain of linux-kernel+bounces-70561-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70561-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 8BA06B213F3 for ; Sun, 18 Feb 2024 19:52:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 04CDE745E9; Sun, 18 Feb 2024 19:51:42 +0000 (UTC) Received: from alerce.blitiri.com.ar (alerce.blitiri.com.ar [49.12.208.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B1E9D7319A for ; Sun, 18 Feb 2024 19:51:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=49.12.208.134 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285900; cv=none; b=dLvai4KIONbkN59Nk3zgFL2bQEuzeIAJr94bxDDZf7ZXnfFo+EQ3Noi62PKAiN/DNoJ1SGDU2v6vs92szg+omCJ/EqTH7PhkZ9mbYooD0kzofuBwtomvE3vRw3BFzHPr98jf7INK+YmPVV0hx3AO4wi5itouSDdpnEh32UgHJxM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708285900; c=relaxed/simple; bh=9RVmOlD4OgrvSuTTroVq8lj5KRTmT9fr3g11wmsxIZQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BdUqKgOBMwh+cHWGnGIIwF/WUwcXtSdTSqjGK/t/tLzQakldjxD4ysSXdqQ4hzEo7IVvSGh6g02c/8dM9KP0uwjPm1BctI/VHn26vEpIlw4ZVWEMtOpXCpXZ4e4DcgKUZ1re/fDE0GHEyuFySLGZE5rN/rz/+dTmrbJD78p+aKE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sdfg.com.ar; spf=pass smtp.mailfrom=sdfg.com.ar; arc=none smtp.client-ip=49.12.208.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sdfg.com.ar Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sdfg.com.ar Received: from lindsay.fibertel.com.ar by sdfg.com.ar (chasquid) with ESMTPSA tls TLS_AES_128_GCM_SHA256 (over submission, TLS-1.3, envelope from "rodrigo@sdfg.com.ar") ; Sun, 18 Feb 2024 19:51:36 +0000 From: Rodrigo Campos To: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Cc: linux-kernel@vger.kernel.org, Rodrigo Campos Subject: [PATCH v3 4/4] selftests/nolibc: Add tests for strlcat() and strlcpy() Date: Sun, 18 Feb 2024 16:51:06 -0300 Message-ID: <20240218195110.1386840-5-rodrigo@sdfg.com.ar> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240218195110.1386840-1-rodrigo@sdfg.com.ar> References: <20240218195110.1386840-1-rodrigo@sdfg.com.ar> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791267698035558114 X-GMAIL-MSGID: 1791267698035558114 I've verified that the tests matches libbsd's strlcat()/strlcpy() implementation. Please note that as strlcat()/strlcpy() are not part of the libc, the tests are only compiled when using nolibc. Signed-off-by: Rodrigo Campos --- tools/testing/selftests/nolibc/nolibc-test.c | 40 ++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 6ba4f8275ac4..d373fc14706c 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -600,6 +600,25 @@ int expect_strne(const char *expr, int llen, const char *cmp) return ret; } +#define EXPECT_STRBUFEQ(cond, expr, buf, val, cmp) \ + do { if (!(cond)) result(llen, SKIPPED); else ret += expect_str_buf_eq(expr, buf, val, llen, cmp); } while (0) + +static __attribute__((unused)) +int expect_str_buf_eq(size_t expr, const char *buf, size_t val, int llen, const char *cmp) +{ + llen += printf(" = %lu <%s> ", expr, buf); + if (strcmp(buf, cmp) != 0) { + result(llen, FAIL); + return 1; + } + if (expr != val) { + result(llen, FAIL); + return 1; + } + + result(llen, OK); + return 0; +} /* declare tests based on line numbers. There must be exactly one test per line. */ #define CASE_TEST(name) \ @@ -991,6 +1010,14 @@ int run_stdlib(int min, int max) for (test = min; test >= 0 && test <= max; test++) { int llen = 0; /* line length */ + /* For functions that take a long buffer, like strlcat() + * Add some more chars after the \0, to test functions that overwrite the buffer set + * the \0 at the exact right position. + */ + char buf[10] = "test123456"; + buf[4] = '\0'; + + /* avoid leaving empty lines below, this will insert holes into * test numbers. */ @@ -1007,6 +1034,19 @@ int run_stdlib(int min, int max) CASE_TEST(strchr_foobar_z); EXPECT_STRZR(1, strchr("foobar", 'z')); break; CASE_TEST(strrchr_foobar_o); EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break; CASE_TEST(strrchr_foobar_z); EXPECT_STRZR(1, strrchr("foobar", 'z')); break; +#ifdef NOLIBC + CASE_TEST(strlcat_0); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 0), buf, 3, "test"); break; + CASE_TEST(strlcat_1); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 1), buf, 4, "test"); break; + CASE_TEST(strlcat_5); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 5), buf, 7, "test"); break; + CASE_TEST(strlcat_6); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 6), buf, 7, "testb"); break; + CASE_TEST(strlcat_7); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 7), buf, 7, "testba"); break; + CASE_TEST(strlcat_8); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 8), buf, 7, "testbar"); break; + CASE_TEST(strlcpy_0); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 0), buf, 3, "test"); break; + CASE_TEST(strlcpy_1); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 1), buf, 3, ""); break; + CASE_TEST(strlcpy_2); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 2), buf, 3, "b"); break; + CASE_TEST(strlcpy_3); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 3), buf, 3, "ba"); break; + CASE_TEST(strlcpy_4); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 4), buf, 3, "bar"); break; +#endif CASE_TEST(memcmp_20_20); EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break; CASE_TEST(memcmp_20_60); EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break; CASE_TEST(memcmp_60_20); EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break;