Clang [-Wshorten-64-to-32] warnings in htop code
This spreadsheet contains all [-Wshorten-64-to-32] warnings that I can find within htop codebase:
https://docs.google.com/spreadsheets/d/12P6VnjGBcqXA-vDI-lsBHiAsmJoruPdLBPfZ3TKNCbg/edit?usp=sharing
I didn't test all of the platforms supported in htop, but I have tried my best.
Platforms tested: Linux, FreeBSD, OpenBSD, macOS (Darwin), PCP and Unsupported Platforms not tested: NetBSD, DragonFly BSD and Solaris
There are 151 134 107 [-Wshorten-64-to-32] warnings in total in the attached spreadsheet.
This page marks all lines with the warnings.
I have also tried categorizing the warnings on the real problems behind or how to fix them. The pull requests #1588 (merged), #1668 (merged) and #1669 (merged) are parts of the solutions to these warnings.
Keywords in the Notes column:
"Cmdline"
(The PR 1588 has been merged.)
Related to byte offsets and lengths of processes' "cmdline", "comm" and "exe" path strings. The ideal solution is to promote the type of these from int to size_t. I have proposed #1588 for this.
"Terminal-width"
These variables or data have confusing uses of string length (strlen and friends) and the display width of the string (number of terminal columns the string can occupy, a la wcswidth). I cannot totally fix the problems in this category for now, as the reworking of certain functions to refer to widths instead can be difficult. I'm not sure if a quick workaround to silence the warning could be accepted. (The workaround I had in mind is using (int)strnlen(str, INT_MAX).)
"strto*"
These are about conversion of atol, strtol, strtoul, strtoull etc. results to smaller integer types. There should be bound checks (or, for fast-strto* functions, assertions on the limits).
"Hashtable-key"
Related to bit widths allowed for Hashtable keys.
"PCP-Metric"
Related to the Metric data type in the PCP code. I won't fix this one as I have little knowledge with the PCP code.
The first set of changes from #1669 has been merged. The others follow soon …
Are there any of the changes still open? AFAICS #1669 seems merged. Or were there any issues still ope?
@BenBE I intended this to be a meta-issue tracking all potentially unsafe integer downcasts. While you and @natoscott fixed the immediate -Wshorten-64-to-32 warnings in macOS, there are also similar warnings in Linux and some BSDs that we haven't addressed yet.
(That's why we cannot bump the Clang version in CI Ubuntu build jobs for now - from 18 to 21. Or, should I file a PR that bumps the Clang versions in CI and to let you see the build errors yourselves?)
| (That's why we cannot bump the Clang version in CI Ubuntu build jobs for now - from 18 to 21. Or, should I file a PR that bumps the Clang versions in CI and to let you see the build errors yourselves?)
This is a good idea IMO - its good to have visibility on the issue. We'll need to cross this bridge sooner or later, so +1 from me for updating to latest clang and exposing these warnings/errors.
As long as we don't merge that PR we can still fix the issues first before going all in …