From nobody Fri Dec 19 17:26:44 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 6062A26B0B6 for ; Wed, 30 Apr 2025 17:51:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746035478; cv=none; b=EmmcBTDRUyKqUy91g/atlc3lIBL7YFMM1BMBxBx+PDpAywvmf/W56eiMa86HWDYKj0Tj1dnF/kOKwHf8eNOO3DYMF8f+1lh9WZ66gJvoS+nl/jyJW5pZb1UNSc4/RG1rwagHpRov4rkW3YafpbLBo+AHWe4t7G6j3Uqx3f8h9aw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746035478; c=relaxed/simple; bh=Y3D+HeelZHUbj722B9+EEIFwYk/UcruDD4c+xdB3tPw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XN+Zh0eewn/CeOO7kTRYR5apDSIjaCRZHCC2Ia4j2bbuZfeoz7jRectdWM64yitlEg0AgtTUbc6N7WgHCZLorJzqOzFp+ziRteAz9NVG+yaXJtH6jA90K3EHSUuviciItiSB0cUiQRCWHVV5tw7cBrDs+F9VtHuEEBOzNHikcbQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Pd7EaaHM; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Pd7EaaHM" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-227ed471999so733955ad.3 for ; Wed, 30 Apr 2025 10:51:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746035475; x=1746640275; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TZlmxX2xoyYFq1ODXrK6/XcfYk4sIZR3JZBZG1kQdeM=; b=Pd7EaaHMYKvQIDO27JAKLQoCSfG72K3Ibqiyq3z7movTTwcQ8rBHIpiZOeeUhn8+z1 Fc/aqYKG4Raeuwh9cn84p3mkGe4VGBzSiICS8ki9Qq8MuP79GX6gat9+R48R4TvXgK/H zkLx1/+o9WEUzFEuT8cYDy0dhWirQNT8FDMSzildhYyDKsX8hgA0RhEN+ax5bXsNu+jF QWwG3ErGCffrlYp8fwB+74tnlOLt+HhMyt19+7CaeOplJ0fO8UrqAK1GAbzlNj4BF2cp tnUHxori5J1ULMbwKFEf/4rV9YA2ip0AGz34mn1PE4GzHe/FU3RT/xENbEKgKesdnrPv UlHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746035475; x=1746640275; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TZlmxX2xoyYFq1ODXrK6/XcfYk4sIZR3JZBZG1kQdeM=; b=t56FZm+4Qc1HkU45C4m/Q2o/k8QBM1gSLEDNIpAmlW4IDQuZ0pM7HPa1cuQSHu7Nbz 2E2CQqQgEyMLVhZZhn6CkbWfB4/di+sG/28+he93eZEc7Ap1VQIJPq9qkH9T7qhIOngD KuPAuxFdStf73GVAqDDjsRlUQqiEDNcRA7PzVnLedPMHUhxcKo4q4QE5kgdA2oAUtJFp iRHjUrPvJ8AElpzbtdfxTjldafpBsm6AbMLtDXRdR1DuHk8DEg1DaNl3CArkigzuIWLZ HNJsgWFz2RILNYAxsbZuH+ioc6cpLH4uhaeF3s2nqFsbD9mPbHmQln5V2R2BELP8d9o9 /70g== X-Forwarded-Encrypted: i=1; AJvYcCWqtyLlYFNttnwkP0mofdHIbWfJ3k2UGYlX+fGNf3MR2qO0tKrVkdOCWGx4Xt3zsf0As+Kax9GaVfHN2Ew=@vger.kernel.org X-Gm-Message-State: AOJu0YyVVYFNRNt8z2PfBPg64IsVqoTm0UJ7IyVUkho30kCE/XLkCRSf bHB+UsAcc/onWQK89sZsgdl+7yuGT4yENjP9KkQNU7c4Ok281aeXxtnfays+0YINSUrQwb1cI3r khItw/A== X-Google-Smtp-Source: AGHT+IGTl/0MRt+DgDNAUuYugWnKJZds1hQoM1nSd1eYiKimwIiKclutS+1wetnNSbh/OGTZQZOUhIBYkuPV X-Received: from pjbst3.prod.google.com ([2002:a17:90b:1fc3:b0:301:4260:4d23]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1f86:b0:2fe:a8b1:7d8 with SMTP id 98e67ed59e1d1-30a33355ff6mr5623948a91.25.1746035475516; Wed, 30 Apr 2025 10:51:15 -0700 (PDT) Date: Wed, 30 Apr 2025 10:49:57 -0700 In-Reply-To: <20250430175036.184610-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250430175036.184610-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.906.g1f30a19c02-goog Message-ID: <20250430175036.184610-10-irogers@google.com> Subject: [PATCH v2 09/47] tools subcmd: Silence -Wshorten-64-to-32 warnings From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , Yury Norov , Rasmus Villemoes , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , Yicong Yang , Jonathan Cameron , Nathan Chancellor , Bill Wendling , Justin Stitt , Josh Poimboeuf , Al Viro , Kyle Meyer , Ben Gainey , Athira Rajeev , Kajol Jain , Aditya Gupta , Eder Zulian , Dapeng Mi , Kuan-Wei Chiu , He Zhe , Dirk Gouders , Brian Geffon , Ravi Bangoria , Howard Chu , Charlie Jenkins , Colin Ian King , Dominique Martinet , Jann Horn , Masahiro Yamada , Arnd Bergmann , Yang Jihong , Dmitry Vyukov , Andi Kleen , Graham Woodward , Ilkka Koskinen , Anshuman Khandual , Zhongqiu Han , Hao Ge , Tengda Wu , Gabriele Monaco , Chun-Tse Shao , Casey Chen , "Dr. David Alan Gilbert" , Li Huafei , "Steinar H. Gunderson" , Levi Yun , Weilin Wang , Thomas Falcon , Thomas Richter , Andrew Kreimer , "=?UTF-8?q?Krzysztof=20=C5=81opatowski?=" , Christophe Leroy , Jean-Philippe Romain , Junhao He , "Masami Hiramatsu (Google)" , Xu Yang , Steve Clevenger , Zixian Cai , Stephen Brennan , Yujie Liu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org, llvm@lists.linux.dev Cc: Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The clang warning -Wshorten-64-to-32 can be useful to catch inadvertent truncation. In some instances this truncation can lead to changing the sign of a result, for example, truncation to return an int to fit a sort routine. Silence the warning by making the implicit truncation explicit. Move mput_char out of header file to its only use in help.c, while changing its argument types. Signed-off-by: Ian Rogers --- tools/lib/subcmd/help.c | 28 ++++++++++++++++------------ tools/lib/subcmd/help.h | 6 ------ tools/lib/subcmd/parse-options.c | 16 ++++++++-------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/tools/lib/subcmd/help.c b/tools/lib/subcmd/help.c index 8561b0f01a24..555b91ada0f9 100644 --- a/tools/lib/subcmd/help.c +++ b/tools/lib/subcmd/help.c @@ -125,13 +125,11 @@ static void get_term_dimensions(struct winsize *ws) ws->ws_col =3D 80; } =20 -static void pretty_print_string_list(struct cmdnames *cmds, int longest) +static void pretty_print_string_list(struct cmdnames *cmds, size_t longest) { - int cols =3D 1, rows; - int space =3D longest + 1; /* min 1 SP between words */ + size_t cols =3D 1, rows, max_cols; + size_t space =3D longest + 1; /* min 1 SP between words */ struct winsize win; - int max_cols; - int i, j; =20 get_term_dimensions(&win); max_cols =3D win.ws_col - 1; /* don't print *on* the edge */ @@ -140,12 +138,12 @@ static void pretty_print_string_list(struct cmdnames = *cmds, int longest) cols =3D max_cols / space; rows =3D (cmds->cnt + cols - 1) / cols; =20 - for (i =3D 0; i < rows; i++) { + for (size_t i =3D 0; i < rows; i++) { printf(" "); =20 - for (j =3D 0; j < cols; j++) { - unsigned int n =3D j * rows + i; - unsigned int size =3D space; + for (size_t j =3D 0; j < cols; j++) { + size_t n =3D j * rows + i; + int size =3D (int)space; =20 if (n >=3D cmds->cnt) break; @@ -180,7 +178,7 @@ static void list_commands_in_dir(struct cmdnames *cmds, const char *path, const char *prefix) { - int prefix_len; + size_t prefix_len; DIR *dir =3D opendir(path); struct dirent *de; char *buf =3D NULL; @@ -194,7 +192,7 @@ static void list_commands_in_dir(struct cmdnames *cmds, astrcatf(&buf, "%s/", path); =20 while ((de =3D readdir(dir)) !=3D NULL) { - int entlen; + size_t entlen; =20 if (!strstarts(de->d_name, prefix)) continue; @@ -250,10 +248,16 @@ void load_command_list(const char *prefix, exclude_cmds(other_cmds, main_cmds); } =20 +static void mput_char(char c, size_t num) +{ + while (num--) + putchar(c); +} + void list_commands(const char *title, struct cmdnames *main_cmds, struct cmdnames *other_cmds) { - unsigned int i, longest =3D 0; + size_t i, longest =3D 0; =20 for (i =3D 0; i < main_cmds->cnt; i++) if (longest < main_cmds->names[i]->len) diff --git a/tools/lib/subcmd/help.h b/tools/lib/subcmd/help.h index 355c066c8d49..258b3a143cbc 100644 --- a/tools/lib/subcmd/help.h +++ b/tools/lib/subcmd/help.h @@ -14,12 +14,6 @@ struct cmdnames { } **names; }; =20 -static inline void mput_char(char c, unsigned int num) -{ - while(num--) - putchar(c); -} - void load_command_list(const char *prefix, struct cmdnames *main_cmds, struct cmdnames *other_cmds); diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-opti= ons.c index 555d617c1f50..add46b81a131 100644 --- a/tools/lib/subcmd/parse-options.c +++ b/tools/lib/subcmd/parse-options.c @@ -201,7 +201,7 @@ static int get_value(struct parse_opt_ctx_t *p, return 0; =20 case OPTION_SET_UINT: - *(unsigned int *)opt->value =3D unset ? 0 : opt->defval; + *(unsigned int *)opt->value =3D unset ? 0 : (unsigned int)opt->defval; return 0; =20 case OPTION_SET_PTR: @@ -256,12 +256,12 @@ static int get_value(struct parse_opt_ctx_t *p, return 0; } if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { - *(int *)opt->value =3D opt->defval; + *(int *)opt->value =3D (int)opt->defval; return 0; } if (get_arg(p, opt, flags, &arg)) return -1; - *(int *)opt->value =3D strtol(arg, (char **)&s, 10); + *(int *)opt->value =3D (int)strtol(arg, (char **)&s, 10); if (*s) return opterror(opt, "expects a numerical value", flags); return 0; @@ -272,14 +272,14 @@ static int get_value(struct parse_opt_ctx_t *p, return 0; } if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { - *(unsigned int *)opt->value =3D opt->defval; + *(unsigned int *)opt->value =3D (unsigned int)opt->defval; return 0; } if (get_arg(p, opt, flags, &arg)) return -1; if (arg[0] =3D=3D '-') return opterror(opt, "expects an unsigned numerical value", flags); - *(unsigned int *)opt->value =3D strtol(arg, (char **)&s, 10); + *(unsigned int *)opt->value =3D (unsigned int)strtol(arg, (char **)&s, 1= 0); if (*s) return opterror(opt, "expects a numerical value", flags); return 0; @@ -770,9 +770,9 @@ static void print_option_help(const struct option *opts= , int full) break; } =20 - if (pos <=3D USAGE_OPTS_WIDTH) - pad =3D USAGE_OPTS_WIDTH - pos; - else { + if (pos <=3D USAGE_OPTS_WIDTH) { + pad =3D (int)(USAGE_OPTS_WIDTH - pos); + } else { fputc('\n', stderr); pad =3D USAGE_OPTS_WIDTH; } --=20 2.49.0.906.g1f30a19c02-goog