gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master bb9938fd: Library (arithmetic.c): ra-to-degree


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master bb9938fd: Library (arithmetic.c): ra-to-degree checks for non-string inputs
Date: Tue, 5 Apr 2022 21:00:47 -0400 (EDT)

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

    Library (arithmetic.c): ra-to-degree checks for non-string inputs
    
    Until now, the two 'ra-to-degree' or 'deg-to-degree' operators didn't do a
    sanity check on the types of their inputs. As a result, they would give a
    segmentation fault when not properly used (which is very bad and can cause
    a lot of confusion for the user). This happened in the following two
    scenarios:
    
        astarithmetic 16 ra-to-degree
        echo 16 | asttable -c'arith $1 ra-to-degree'
        echo 16h0m0 | asttable -c'arith $1 ra-to-degree'
    
    In the third case, its because table automatically converts sexagesimal
    formats to degrees in floating point (default type) columns.
    
    With this commit, before this conversion is done with these operators, the
    program will check if the input is string or not. If not, it will abort the
    program and print an informative message on the cause and solution, to help
    the user understand and fix the problem.
    
    This bug was reported by Pedram Ashofte Ardakani.
    
    This fixes bug #62253.
---
 NEWS             |  3 +++
 lib/arithmetic.c | 38 +++++++++++++++++++++++++++++++++-----
 2 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/NEWS b/NEWS
index 9f3bc501..5ea15b62 100644
--- a/NEWS
+++ b/NEWS
@@ -20,6 +20,9 @@ See the end of the file for license conditions.
               Raul Infante-Sainz.
   bug #62252: Radial profile giving one larger radius in high axis-ratio
               profile. Found and fixed by Sepideh Eskandarlou.
+  bug #62253: Arithmetic: segmentation fault when invalid input given for
+              'ra-to-degree' or 'deg-to-degree' operators. Found by Pedram
+              Ashofte Ardakani.
 
 
 
diff --git a/lib/arithmetic.c b/lib/arithmetic.c
index 2285fe7d..1b353853 100644
--- a/lib/arithmetic.c
+++ b/lib/arithmetic.c
@@ -502,10 +502,38 @@ arithmetic_units_degree_to_dec(double decimal)
             "UNIARY_FUNCTION_ON_ELEMENT", in->type);                    \
     }
 
-
-#define UNIFUNC_RUN_FUNCTION_ON_ELEMENT_STRING(OT, OP){                 \
+#define UNIFUNC_RUN_FUNCTION_ON_ELEMENT_SEXAGESIMAL(OT, OP){            \
     OT *oa=o->array;                                                    \
-    char **ia=in->array, **iaf=ia + in->size;                           \
+    char **ia, **iaf;                                                   \
+    /* If the input dataset isn't a string (it was parsed as a */       \
+    /* number, stop the program and let the user know what to do. */    \
+    /* This can happen with the Arithmetic program, for example if */   \
+    /* this command is called: 'astarithmetic 16 ra-to-deg'. */         \
+    if(in->type!=GAL_TYPE_STRING)                                       \
+      error(EXIT_FAILURE, 0, "%s: the input should be a string in "     \
+            "the format of %s (where the '_' are place-holders for "    \
+            "numbers). This error may happen when you are calling a "   \
+            "command like \"astarithmetic 16 %s\" or \"echo %s | "      \
+            "asttable -c'arith $1 %s'\". In such cases, please use "    \
+            "this command: "                                            \
+            "\"echo %s | asttable\" (by default, when a string type "   \
+            "isn't specified for a column, and it conforms to the "     \
+            "pattern above, 'asttable' does the conversion "            \
+            "internally; this is the cause of the second type of "      \
+            "error above). The '%s' operator is only relevant for "     \
+            "many sexagesimal values in a string-type table column",    \
+            gal_arithmetic_operator_string(operator),                   \
+            ( operator==GAL_ARITHMETIC_OP_RA_TO_DEGREE                  \
+              ? "'_h_m_s' or '_h_m_'"                                   \
+              : "'_d_m_s' or '_d_m_'" ),                                \
+            gal_arithmetic_operator_string(operator),                   \
+            ( operator==GAL_ARITHMETIC_OP_RA_TO_DEGREE                  \
+              ? "16h0m0" : "16d0m0" ),                                  \
+            gal_arithmetic_operator_string(operator),                   \
+            ( operator==GAL_ARITHMETIC_OP_RA_TO_DEGREE                  \
+              ? "16h0m0" : "16d0m0" ),                                  \
+            gal_arithmetic_operator_string(operator));                  \
+    iaf = (ia=in->array) + in->size;                                    \
     do *oa++ = OP(*ia++); while(ia<iaf);                                \
 }
 
@@ -605,10 +633,10 @@ arithmetic_function_unary(int operator, int flags, 
gal_data_t *in)
     case GAL_ARITHMETIC_OP_AU_TO_LY:
       UNIARY_FUNCTION_ON_ELEMENT( gal_units_au_to_ly, +0, +0); break;
     case GAL_ARITHMETIC_OP_RA_TO_DEGREE:
-      UNIFUNC_RUN_FUNCTION_ON_ELEMENT_STRING(double, gal_units_ra_to_degree);
+      UNIFUNC_RUN_FUNCTION_ON_ELEMENT_SEXAGESIMAL(double, 
gal_units_ra_to_degree);
       break;
     case GAL_ARITHMETIC_OP_DEC_TO_DEGREE:
-      UNIFUNC_RUN_FUNCTION_ON_ELEMENT_STRING(double, gal_units_dec_to_degree);
+      UNIFUNC_RUN_FUNCTION_ON_ELEMENT_SEXAGESIMAL(double, 
gal_units_dec_to_degree);
       break;
     case GAL_ARITHMETIC_OP_DEGREE_TO_RA:
       UNIARY_FUNCTION_ON_ELEMENT_OUTPUT_STRING(arithmetic_units_degree_to_ra);



reply via email to

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