Repository navigation
Conversation
Adresses ” -Wshorten-64-to-32” Clang warnings.
| for (i = 0; i < ref->nfiles; i++) { | ||
| I__list_group_name_fit(buf, ref->file[i].name, ref->file[i].mapset); | ||
| len = strlen(buf) + 4; | ||
| len = (int)strlen(buf) + 4; |
There was a problem hiding this comment.
Why is the result of strlen is not a size_t this time? I'm trying to understand the pattern (for my understanding)
There was a problem hiding this comment.
buf is of size 80 (char buf[80]), so casting is absolutely safe here. size_t is unsigned, so for avoiding further issues like "comparison between signed and unsigned" (if I were to change len to size_t), I stay with the cast here.
So the pattern is: if I can change type without cascading issues and minimal overall changes, that is my preferred solution, otherwise I look for an alternative, locally adapted solution.
wenzeslaus
left a comment
There was a problem hiding this comment.
The (unsigned) cast in I_compute_scatts() does not make the conversion explicit, it only changes it, because the product is still converted to the int parameter of G_zero(). Since the array is zeroed right after it is allocated, I suggest allocating it with G_calloc(), which takes size_t, and removing the G_zero() call. The other changes already follow the same rule as in #8019, with lengths staying size_t and plain casts only where the value is bounded, except for the unused I_location_info(). The details below and the inline suggestions are AI generated.
Details (AI generated)
G_zero(void *, int) passes its length to memset(). On main, the call is G_zero(b_needed_bands, (size_t)n_bands * sizeof(int)), which -Wshorten-64-to-32 reports as size_t to int. In the PR, n_bands * (unsigned)sizeof(int) is unsigned int, so the same conversion to int remains, now as a sign conversion that -Wshorten-64-to-32 does not report (-Wsign-conversion reports it twice on that line). Nothing writes to b_needed_bands between G_malloc() and G_zero(), so G_calloc(n_bands, sizeof(int)) gives the same zeroed array with size_t throughout.
All changes of the PR by the rule from #8019:
| Place | PR | Result |
|---|---|---|
sig.c, sigset.c total |
unsigned int -> size_t |
ok, stays size_t into G_realloc() |
manage_signatures.c mapset_len |
int -> size_t |
ok |
iscatt_core.c n_pixs |
int -> size_t |
ok, compared with the fread() count |
list_gp.c, list_subgp.c len |
(int)strlen(buf) |
ok, bounded by char buf[80] |
iscatt_core.c G_zero() |
(unsigned)sizeof(int) |
G_calloc(), no conversion |
loc_info.c len |
int -> size_t |
wraps around for text longer than 79 characters, but I_location_info() has no callers in the OSGeo repositories, so like HTML_Text() in #8023 it is left as is |
The suggested code was applied to iscatt_core.c of this PR and compiled with clang 18 using -Wshorten-64-to-32 -Wconversion -Wsign-conversion. No new warnings appear, and the two sign-conversion warnings on the G_zero() line are gone.
| G_zero(b_needed_bands, n_bands * (unsigned)sizeof(int)); | ||
|
|
There was a problem hiding this comment.
Allocate the array with G_calloc() at line 713:
int *b_needed_bands = G_calloc(n_bands, sizeof(int));Then remove this call:
| G_zero(b_needed_bands, n_bands * (unsigned)sizeof(int)); |
Adresses ” -Wshorten-64-to-32” Clang warnings.