gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master ef27131 1/6: Calloc instead of malloc when rea


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master ef27131 1/6: Calloc instead of malloc when reading fits keyword comments
Date: Sat, 25 Nov 2017 11:00:59 -0500 (EST)

branch: master
commit ef271312b4523e7a0a745852c616b43556469e20
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Calloc instead of malloc when reading fits keyword comments
    
    When allocating space for a keyword `gal_fits_key_read_from_ptr' would use
    `malloc' to just allocate (and not clear/initialize) the memory. But when
    the keyword doesn't exist, the comment's space will also not be filled and
    so checking it will use an un-initialized value. So we use `calloc' now to
    keep space for the comments and units.
    
    Also, when reading a text table column information, we wouldn't check if
    the only character left is a string-null. This could happen when the values
    inside braces were like `[,,nan]'.
---
 lib/fits.c | 6 +++---
 lib/txt.c  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/fits.c b/lib/fits.c
index 96d4c76..0bb4672 100644
--- a/lib/fits.c
+++ b/lib/fits.c
@@ -986,7 +986,7 @@ gal_fits_key_read_from_ptr(fitsfile *fptr, gal_data_t 
*keysll,
         if(readcomment)
           {
             errno=0;
-            tmp->comment=malloc(FLEN_COMMENT * sizeof *tmp->comment);
+            tmp->comment=calloc(FLEN_COMMENT, sizeof *tmp->comment);
             if(tmp->comment==NULL)
               error(EXIT_FAILURE, errno, "%s: %zu bytes for tmp->comment",
                     __func__, FLEN_COMMENT * sizeof *tmp->comment);
@@ -1004,7 +1004,7 @@ gal_fits_key_read_from_ptr(fitsfile *fptr, gal_data_t 
*keysll,
           {
             /* Allocate space for the unit and read it in. */
             errno=0;
-            tmp->unit=malloc(FLEN_COMMENT * sizeof *tmp->unit);
+            tmp->unit=calloc(FLEN_COMMENT, sizeof *tmp->unit);
             if(tmp->unit==NULL)
               error(EXIT_FAILURE, errno, "%s: %zu bytes for tmp->unit",
                     __func__, FLEN_COMMENT * sizeof *tmp->unit);
@@ -1016,7 +1016,7 @@ gal_fits_key_read_from_ptr(fitsfile *fptr, gal_data_t 
*keysll,
         else
           tmp->unit=NULL;
 
-        /* Read the keyword and place its value in the poitner. */
+        /* Read the keyword and place its value in the pointer. */
         fits_read_key(fptr, gal_fits_type_to_datatype(tmp->type),
                       tmp->name, valueptr, tmp->comment, &tmp->status);
 
diff --git a/lib/txt.c b/lib/txt.c
index 5fb6eab..0f7fd90 100644
--- a/lib/txt.c
+++ b/lib/txt.c
@@ -237,7 +237,7 @@ txt_info_from_comment(char *line, gal_data_t **datall, char 
*comm_start)
          are dealing with the string type, we have to pull out the number
          part first. If there is no number for a string type, then ignore
          the line. */
-      if(typestr)
+      if(typestr && *typestr!='\0')
         {
           typestr=txt_trim_space(typestr);
           if( !strncmp(typestr, "str", 3) )



reply via email to

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