Skip to content

lib/imagery: Fix "Implicit conversion loses integer precision" warnings - #8021

Open
nilason wants to merge 1 commit into
OSGeo:mainfrom
nilason:fix_libimagery_implicit_conversion
Open

nilason wants to merge 1 commit into
OSGeo:mainfrom
nilason:fix_libimagery_implicit_conversion

Conversation

@nilason

@nilason nilason commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Adresses ” -Wshorten-64-to-32” Clang warnings.

Adresses ” -Wshorten-64-to-32” Clang warnings.
@github-actions github-actions Bot added C Related code is in C libraries imagery labels Oct 5, 2026
Comment thread lib/imagery/list_subgp.c
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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the result of strlen is not a size_t this time? I'm trying to understand the pattern (for my understanding)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 wenzeslaus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread lib/imagery/iscatt_core.c
Comment on lines +725 to 726
G_zero(b_needed_bands, n_bands * (unsigned)sizeof(int));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allocate the array with G_calloc() at line 713:

    int *b_needed_bands = G_calloc(n_bands, sizeof(int));

Then remove this call:

Suggested change
G_zero(b_needed_bands, n_bands * (unsigned)sizeof(int));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C Related code is in C imagery libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants