From patchwork Fri Feb 9 09:36:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kirjanov X-Patchwork-Id: 13551005 X-Patchwork-Delegate: stephen@networkplumber.org Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (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 70BA2664AC for ; Fri, 9 Feb 2024 09:37:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707471456; cv=none; b=L6YI6dfVYFW1INb40UJCUKd6mCVbRg7ZkYF+WWnJRdOsFDkKUVrAXQRaArcAAPjsXO2BCx3E3CJv+9emp7n4f2IzCMPae9G6/34gZzUyRDw65wE5k6upcgfZ3GUpwPfjnaomU6Z4hIxZ2cLGj0vDQuxdREIgE7rMF4qBAqaTAw0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707471456; c=relaxed/simple; bh=IBfwb5xJFywY7Qg/BA7Zs8R0H/jAYwUE/V+/jZ60DHM=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=l7yIfOhD4U14MaWSgHOn8DKCstVSzHyQiO3yle9wYtMlo2i1j8MFJGiBbRP66LWYPKXVM7kq0k93mQcFSGUkGj16Tn3hzbd7V2sD9Pw/yT1LlwKcAzxcpOqqv4+MrTcmN3XdXRxTwsQuX8OZFUZ2f9bLIHFgR/bCoOWaa6evWeY= 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=VFHxFIFA; arc=none smtp.client-ip=209.85.167.50 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="VFHxFIFA" Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-511625a9d84so237463e87.1 for ; Fri, 09 Feb 2024 01:37:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707471452; x=1708076252; 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=qQHED3yEDzAPWIE5f1wk40fziHzGqB47Kk7cOI44Hjs=; b=VFHxFIFAAPragUV5lwpSk4swPtXvgeNE46ARnw4QaHFgTa/d7eCrJqFtaY7OOj1pDH 0jEezfrgZzGeyKl/EdWyBIZLL4E7frnoLqYoy8I4mkP9C6quBM/Q6+cdxMFlxWnVE/yk rRpRT6iKz9dLbyKl8/x0d8Wd/+gulA+vhn0jNkfN1czOevLLt36NlaHtvaZCgalf7P1c viLXqJMAq1FI3MpOJ74WMt8YDHUx+leRnWZawFVLU5FefUXbTy25OTZeT9F1lCsqMOuw ONWYk4zXbpQ8FUCck52qAOHtdyCi/C6XG8GzVBz0toMc2qegdepPFtqCeblow1Z9hPwk uCmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707471452; x=1708076252; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qQHED3yEDzAPWIE5f1wk40fziHzGqB47Kk7cOI44Hjs=; b=BpE2zcb3R2IwwbujkV3Quwtctt9F0WGIeI9TYphLD4Vuv9qa3rSzi+E8/gqNZNWuHU bCwu5/kfNiFlgsw4Zn5dPRg5QdoTH+rIJX3bcZ/maJE+V4BxjigeTi2JhfECqMX7N5do M1vUdrkx/GdS10or1S9oe4lNut/8NKqToir2T/u/JKYZwprylxlUEQWAFQG9Wfx7t2kU wzDEFzAfSypmoYyybqsSrBPtyYg15bz0MktJ8Msqv1X8uSts34A8jp9/8DPCewVZt88L U3DtimgUSs7jr6+WFZC5gWOwSlc5WzUN7jnWJWi+Z+Iq6VZg2GEnZwS/DV+VorBdrMTw QoPw== X-Gm-Message-State: AOJu0Yyk5MbILwjlXqqWYlfRnbRabhoJBQxNYvnR8KCm6eMla0UMUsZV HcI8BW8Ri/Zugo6TGR0jgaeDuFK/y3B+SlTrCil2eg8awH0gdHBSwwSR2oPHaHt4Mg== X-Google-Smtp-Source: AGHT+IFbaPzAbdEv+f0IpIGz0tpM0ag+iXjiK9GcdpHl+se5MNkKbs8j6I1IgUjY5V8Q2R5wYPloyA== X-Received: by 2002:ac2:4146:0:b0:511:5b50:4cca with SMTP id c6-20020ac24146000000b005115b504ccamr530478lfi.5.1707471452176; Fri, 09 Feb 2024 01:37:32 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWhgbhbbNySP/4S3JQvGRfH1ijueIoo4Pqu02+Rg/ObHoXvejNQYsgS5Bc+qq5S+dD11V0wir8pyan+ucS0s+j93yJvQmXHC/ATQUsg7MwXJTpfId0a Received: from localhost.localdomain ([83.217.200.104]) by smtp.gmail.com with ESMTPSA id 8-20020ac24828000000b005117ac9cd1asm49659lft.88.2024.02.09.01.37.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 01:37:31 -0800 (PST) From: Denis Kirjanov X-Google-Original-From: Denis Kirjanov To: stephen@networkplumber.org, dsahern@kernel.org Cc: netdev@vger.kernel.org, Denis Kirjanov Subject: [PATCH iproute2 1/2] lib: utils: introduce scnprintf Date: Fri, 9 Feb 2024 04:36:18 -0500 Message-Id: <20240209093619.2553-1-dkirjanov@suse.de> X-Mailer: git-send-email 2.30.2 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: dsahern@gmail.com The function is similar to the standard snprintf but returns the number of characters actually written to @buf argument excluding the trailing '\0' Signed-off-by: Denis Kirjanov --- include/utils.h | 1 + lib/utils.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/utils.h b/include/utils.h index 9ba129b8..5b65edc5 100644 --- a/include/utils.h +++ b/include/utils.h @@ -393,4 +393,5 @@ int proto_a2n(unsigned short *id, const char *buf, const char *proto_n2a(unsigned short id, char *buf, int len, const struct proto *proto_tb, size_t tb_len); +int scnprintf(char * buf, size_t size, const char * fmt, ...); #endif /* __UTILS_H__ */ diff --git a/lib/utils.c b/lib/utils.c index 6c1c1a8d..752da66f 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -2003,3 +2004,16 @@ int proto_a2n(unsigned short *id, const char *buf, return 0; } + +int scnprintf(char * buf, size_t size, const char * fmt, ...) +{ + ssize_t ssize = size; + va_list args; + int i; + + va_start(args, fmt); + i = vsnprintf(buf, size, fmt, args); + va_end(args); + + return (i >= ssize) ? (ssize - 1) : i; +} From patchwork Fri Feb 9 09:36:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kirjanov X-Patchwork-Id: 13551006 X-Patchwork-Delegate: stephen@networkplumber.org Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) (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 755FC664AC for ; Fri, 9 Feb 2024 09:37:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707471460; cv=none; b=oQst5XPDTr4tVeYMjib/gKkW9Q+6wVnez3Xk/DFgBbWuezEQ/yOtN7vkllCuPMcBX4fnRnkRNPQKxp6bCxljDp388cUS5Uktyd18wCCIuP3/5MUbUBMHHptUBywwvZUpWEapSCL6HJ3Ak5MRPccrH6KTEdSkgfze9ftvOTwjt4s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707471460; c=relaxed/simple; bh=+RI+UfoJgxND/+bJlc6Ob65S7aonmRfy53IzorOPk4w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BPnfqegOzcE1XiGAUq4llia/JiXrbnZUssox91wLl//+IpekT4r/eArPqEsqEO2zmW9KAT5rUVeXZsBB5L0A+VMx/XZHbw5Wz+JqE0AgUSkPJvSoNDxXzhOmxDXSDFgMhmXxpv87MhI8btVj0j+BLuocfHZOXmuKGMSSBkUyfAc= 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=K1DVUfgg; arc=none smtp.client-ip=209.85.208.171 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="K1DVUfgg" Received: by mail-lj1-f171.google.com with SMTP id 38308e7fff4ca-2d0cc05abd3so3444101fa.0 for ; Fri, 09 Feb 2024 01:37:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707471456; x=1708076256; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ged2+EmYXVpHpcnrvAq19WriE+z/AxxAUjrYqs6VoOo=; b=K1DVUfggv3r9+fZjiaeJTqHX925xAaBGMcDHKhDWQ9Q+pAW8if2+wGnFMRn2CkwfAQ W7GyeMXjKsMI9CpGKrtEXMJj6Qh6Ld4ZtW1ufF7RZt4jJO0ryIB3ePt+DKffLtRB1NqY BHaIBmFq4Va2qpCastV0kVSvHJ7MhGNq8+RbUs4WQzdpkuscokpKgqBnWidKcS7fqUJn +bo0YbokKu/EUIP+LZe1Ec9NsBrhn7XIWl3YxQ3l7s9t01JD9zaymdFtKq456lIu2ici aHn5kf9hhRmwBSeLhahgu5xa4+1JJ4m0WFE5Mh8GGJ1IiTB1kJRnC2BOkaApSF91g8c8 Hsog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707471456; x=1708076256; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ged2+EmYXVpHpcnrvAq19WriE+z/AxxAUjrYqs6VoOo=; b=kSlDsyQrc7BzVFJtQvfmasU8QWIHMfzZ3FgOD0EN9yV103IYWx67XI598Bl0yqcBk/ 5AmxOURMI/F1J7q/JOyXXAS07NckZRBnVf16xWNtIyGeYJI/WG0y5mp2VcN5kyvdpfrb toefNf0sT+TFlBHLr2+u1dSJZkrT4xUeuRlBC3vx3rCj3zqN1ju3xDnKx/EYwSfNJzZG /eStIusKyu3ktA/ets9uwVujlmXmUokl+QUmwBHkF4V8vyVei/361UJjLRPOSycS656Y z6mpOyQNC8hNnc10AjpvuQ8gubwS/lpqhF99R1MTIoI+0Ey1rvybUAD2Np0nGM8jFh1d 178w== X-Gm-Message-State: AOJu0YzV9RwhO8nT8w4aornCPu2us7XUMf3PbyYkB90aEr/fYEd6L+3W TzfuwjY/FrIY4+6adUfpW1OmcGqclQ/L1JFGU6mG6nRtdGYY/Fz4 X-Google-Smtp-Source: AGHT+IGucg36cX0Dxi5j6FGLxju1pBPk5ib/WoFO/eiC3g4byQEtdBOELhiM7TBpV6tyEdJmBiUALw== X-Received: by 2002:a05:6512:3e26:b0:511:79ce:804c with SMTP id i38-20020a0565123e2600b0051179ce804cmr592804lfv.0.1707471456158; Fri, 09 Feb 2024 01:37:36 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCV2KJNVWIZzYux2Z6Aq8Z/IEVnxd48Aokzait6KB+5+p6Lk7LokPfuwe6iN5+BJZQOl2O8D90Ly+pUAq9DY6EzSCiobv95WGfTVc9T9kvFiMv9rRL0S Received: from localhost.localdomain ([83.217.200.104]) by smtp.gmail.com with ESMTPSA id 8-20020ac24828000000b005117ac9cd1asm49659lft.88.2024.02.09.01.37.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 01:37:35 -0800 (PST) From: Denis Kirjanov X-Google-Original-From: Denis Kirjanov To: stephen@networkplumber.org, dsahern@kernel.org Cc: netdev@vger.kernel.org, Denis Kirjanov Subject: [PATCH iproute2 2/2] ifstat: convert string formatting calls to scnprintf Date: Fri, 9 Feb 2024 04:36:19 -0500 Message-Id: <20240209093619.2553-2-dkirjanov@suse.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240209093619.2553-1-dkirjanov@suse.de> References: <20240209093619.2553-1-dkirjanov@suse.de> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: dsahern@gmail.com Use scnprintf to print only valid data Signed-off-by: Denis Kirjanov --- misc/ifstat.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/misc/ifstat.c b/misc/ifstat.c index 721f4914..288e9064 100644 --- a/misc/ifstat.c +++ b/misc/ifstat.c @@ -379,10 +379,10 @@ static void format_rate(FILE *fp, const unsigned long long *vals, fprintf(fp, "%8llu ", vals[i]); if (rates[i] > mega) { - sprintf(temp, "%uM", (unsigned int)(rates[i]/mega)); + scnprintf(temp, sizeof(temp), "%uM", (unsigned int)(rates[i]/mega)); fprintf(fp, "%-6s ", temp); } else if (rates[i] > kilo) { - sprintf(temp, "%uK", (unsigned int)(rates[i]/kilo)); + scnprintf(temp, sizeof(temp), "%uK", (unsigned int)(rates[i]/kilo)); fprintf(fp, "%-6s ", temp); } else fprintf(fp, "%-6u ", (unsigned int)rates[i]); @@ -400,10 +400,10 @@ static void format_pair(FILE *fp, const unsigned long long *vals, int i, int k) fprintf(fp, "%8llu ", vals[i]); if (vals[k] > giga) { - sprintf(temp, "%uM", (unsigned int)(vals[k]/mega)); + scnprintf(temp, sizeof(temp), "%uM", (unsigned int)(vals[k]/mega)); fprintf(fp, "%-6s ", temp); } else if (vals[k] > mega) { - sprintf(temp, "%uK", (unsigned int)(vals[k]/kilo)); + scnprintf(temp, sizeof(temp), "%uK", (unsigned int)(vals[k]/kilo)); fprintf(fp, "%-6s ", temp); } else fprintf(fp, "%-6u ", (unsigned int)vals[k]); @@ -675,7 +675,7 @@ static void server_loop(int fd) p.fd = fd; p.events = p.revents = POLLIN; - sprintf(info_source, "%d.%lu sampling_interval=%d time_const=%d", + scnprintf(info_source, sizeof(info_source), "%d.%lu sampling_interval=%d time_const=%d", getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000); load_info(); @@ -893,7 +893,7 @@ int main(int argc, char *argv[]) sun.sun_family = AF_UNIX; sun.sun_path[0] = 0; - sprintf(sun.sun_path+1, "ifstat%d", getuid()); + scnprintf(sun.sun_path + 1, sizeof(sun.sun_path) - 1, "ifstat%d", getuid()); if (scan_interval > 0) { if (time_constant == 0) @@ -926,14 +926,14 @@ int main(int argc, char *argv[]) npatterns = argc; if (getenv("IFSTAT_HISTORY")) - snprintf(hist_name, sizeof(hist_name), + scnprintf(hist_name, sizeof(hist_name), "%s", getenv("IFSTAT_HISTORY")); else if (!stats_type) - snprintf(hist_name, sizeof(hist_name), + scnprintf(hist_name, sizeof(hist_name), "%s/.ifstat.u%d", P_tmpdir, getuid()); else - snprintf(hist_name, sizeof(hist_name), + scnprintf(hist_name, sizeof(hist_name), "%s/.%s_ifstat.u%d", P_tmpdir, stats_type, getuid());