Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp789577rdb; Sun, 18 Feb 2024 11:52:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU1h98xCkulBx2u3Iq5jMU4ez1sRlXYHi1tNgYbeOgsbumVgIjJBu+3t2e3l5F726OUFd0YckbVYNZ7a8KgBubc9y3yHGEoFsEiSuZsbQ== X-Google-Smtp-Source: AGHT+IFO7gJBlh2NDRrsD/cBGDYYCk6/EVMBSMSML/gRZ6VRpiiz6JIrEJshA5q2Ab5rsD5RmNIo X-Received: by 2002:a17:902:e789:b0:1db:fa84:9be3 with SMTP id cp9-20020a170902e78900b001dbfa849be3mr479752plb.8.1708285927378; Sun, 18 Feb 2024 11:52:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708285927; cv=pass; d=google.com; s=arc-20160816; b=EW/amens21cTCx4U7kWNb0HsgCzjE2DGUXUHg/L75LqJxbug5AE9zSBq+wb+i3qr44 FzBBzlCxnSmJYPUSCZJRWQ3N+N4KWbPmC8VNbhojr5Y+5Bc4H1plEIk7wIppCUP/cXZM PvWjDYNrc0tQnIdxO9B6kjsxmj88AWCFn8m0GOxCBHz2WPQtt2NID1j9/ynqvuJiUEXE JVHioMzhshOUSf7PWOWc4j5b+YY/XczOROj3Hiuh3ypXFKQLW4mXYYCFwnwDz4K28eS+ gcF2JyV2vLh5YVdRLYliyhjxvvwPS8+Y7+VP03+rGAe0UzKXjxAlBnT5uMvwU0tvP9Qr Ktwg== 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-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70559-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id k10-20020a170902d58a00b001db28c4b756si3189626plh.269.2024.02.18.11.52.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 11:52:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70559-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70559-linux.lists.archive=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 14CC6281A84 for ; Sun, 18 Feb 2024 19:52:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DDE5D7318B; 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 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=20Wei=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 Content-Transfer-Encoding: 8bit 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; } -- 2.43.0