From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 F09FD347532 for ; Mon, 4 May 2026 21:23:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777929788; cv=none; b=LNoAyD5EaBv1hExMKUtKHVqJJm0KqVfOiEkqFixwHWh0M1zWaKBu79kv+uheMQnnvCkumxhcW/HoeFkbMVTvU4f3TZGyDMVFkaFgrFgrt4QceSCp5Ec7yxzfAHTJgdy2tUfQ3WT7iAL0Bl3bhc3Tov+PqQUMojnDp6zpkIDo9t0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777929788; c=relaxed/simple; bh=CA6FQk89TOA4baC96QtgPVdZS7dng4OcxdsdoWy3+g0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ug2NUX9acVxjPuWnlRW/92Tevy0ysdCmP9659FgKwh6u2Gp7/ktvTCxCxxK8j6Fhd+whijE3V2tmfi4HHQEpEz59rshClB8+GPLNZDZ9OWmRc++ex4bG2ZxRg5udlLc00knCEt7cGyMvoDEoOvB+gIBolFRJ44CVRPuKpPcSJ30= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FXT/fqHZ; arc=none smtp.client-ip=209.85.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FXT/fqHZ" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4891f6b6388so2509305e9.0 for ; Mon, 04 May 2026 14:23:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777929784; x=1778534584; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=n+yOUy5o4tyUwA3ddWOnmqsznj/i+jEBSQPJyT11Cik=; b=FXT/fqHZ/uCsh1xgm/Xq+WdcaYS0NgjItuHGlZKbmSi+O4bQ0jtZ6hgKMPn/sbNOE5 oWQrugOlHmtoStAS4Wwpft3s4msyZYgYDfoQtIOb/IKnJevIVjNL3aPaZv+3qNkHc8tW FypVTy1jAfXyshK2UXZ84KRc6ZbUPGHCz1sVunDvGg3cNZgPSfTkDaxfaXwT0rgc6rXd UD2Zk5OpZuWhvhF5u4wJhXNK00/rcX8tQ+pNztFtT+1JLZbmbLNnGySctW1gSwNB8bEM 495MIznawQWjR25/6vZnX4Ci/hIyj2wGj3nlzoZFbLl+BrHmsYPOFQJUOLJE84xG5jw+ /r7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777929784; x=1778534584; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=n+yOUy5o4tyUwA3ddWOnmqsznj/i+jEBSQPJyT11Cik=; b=p9PjM9GveWhyZmreCswCo3Q6CyX8nAOCkmgGQYTzAN2K1gdhbEgv6FN/ot+JwOAIMi Fcg4wox1DjpvhsSBuSU0bT7aak1nz6c3jpdk5Ml4CfCYTbShEdvFhtfb4lUgXaAI0HY/ EuzshTt9dxarR8tUfh+7Gurr5guq+7F24O63skuqSFNM5kvk13UnuelMcGXU2QawhNtn xKkIQUv4GvOaoC4J8kcrx6if6B6UbZpsUk9/eDsQglB8EGyvQO61eQEIo/wVfMD/5UyB pc60n9Dw0UdzwIei9i3bFPaDjUTV9wem96mykemRj8H3Kx9nUEEBbcy02GFvLnLg2cED kRaA== X-Forwarded-Encrypted: i=1; AFNElJ+j0O9jcTfqKyybUiUPw88UAeBv5t+3htHPeS8tWBj+zPeYPdrEABkZkqhnGhZlrq+sE+F2TWvPJHCKo5M=@vger.kernel.org X-Gm-Message-State: AOJu0YxF2JqEI+W0ecr/VQBSTJuD7nZpprgTgNCE7G0vdH0tfvkMZ2z+ H2NU0Wmf9IeO7NKUDmcLvOWaX5eu08Yc/XIekKU8kbUNBnDJ6b4h/fKVsI6+GS2zzNU= X-Gm-Gg: AeBDietpwrxewooZnWTtHwYybNW+l9Afn9z941dMMKG0JMUgAp4/FXcCgNxcbj9hXW3 PvKSyRCq3VvjGNKv2NCBFXPrCxzW/RlDPS1FWV68XL//nORnuLRFUOnZpMthScC+IFsg10bv7rn XIpt7TLHI1jQ/pawDsR6OyujA9R1ZH0RbGgqVQ9Uwu4J1HFctcv77g8Y0QRs+2odmQEadrcczE+ gU2rHtkKF7z6X9H7TQuM12HifLeJEGZYHlti7z908YYv67ver3e1sSYmQ2c6MUDmTB11CGwHf0p VgbSqmLsI9+XAooPWwwD+w8vnHgJy0nz5L0WuA7Fh8KczjeSGU68waLtbom6pTi0IGoeUHj7/OO ByX/w0rKblYJtQtyVw66nDCJwCH6tDDJhcsfy0e1P4KoCFky3KoCqM+9VD2aFQ7HZnZA7fV2M+h i8+UYxVCYUycZzC7St36YB X-Received: by 2002:a05:600c:4f0b:b0:485:f1d6:2b1d with SMTP id 5b1f17b1804b1-48a980fb8f1mr84642285e9.0.1777929784337; Mon, 04 May 2026 14:23:04 -0700 (PDT) Received: from lucas ([2a01:e0a:354:c290:234c:5dad:48b0:9188]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48a824f9f0dsm335135835e9.15.2026.05.04.14.23.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 May 2026 14:23:04 -0700 (PDT) From: Lucas Poupeau To: jpoimboe@kernel.org, pmladek@suse.com Cc: m32285159@gmail.com, linux-kernel@vger.kernel.org, Lucas Poupeau Subject: [PATCH] tools: include: add proper strscpy() declaration Date: Mon, 4 May 2026 23:23:01 +0200 Message-ID: <20260504212301.63750-1-lucasp.linux@gmail.com> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Currently, strscpy() is defined as a macro for strcpy() in the tools headers. This is unsafe and prevents using the real strscpy() logic that provides better buffer overflow protection. Remove the macro hack and add a proper extern declaration for strscpy(). This allows tools to use the safer string copying API once the implementation is provided. Suggested-by: Maxwell Doose Signed-off-by: Lucas Poupeau --- tools/include/linux/string.h | 5 ++++- tools/lib/string.c | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/tools/include/linux/string.h b/tools/include/linux/string.h index 51ad3cf4fa82..4f3547d0cd84 100644 --- a/tools/include/linux/string.h +++ b/tools/include/linux/string.h @@ -12,7 +12,6 @@ void argv_free(char **argv); int strtobool(const char *s, bool *res); -#define strscpy strcpy /* * glibc based builds needs the extern while uClibc doesn't. @@ -30,6 +29,10 @@ extern size_t strlcpy(char *dest, const char *src, size_t size); #endif #endif +extern ssize_t strscpy(char *dest, const char *src, size_t count); + +char *str_error_r(int errnum, char *buf, size_t buflen); + char *str_error_r(int errnum, char *buf, size_t buflen); char *strreplace(char *s, char old, char new); diff --git a/tools/lib/string.c b/tools/lib/string.c index 3126d2cff716..12fabbe583cf 100644 --- a/tools/lib/string.c +++ b/tools/lib/string.c @@ -36,6 +36,43 @@ void *memdup(const void *src, size_t len) return p; } +/** + * 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 source string to the destination buffer. The result is + * always a valid NUL-terminated string that fits in the buffer. + * + * Return: + * * The number of characters copied (not including the trailing NUL) + * * -E2BIG if count is 0 or @src was truncated. + */ +ssize_t strscpy(char *dest, const char *src, size_t count) +{ + size_t res = 0; + + if (count == 0) + return -E2BIG; + + while (count) { + char c = src[res]; + + dest[res] = c; + if (!c) + return res; + res++; + count--; + } + + /* Hit buffer length without finding a NUL; force NUL-termination. */ + if (res) + dest[res-1] = '\0'; + + return -E2BIG; +} + /** * strtobool - convert common user inputs into boolean values * @s: input string -- 2.54.0