gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 6836e7d 2/2: Correct size for output columns i


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 6836e7d 2/2: Correct size for output columns in Table
Date: Fri, 21 Oct 2016 21:07:30 +0000 (UTC)

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

    Correct size for output columns in Table
    
    The `data' element of Table's `outcolumn' structure (in `bin/table/main.h')
    has a `void *' type to allow various types. However, to allocate space for
    the output column we were using the `sizeof' operator on it! This would
    give a size of 1 and would cause a crash some systems.
    
    In the previous commit (5c41564: New function to return the size of FITS
    datatype), a new function was added to the Gnuastro FITS functions to
    return the number of bytes in each datatype. With that function, the
    datatype's size is now also stored in the `outcolumn' structure and the
    allocation is done based on that.
    
    This fixes bug #49347.
---
 bin/table/main.h  |    1 +
 bin/table/table.c |    8 ++++----
 bin/table/ui.c    |    9 +++++++--
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/bin/table/main.h b/bin/table/main.h
index 3cba9bf..bf6d7e9 100644
--- a/bin/table/main.h
+++ b/bin/table/main.h
@@ -44,6 +44,7 @@ struct outcolumn
   int     anynul;               /* If there is any blank characters.    */
   void   *nulval;               /* The blank value for this column.     */
   void     *data;               /* Array keeping the column data.       */
+  size_t   esize;               /* Size of each element in this column. */
   char fmt[MAX_COL_FORMAT_LENGTH];  /* format to use in printf.         */
 };
 
diff --git a/bin/table/table.c b/bin/table/table.c
index 0678de7..c95521f 100644
--- a/bin/table/table.c
+++ b/bin/table/table.c
@@ -141,7 +141,7 @@ setformatstring(struct tableparams *p, size_t outcolid)
 
     default:
       error(EXIT_FAILURE, 0, "datatype value of %d not recognized in "
-            "gal_fits_datatype_alloc", ocol->datatype);
+            "setformatstring (table.c)", ocol->datatype);
     }
 
   /* Put the type, width and accu into the format string for this
@@ -194,11 +194,11 @@ readinputcols(struct tableparams *p)
              data. So we allocate an array to only put this column's values
              in.*/
           errno=0;
-          colfromtxt=col->data=malloc(nrows * sizeof *col->data);
-
+          colfromtxt=col->data=malloc(nrows * col->esize);
           if(col->data==NULL)
             error(EXIT_FAILURE, errno, "%zu bytes for col->data",
-                  nrows * sizeof *col->data);
+                  nrows * col->esize);
+
           for(j=0;j<nrows;++j)
             colfromtxt[j]=p->up.txtarray[ j * incols + col->inindex ];
         }
diff --git a/bin/table/ui.c b/bin/table/ui.c
index 72b4262..55937d4 100644
--- a/bin/table/ui.c
+++ b/bin/table/ui.c
@@ -872,10 +872,13 @@ outputcolumns(struct tableparams *p)
   while(colsll)
     {
       gal_linkedlist_pop_from_sll(&colsll, &inindex);
-      p->ocols[i].datatype=up->datatype[inindex];
       p->ocols[i].inindex=inindex;
+      p->ocols[i].datatype=up->datatype[inindex];
+      p->ocols[i].esize=gal_fits_datatype_size(up->datatype[inindex]);
+      printf("\n%zu\n", p->ocols[i].esize);
       --i;
     }
+  exit(1);
 }
 
 
@@ -905,8 +908,9 @@ preparearrays(struct tableparams *p)
               p->nocols * sizeof *p->ocols);
       for(i=0;i<p->nocols;++i)
         {
-          p->ocols[i].datatype=up->datatype[i];
           p->ocols[i].inindex=i;
+          p->ocols[i].datatype=up->datatype[i];
+          p->ocols[i].esize=gal_fits_datatype_size(p->ocols[i].datatype);
         }
     }
 }
@@ -1006,6 +1010,7 @@ freeandreport(struct tableparams *p)
 
   /* Free the allocated arrays: */
   free(p->cp.hdu);
+  free(up->txtarray);
   free(up->datatype);
   free(p->cp.output);
 



reply via email to

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