[Top][All Lists]

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

[gnuastro-commits] master 060f4e6c: Fits: --printkeynames accounts for e

From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 060f4e6c: Fits: --printkeynames accounts for empty lines before END
Date: Thu, 4 Aug 2022 04:11:16 -0400 (EDT)

branch: master
commit 060f4e6c728a9574c071b640fa84fd5a30f50036
Author: Mohammad Akhlaghi <>
Commit: Mohammad Akhlaghi <>

    Fits: --printkeynames accounts for empty lines before END
    Until now, when the '--printkeynames' option of the Fits program was run on
    a FITS file that had empty keywords before the 'END' keyword, it would
    never abort (falling into an infinite loop). For the causes of this issue,
    see the description of Commit 5f1d2c735e75d, titled "Library (fits.h):
    table metadata parsed by returned status of CFITSIO".
    With this commit, a similar fix to that commit has been implemented:
    instead of stopping the loop when we hit the 'END' keyword, we stop when
    CFITSIO returns a non-zero 'status'.
    This bug was reported by Pedram Ashofteh Ardakani.
    This fixes bug #62861.
 NEWS                |  3 +++
 bin/fits/keywords.c | 21 ++++++++++++---------
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/NEWS b/NEWS
index 83bff110..dad46f3f 100644
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,9 @@ See the end of the file for license conditions.
 ** Changed features
 ** Bugs fixed
+  bug #62861: '--printkeynames' of Fits program gets caught in an infinite
+              loop on FITS files that have empty keywords before
+              'END'. Found by Pedram Ashofteh Ardakani.
diff --git a/bin/fits/keywords.c b/bin/fits/keywords.c
index 21d00da9..ce694d92 100644
--- a/bin/fits/keywords.c
+++ b/bin/fits/keywords.c
@@ -321,16 +321,19 @@ keywords_list_key_names(struct fitsparams *p, fitsfile 
      printed. */
-  /* Go through all the keywords until you reach 'END'. */
-  while( strcmp(keyname, "END") )
-      {
-        /* Print the most recent keyword: this is placed before reading the
-           keyword because we want to stop upon reading 'END'. */
-        if( strlen(keyname) ) printf("%s\n", keyname);
+  /* Once we reach the end of the keywords, 'status' will become non-zero
+     (we shouldn't check with the 'END' keyword name because CFITSIO will
+     never reach it if there are empty keywords immediately before 'END')!
+     For more, see Gnuastro's commit 5f1d2c735e75dd000ed:
+ */
+  while(status==0)
+    {
+      /* Read the (next) keyword. */
+      fits_read_keyn(fptr, i++, keyname, value, comment, &status);
-        /* Read the next keyword. */
-        fits_read_keyn(fptr, i++, keyname, value, comment, &status);
-      }
+      /* Print the keyword name (if its not empty!). */
+      if( keyname[0]!='\0' ) printf("%s\n", keyname);
+    }

reply via email to

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