gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master fff090c1 2/2: Makefile extensions: using fprin


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master fff090c1 2/2: Makefile extensions: using fprintf(stderr, ...) instead of error()
Date: Wed, 17 Apr 2024 10:56:06 -0400 (EDT)

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

    Makefile extensions: using fprintf(stderr,...) instead of error()
    
    Until now, the Makefile extension functions used 'error()'. But this is a
    GNU feature and within Gnuastro it is built internally by the library. As a
    Makefile extension, it is 'make' which links to these functions and it may
    have its own different implementation; producing segmentation fault (when
    trying to find this function!). This was found when the number given to
    'ast-text-prev-batch' was zero (causing a segmentation fault because of not
    being able to call 'error').
    
    With this commit, all the instances of 'error()' have been replaced with
    'fprintf(); exit(1)'. Also, a check as been added so if the number given to
    'ast-text-prev-batch' has a value of 0, Make aborts with an error message
    to inform the user.
---
 doc/gnuastro.texi |  1 +
 lib/makeplugin.c  | 52 +++++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 0acaa258..4b2a3abf 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -36024,6 +36024,7 @@ Unfortunately the @code{.NOTPARALLEL} target of GNU 
Make doesn't allow this leve
 
 @item $(ast-text-prev-batch TARGET, NUM, LIST)
 Returns the previous batch of @code{NUM} words in @code{LIST} (in relation to 
the batch containing @code{TARGET}).
+@code{NUM} will be interpreted as an unsigned integer and cannot be zero.
 If any of the arguments are an empty string (or only contain space characters 
like `@key{SPACE}', `@key{TAB}', new-line and etc), this function will return 
an empty string (having no effect in Make).
 In the special case that @code{NUM=1}, this is equivalent to the 
@code{ast-text-prev} function that is described above.
 
diff --git a/lib/makeplugin.c b/lib/makeplugin.c
index fdbe3427..abefada5 100644
--- a/lib/makeplugin.c
+++ b/lib/makeplugin.c
@@ -2,6 +2,11 @@
 Extensions to GNU Make for working with FITS files.
 This is part of GNU Astronomy Utilities (Gnuastro) package.
 
+  ------------------------------------------------------------------------
+  DO NOT USE 'error()': this library is not called by Gnuastro, but by
+  'make', which can have linked to its own separate 'error' implementation.
+  ------------------------------------------------------------------------
+
 Original author:
      Mohammad Akhlaghi <mohammad@akhlaghi.org>
 Contributing author(s):
@@ -132,8 +137,11 @@ makeplugin_version_is(const char *caller, unsigned int 
argc, char **argv)
 
   /* Write the value into the 'out' pointer. */
   if( asprintf(&out, "%d", check)<0 )
-    error(EXIT_FAILURE, 0, "%s: couldn't allocate output string",
-          __func__);
+    {
+      fprintf(stderr, "ast-version-is: %s: couldn't allocate "
+              "output string", __func__);
+      exit(1);
+    }
 
   /* Return the output string. */
   return out;
@@ -276,6 +284,15 @@ makeplugin_text_prev_batch_work(char *target, size_t 
num_in_batch,
   char *startend[4]={NULL, NULL, NULL, NULL};
   char *cp, *token, *saveptr=NULL, *out=NULL, *delimiters=" ";
 
+  /* Small sanity check. */
+  if(num_in_batch==0)
+    {
+      fprintf(stderr, "%s: a bug! Please contact us at '%s' to "
+              "find and fix the problem. The value to 'num_in_batch' is 0",
+              __func__, PACKAGE_BUGREPORT);
+      exit(1);
+    }
+
   /* Parse the line to find the desired element, but first copy the input
      list into a new editable space with 'strdupa'. */
   gal_checkset_allocate_copy(list, &cp);
@@ -380,7 +397,7 @@ static char *
 makeplugin_text_prev_batch(const char *caller, unsigned int argc,
                            char **argv)
 {
-  size_t num;
+  size_t num=0;
   void *nptr;
   char *target=argv[0], *numstr=argv[1], *list=argv[2];
 
@@ -390,8 +407,20 @@ makeplugin_text_prev_batch(const char *caller, unsigned 
int argc,
   /* Interpret the number. */
   nptr=&num;
   if( gal_type_from_string(&nptr, numstr, GAL_TYPE_SIZE_T) )
-    error(EXIT_SUCCESS, 0, "'%s' could not be read as an "
-          "unsigned integer", numstr);
+    {
+      fprintf(stderr, "ast-text-prev-batch: '%s' could not be read as "
+              "an unsigned integer", numstr);
+      exit(1);
+    }
+
+  /* In case the number is 0, return an error message. */
+  if(num==0)
+    {
+      fprintf(stderr, "ast-text-prev-batch: the given number of "
+              "elements in each batch (0) is undefined, please give a "
+              "positive integer");
+      exit(1);
+    }
 
   /* Generate the outputs.*/
   return makeplugin_text_prev_batch_work(target, num, list);
@@ -432,8 +461,11 @@ makeplugin_text_prev_batch_by_ram(const char *caller, 
unsigned int argc,
   /* Interpret the number. */
   nptr=&needed_gb;
   if( gal_type_from_string(&nptr, ramstr, GAL_TYPE_FLOAT32) )
-    error(EXIT_SUCCESS, 0, "'%s' could not be read as an "
-          "unsigned integer", ramstr);
+    {
+      fprintf(stderr, "'%s' could not be read as an unsigned "
+              "integer", ramstr);
+      exit(1);
+    }
 
   /* Estimate the number of words in each batch (to be run in parallel if
      this function is used in targets list) and call the final function. */
@@ -483,8 +515,10 @@ makeplugin_fits_check_input(char **argv, size_t numargs, 
char *name)
       if(*c=='\0')
         {
           if(i>0) /* Message only necessary for first argument. */
-            error(EXIT_SUCCESS, 0, "%s: argument %zu is empty",
-                  name, i+1);
+            {
+              fprintf(stderr, "%s: argument %zu is empty", name, i+1);
+              exit(1);
+            }
           return 1;
         }
     }



reply via email to

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