This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
locale.c: Change type of a variable to silence compiler warning
authorTAKAI Kousuke <62541129+t-a-k@users.noreply.github.com>
Tue, 21 Nov 2023 17:26:32 +0000 (02:26 +0900)
committerTony Cook <tony@develop-help.com>
Tue, 28 Nov 2023 04:45:10 +0000 (15:45 +1100)
The variable `already_checked` in Perl_init_i18nl10n() never become
negative, and never exceed `C_trial` which is a small positive integer.
Thus `unsigned int` should be enough.

This fixes build warning on 32-bit target
(where sizeof(unsigned int) == sizeof(SSize_t)):
    locale.c: In function ‘Perl_init_i18nl10n’:
    locale.c:7370:40: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘ssize_t’ {aka ‘int’} [-Wsign-compare]
     7370 |             for (unsigned int i = 0; i < already_checked; i++) {
          |                                        ^

locale.c

index 4e3b5b3..af4c399 100644 (file)
--- a/locale.c
+++ b/locale.c
@@ -7288,7 +7288,7 @@ Perl_init_i18nl10n(pTHX_ int printwarn)
     } trials;
 
     trials trial;
-    SSize_t already_checked = 0;
+    unsigned int already_checked = 0;
     const char * checked[C_trial];
 
 #  ifdef LC_ALL
@@ -7374,6 +7374,7 @@ Perl_init_i18nl10n(pTHX_ int printwarn)
             }
 
             /* And, for future iterations, indicate we've tried this locale */
+            assert(already_checked < C_ARRAY_LENGTH(checked));
             checked[already_checked] = savepv(locale);
             SAVEFREEPV(checked[already_checked]);
             already_checked++;