gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 5dd3ff1: wcsdistortion.h library: fixed compil


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 5dd3ff1: wcsdistortion.h library: fixed compiler warnings
Date: Mon, 16 Nov 2020 20:07:19 -0500 (EST)

branch: master
commit 5dd3ff1af0411848ea964e4d85366e7863ee028f
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    wcsdistortion.h library: fixed compiler warnings
    
    Until now, some of the FITS header keywords that were read/parsed were
    mistakenly reading/writing numbers with 'size_t' type as '%ld'. We hadn't
    noticed this until now, because on 64-bit systems, 'size_t' is an unsigned
    long type so the compiler wouldn't complain. However, on some 32-bit
    systems, 'size_t' translates to unsigned integer, and this caused compiler
    warnings.
    
    With this commit, the general printing format for 'size_t' ('%zu') is used
    to fix that problem. According to the C99 standard, %zu will evaluate to
    integer or long based on the host system and should save us from having to
    add checks.
    
    Also, after making this correction, I noticed that GCC is complaining about
    writing 'size_t' values into the very limited (8 or 9 byte) literal string!
    So I just expanded the space in each literal string to 50 bytes instead,
    which fixed the warnings. In practice only or 8 or 9 bytes will be needed
    (since we never have more than 10 or 20 distortion coefficients, and any
    number below 20 are at maximum two characters/bytes). But well, the "smart"
    compilers don't know this, so they also worry about not being able to write
    the largest possible 'size_t' value (18446744073709551615, which is 20
    characters/bytes) and print warnings!
    
    This issue was reported by Francois Ochsenbein.
---
 THANKS                       |  1 +
 bin/arithmetic/arithmetic.c  |  2 +-
 doc/announce-acknowledge.txt |  1 +
 lib/wcsdistortion.c          | 44 ++++++++++++++++++++++++++++----------------
 4 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/THANKS b/THANKS
index d3dacc6..59eac9b 100644
--- a/THANKS
+++ b/THANKS
@@ -71,6 +71,7 @@ support in Gnuastro. The list is ordered alphabetically (by 
family name).
     Bob Proulx                           bob@proulx.com
     Joseph Putko                         josephputko@gmail.com
     Samane Raji                          samaneraji@gmail.com
+    Francois Ochsenbein                  francois.ochsenbein@gmail.com
     Teymoor Saifollahi                   teymur.saif@gmail.com
     Joanna Sakowska                      js01093@surrey.ac.uk
     Elham Saremi                         saremi@ipm.ir
diff --git a/bin/arithmetic/arithmetic.c b/bin/arithmetic/arithmetic.c
index 02b698e..1277153 100644
--- a/bin/arithmetic/arithmetic.c
+++ b/bin/arithmetic/arithmetic.c
@@ -930,7 +930,7 @@ arithmetic_collapse(struct arithmeticparams *p, char 
*token, int operator)
           "is %ld", dim);
   if(dim > input->ndim)
     error(EXIT_FAILURE, 0, "input dataset to '%s' has %zu dimension(s), "
-          "but you have asked to collapse along dimension %zu", token,
+          "but you have asked to collapse along dimension %ld", token,
           input->ndim, dim);
 
 
diff --git a/doc/announce-acknowledge.txt b/doc/announce-acknowledge.txt
index 16c9ed7..225028d 100644
--- a/doc/announce-acknowledge.txt
+++ b/doc/announce-acknowledge.txt
@@ -3,6 +3,7 @@ Alphabetically ordered list to acknowledge in the next release.
 Martin Kuemmel
 Sebastian Luna Valero
 Samane Raji
+Francois Ochsenbein
 Joanna Sakowska
 Zahra Sharbaf
 Sachin Kumar Singh
diff --git a/lib/wcsdistortion.c b/lib/wcsdistortion.c
index 31212f7..5b0b285 100644
--- a/lib/wcsdistortion.c
+++ b/lib/wcsdistortion.c
@@ -116,7 +116,7 @@ wcsdistortion_get_tpvparams(struct wcsprm *wcs, double 
cd[2][2],
           if (strncmp(cp, "TPV.", 4) != 0) continue;
           cp += 4;
 
-          sscanf(cp, "%ld", &index);
+          sscanf(cp, "%zu", &index);
           pv1[index]=disseq->dp[i].value.f;
 
           /* For a check
@@ -134,7 +134,7 @@ wcsdistortion_get_tpvparams(struct wcsprm *wcs, double 
cd[2][2],
           if (strncmp(cp, "TPV.", 4) != 0) continue;
           cp += 4;
 
-          sscanf(cp, "%ld", &index);
+          sscanf(cp, "%zu", &index);
 
           pv2[index]=disseq->dp[i].value.f;
 
@@ -214,7 +214,7 @@ wcsdistortion_get_sipparams(struct wcsprm *wcs, double 
cd[2][2],
           if (strncmp(cp, "SIP.FWD.", 8) != 0) continue;
           cp += 8;
 
-          sscanf(cp, "%ld_%ld", &m, &n);
+          sscanf(cp, "%zu_%zu", &m, &n);
           a_coeff[m][n]=dispre->dp[i].value.f;
 
           /*For a check.
@@ -232,7 +232,7 @@ wcsdistortion_get_sipparams(struct wcsprm *wcs, double 
cd[2][2],
           if (strncmp(cp, "SIP.FWD.", 8) != 0) continue;
           cp += 8;
 
-          sscanf(cp, "%ld_%ld", &m, &n);
+          sscanf(cp, "%zu_%zu", &m, &n);
 
           b_coeff[m][n]=dispre->dp[i].value.f;
 
@@ -1446,7 +1446,13 @@ wcsdistortion_add_sipkeywords(struct wcsprm *wcs, size_t 
*fitsize,
   size_t ap_order=0, bp_order=0;
   size_t m, n, num=0, numkey=200;
   double ap_coeff[5][5], bp_coeff[5][5];
-  char *fullheader, fmt[50], sipkey[8], keyaxis[9], pcaxis[10];
+
+  /* The literal strings are a little longer than necessary because GCC
+     will complain/warn that size_t can be very long. Although it will
+     never happen that we need to write 10-decimal numbers in these, but to
+     have a clean build, we'll just set the literal space to be large
+     enough to avoid the warnings. */
+  char *fullheader, fmt[50], sipkey[50], keyaxis[50], pcaxis[50];
 
   /* Initialise the 2d matrices. */
   *nkeys = 0;
@@ -1532,7 +1538,7 @@ wcsdistortion_add_sipkeywords(struct wcsprm *wcs, size_t 
*fitsize,
         if(val != 0)
           {
             /* Make keywords */
-            sprintf(sipkey, "A_%ld_%ld", m, n);
+            sprintf(sipkey, "A_%zu_%zu", m, n);
             sprintf(fullheader+(FLEN_CARD-1)*num++, fmt, sipkey, val, "");
             a_order=wcsdistortion_max(a_order, wcsdistortion_max(m,n));
           }
@@ -1542,16 +1548,16 @@ wcsdistortion_add_sipkeywords(struct wcsprm *wcs, 
size_t *fitsize,
         if(val != 0)
           {
             /* Make keywords */
-            sprintf(sipkey, "B_%ld_%ld", m, n);
+            sprintf(sipkey, "B_%zu_%zu", m, n);
             sprintf(fullheader+(FLEN_CARD-1)*num++, fmt, sipkey, val, "");
             b_order=wcsdistortion_max(b_order, wcsdistortion_max(m,n));
           }
 
       }
 
-  sprintf(fullheader+(FLEN_CARD-1)*num++, "%-8s= %20ld%50s", "A_ORDER",
+  sprintf(fullheader+(FLEN_CARD-1)*num++, "%-8s= %20zu%50s", "A_ORDER",
           a_order, "");
-  sprintf(fullheader+(FLEN_CARD-1)*num++, "%-8s= %20ld%50s", "B_ORDER",
+  sprintf(fullheader+(FLEN_CARD-1)*num++, "%-8s= %20zu%50s", "B_ORDER",
           b_order, "");
 
   /* If reverse coefficients are required. */
@@ -1570,7 +1576,7 @@ wcsdistortion_add_sipkeywords(struct wcsprm *wcs, size_t 
*fitsize,
             if(val != 0)
               {
                 /* Make keywords */
-                sprintf(sipkey, "AP_%ld_%ld", m, n);
+                sprintf(sipkey, "AP_%zu_%zu", m, n);
                 sprintf(fullheader+(FLEN_CARD-1)*num++, fmt, sipkey,
                         val, "");
               }
@@ -1580,15 +1586,15 @@ wcsdistortion_add_sipkeywords(struct wcsprm *wcs, 
size_t *fitsize,
             if(val != 0)
               {
                 /* Make keywords */
-                sprintf(sipkey, "BP_%ld_%ld", m, n);
+                sprintf(sipkey, "BP_%zu_%zu", m, n);
                 sprintf(fullheader+(FLEN_CARD-1)*num++, fmt, sipkey,
                         val, "");
               }
           }
 
-      sprintf(fullheader+(FLEN_CARD-1)*num++, "%-8s= %20ld%50s", "AP_ORDER",
+      sprintf(fullheader+(FLEN_CARD-1)*num++, "%-8s= %20zu%50s", "AP_ORDER",
               ap_order, "");
-      sprintf(fullheader+(FLEN_CARD-1)*num++, "%-8s= %20ld%50s", "BP_ORDER",
+      sprintf(fullheader+(FLEN_CARD-1)*num++, "%-8s= %20zu%50s", "BP_ORDER",
               bp_order, "");
 
     }
@@ -1618,7 +1624,13 @@ wcsdistortion_add_pvkeywords(struct wcsprm *wcs, double 
*pv1,
   uint8_t i, j, k=0;
   int size = wcs->naxis;
   size_t m, n, num=0, numkey=100;
-  char *fullheader, fmt[50], pvkey[8], keyaxis[9], pcaxis[10];
+
+  /* The literal strings are a little longer than necessary because GCC
+     will complain/warn that size_t can be very long. Although it will
+     never happen that we need to write 10-decimal numbers in these, but to
+     have a clean build, we'll just set the literal space to be large
+     enough to avoid the warnings. */
+  char *fullheader, fmt[50], pvkey[50], keyaxis[50], pcaxis[50];
 
   /* Initialize values. */
   *nkeys = 0;
@@ -1704,7 +1716,7 @@ wcsdistortion_add_pvkeywords(struct wcsprm *wcs, double 
*pv1,
             if(val != 0)
               {
                 /* Make keywords */
-                sprintf(pvkey, "PV%ld_%ld", m, n);
+                sprintf(pvkey, "PV%zu_%zu", m, n);
                 sprintf(fullheader+(FLEN_CARD-1)*num++, fmt, pvkey,
                         val, "");
               }
@@ -1717,7 +1729,7 @@ wcsdistortion_add_pvkeywords(struct wcsprm *wcs, double 
*pv1,
             if(val != 0)
               {
                 /* Make keywords */
-                sprintf(pvkey, "PV%ld_%ld", m, n);
+                sprintf(pvkey, "PV%zu_%zu", m, n);
                 sprintf(fullheader+(FLEN_CARD-1)*num++, fmt, pvkey,
                         val, "");
               }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]