]> granicus.if.org Git - fcron/commitdiff
Replaced function free_safe by macro Free_safe, and moved memory management functions...
authorThibault Godouet <fcron@free.fr>
Wed, 21 Nov 2012 22:16:25 +0000 (22:16 +0000)
committerThibault Godouet <fcron@free.fr>
Wed, 21 Nov 2012 22:16:25 +0000 (22:16 +0000)
free_safe() was broken, as the pointer was not set to NULL outside of free_safe(). Simplest/cleanest way to fix this was to replace it by a macro doing the same. Now the pointers are correctly set to NULL, and things are actually safe.

24 files changed:
Makefile.in
conf.c
convert-fcrontab.c
database.c
env_list.c
fcron.c
fcron.h
fcronconf.c
fcrondyn.c
fcronsighup.h
fcrontab.h
fifo_list.c
fileconf.c
global.h
job.c
log.c
mem.c [new file with mode: 0644]
mem.h [new file with mode: 0644]
read_string.c
socket.c
subs.c
subs.h
temp_file.c
u_list.c

index b18ec417f90bf5efad85479385e2c63eeb3ad4e0..1f906ddbf701014a1a8002c973bc4479f1517237 100644 (file)
@@ -74,12 +74,12 @@ CFLAGS += $(OPTIM) $(OPTION) $(DEFS) $(CPPFLAGS)
 ifeq ($(FCRONDYN), 1)
 LIBOBJS := socket.o $(LIBOBJS)
 endif
-OBJSD := fcron.o subs.o save.o temp_file.o log.o database.o job.o conf.o u_list.o exe_list.o lavg_list.o env_list.o fcronconf.o $(LIBOBJS)
-OBJSTAB := fcrontab.o subs.o save.o temp_file.o  log.o fileconf.o allow.o read_string.o u_list.o env_list.o fcronconf.o
-OBJSDYN := fcrondyn.o subs.o log.o allow.o read_string.o fcronconf.o
-OBJCONV := convert-fcrontab.o subs.o save.o log.o u_list.o env_list.o
-OBJSIG := fcronsighup.o subs.o log.o allow.o fcronconf.o
-HEADERSALL := config.h $(SRCDIR)/global.h $(SRCDIR)/log.h $(SRCDIR)/subs.h $(SRCDIR)/save.h $(SRCDIR)/option.h $(SRCDIR)/dyncom.h
+OBJSD := fcron.o subs.o mem.o save.o temp_file.o log.o database.o job.o conf.o u_list.o exe_list.o lavg_list.o env_list.o fcronconf.o $(LIBOBJS)
+OBJSTAB := fcrontab.o subs.o mem.o save.o temp_file.o  log.o fileconf.o allow.o read_string.o u_list.o env_list.o fcronconf.o
+OBJSDYN := fcrondyn.o subs.o mem.o log.o allow.o read_string.o fcronconf.o
+OBJCONV := convert-fcrontab.o subs.o mem.o save.o log.o u_list.o env_list.o
+OBJSIG := fcronsighup.o subs.o mem.o log.o allow.o fcronconf.o
+HEADERSALL := config.h $(SRCDIR)/global.h $(SRCDIR)/log.h $(SRCDIR)/subs.h $(SRCDIR)/mem.h $(SRCDIR)/save.h $(SRCDIR)/option.h $(SRCDIR)/dyncom.h
 
 # this is a regular expression :
 # do not ci automaticaly generated files and doc (done by doc's Makefile)
diff --git a/conf.c b/conf.c
index c45b4491a317a370fa174095ee8333680a106e9c..019aa71ada6d803fbcde83c6f1b6a20687482720 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -181,22 +181,22 @@ synchronize_dir(const char *dir_name, int is_system_startup)
        next = rm_list;
        while( (l = next) != NULL ) {
            next = l->next;
-           free_safe(l->str);
-           free_safe(l);
+           Free_safe(l->str);
+           Free_safe(l);
        }
            
        next = new_list;
        while( (l = next) != NULL ) {
            next = l->next;
-           free_safe(l->str);
-           free_safe(l);
+           Free_safe(l->str);
+           Free_safe(l);
        }
 
        next = file_list;
        while( (l = next) != NULL ) {
            next = l->next;
-           free_safe(l->str);
-           free_safe(l);
+           Free_safe(l->str);
+           Free_safe(l);
        }
 
     }
@@ -394,8 +394,7 @@ read_strn(int fd, char **str, short int size)
     return OK;
 
   err:
-    if (*str)
-       free_safe(*str);
+    Free_safe(*str);
     return ERR;
     
 }
@@ -562,7 +561,7 @@ read_file(const char *file_name, cf_t *cf, int is_system_startup)
     }
     /* get the owner's name */
     /* we set cf->cf_user before for SE Linux, so we need to free it here */
-    free_safe(cf->cf_user);
+    Free_safe(cf->cf_user);
     if ( read_strn(fileno(ff), &cf->cf_user, size) != OK ) {
        error("Cannot read user's name : file ignored");
        goto err;
@@ -571,7 +570,7 @@ read_file(const char *file_name, cf_t *cf, int is_system_startup)
        /* we use file owner's name for more security (see above) */
        /* free the value obtained by read_strn() (we need to read it anyway
         * to set the file ptr to the next thing to read) */
-       free_safe(cf->cf_user);
+       Free_safe(cf->cf_user);
        cf->cf_user = runas_str;
     } 
 
@@ -610,7 +609,7 @@ read_file(const char *file_name, cf_t *cf, int is_system_startup)
                 else {
                     env_list_putenv(cf->cf_env_list, envvar, 1);
                 }
-                free_safe(envvar);
+                Free_safe(envvar);
             }
            break;
 
@@ -742,7 +741,7 @@ read_file(const char *file_name, cf_t *cf, int is_system_startup)
     }
 
     /* free last cl Alloc : unused */
-    free_safe(cl);
+    Free_safe(cl);
 
     /* check for an error */
     if ( ferror(ff) != 0 )
@@ -759,10 +758,10 @@ read_file(const char *file_name, cf_t *cf, int is_system_startup)
 
     if ( cl != NULL && cl->cl_next == NULL ) {
        /* line is not yet in the line list of the file : free it */
-       if ( cl->cl_shell ) free_safe(cl->cl_shell);
-       if ( cl->cl_runas) free_safe(cl->cl_runas);
-       if ( cl->cl_mailto) free_safe(cl->cl_mailto);
-       free_safe(cl);
+       Free_safe(cl->cl_shell);
+       Free_safe(cl->cl_runas);
+       Free_safe(cl->cl_mailto);
+       Free_safe(cl);
     }
 
     /* check if we have started to read the lines and env var */
@@ -776,8 +775,9 @@ read_file(const char *file_name, cf_t *cf, int is_system_startup)
        delete_file(cf->cf_user);
        
     }
-    else if (cf->cf_user != NULL)
-       free_safe(cf->cf_user);
+    else {
+       Free_safe(cf->cf_user);
+    }
 
     return ERR;
 
@@ -996,11 +996,11 @@ free_line(cl_t *cl)
     /* free a line, including its fields */
 {
     if (cl != NULL) {
-        free_safe(cl->cl_shell);
-        free_safe(cl->cl_runas);
-        free_safe(cl->cl_mailto);
-        free_safe(cl->cl_tz);
-        free_safe(cl);
+        Free_safe(cl->cl_shell);
+        Free_safe(cl->cl_runas);
+        Free_safe(cl->cl_mailto);
+        Free_safe(cl->cl_tz);
+        Free_safe(cl);
     }
 }
 
@@ -1082,7 +1082,7 @@ delete_file(const char *user_name)
                    != NULL)
                    k++;
        }
-       free_safe(serial_array);
+       Free_safe(serial_array);
        serial_array = s_a;
        serial_array_index = 0;
 
@@ -1101,7 +1101,7 @@ delete_file(const char *user_name)
                        prev_j->j_next = j->j_next;
                    else
                        queue_base = j->j_next;
-                   free_safe(j);
+                   Free_safe(j);
                    break;
                }
                else
@@ -1129,8 +1129,8 @@ delete_file(const char *user_name)
     env_list_destroy(file->cf_env_list);
 
     /* finally free file itself */
-    free_safe(file->cf_user);
-    free_safe(file);
+    Free_safe(file->cf_user);
+    Free_safe(file);
 
 }
 
index 7fae425be8f7f2d9d58758b5b468d1fda5f95697..9e04d9e733005929983c6b3839c650070989296a 100644 (file)
@@ -25,6 +25,7 @@
  /* $Id: convert-fcrontab.c,v 1.23 2007-04-14 18:04:21 thib Exp $ */
 
 #include "convert-fcrontab.h"
+#include "mem.h"
 
 char rcs_info[] = "$Id: convert-fcrontab.c,v 1.23 2007-04-14 18:04:21 thib Exp $";
 
@@ -119,18 +120,18 @@ delete_file(cf_t *file)
     cur_line = file->cf_line_base;
     while ( (line = cur_line) != NULL) {
        cur_line = line->cl_next;
-       free_safe(line->cl_shell);
-       free_safe(line->cl_mailto);
-       free_safe(line->cl_runas);
-       free_safe(line);
+       Free_safe(line->cl_shell);
+       Free_safe(line->cl_mailto);
+       Free_safe(line->cl_runas);
+       Free_safe(line);
     }
 
     /* free env variables */
     env_list_destroy(file->cf_env_list);
 
     /* finally free file itself */
-    free_safe(file->cf_user);
-    free_safe(file);
+    Free_safe(file->cf_user);
+    Free_safe(file);
 
 }
 
@@ -182,7 +183,7 @@ convert_file(char *file_name)
     /* read env variables */
     while( (env = read_str(f, buf, sizeof(buf))) != NULL ) {
         env_list_putenv(file->cf_env_list, env, 1);
-        free_safe(env);
+        Free_safe(env);
     }
 
     /* read lines */
@@ -208,7 +209,7 @@ convert_file(char *file_name)
 
     }
 
-    free_safe(line);
+    Free_safe(line);
 
     fclose(f);
 
index 55ce409e46bd6dc52ba2b33fdc883ddf5954590b..4008cae5d570bcdd31b66e65dcc93fcd2e828f68 100644 (file)
@@ -269,7 +269,7 @@ job_queue_remove(cl_t *line)
                 /* first element of the list */
                 queue_base = j->j_next;
 
-            free_safe(j);
+            Free_safe(j);
             return jprev;
         }
     }
@@ -362,7 +362,7 @@ add_serial_job(cl_t *line, int info_fd)
            memcpy(ptr, serial_array + (old_size - serial_array_index),
                   (sizeof(cl_t*) * serial_array_index));
            serial_array_index = 0;
-           free_safe(serial_array);
+           Free_safe(serial_array);
            serial_array = ptr;
        }
     }
index 8a768d0f147e6e3e5bbc67b27497de1ca6fba67a..11c558627e5a77ab3fc8bb11f789c2f9664d112c 100644 (file)
@@ -166,7 +166,7 @@ env_list_destroy(env_list_t *list)
     env_list_end_iteration(list);
     /* free the data in the env_t entries */
     for ( c = env_list_first(list) ; c != NULL ; c = env_list_next(list) ) {
-        free_safe(c->e_envvar);
+        Free_safe(c->e_envvar);
     }
     /* free the actual list structure */
     return (env_list_t *) u_list_destroy((u_list_t *) list);
@@ -199,9 +199,9 @@ env_list_free_envp(char **envp)
     char *p = NULL;
 
     for ( p = envp[0] ; p != NULL ; p++ ) {
-        free_safe(p);
+        Free_safe(p);
     }
-    free_safe(envp);
+    Free_safe(envp);
 
 }
 
diff --git a/fcron.c b/fcron.c
index 380020e72893953aa13a26b63c54c8cdbb4925d4..c36690e050da07b179847d1509dff297988b692d 100644 (file)
--- a/fcron.c
+++ b/fcron.c
@@ -228,7 +228,7 @@ xexit(int exit_value)
     lavg_list_destroy(lavg_list);
     free_conf();
 
-    free_safe(orig_tz_envvar);
+    Free_safe(orig_tz_envvar);
 
     explain("Exiting with code %d", exit_value);
     exit (exit_value);
diff --git a/fcron.h b/fcron.h
index 00527f4e616a471e36d2cc86c51baecaa9a576bc..70a65c0bb7ce98abc8e1f00da330d2bc05f439c8 100644 (file)
--- a/fcron.h
+++ b/fcron.h
@@ -27,6 +27,7 @@
 #define __FCRON_H__
 
 #include "global.h"
+#include "mem.h"
 #include "exe_list.h"
 #include "lavg_list.h"
 #include "fcronconf.h"
index ae46636b8ccea032cbc448a6554a15e44f9f54c6..028612872743c4b1c207915af46422f2b2811205 100644 (file)
@@ -24,6 +24,7 @@
  /* $Id: subs.c,v 1.29 2008-05-11 11:08:23 thib Exp $ */
 
 #include "global.h"
+#include "mem.h"
 #include "fcronconf.h"
 #include "subs.h"
 
@@ -70,15 +71,15 @@ void
 free_conf(void)
 /* free() the memory allocated in init_conf() */
 {
-    free_safe(fcronconf);
-    free_safe(fcrontabs);
-    free_safe(pidfile);
-    free_safe(fifofile);
-    free_safe(fcronallow);
-    free_safe(fcrondeny);
-    free_safe(shell);
-    free_safe(sendmail);
-    free_safe(editor);
+    Free_safe(fcronconf);
+    Free_safe(fcrontabs);
+    Free_safe(pidfile);
+    Free_safe(fifofile);
+    Free_safe(fcronallow);
+    Free_safe(fcrondeny);
+    Free_safe(shell);
+    Free_safe(sendmail);
+    Free_safe(editor);
 }
 
 void
index 3661d34b610e4dee75a6fab5eec6d2c1e4076ddd..5ea8db9b27d70f0c472048f733c324dd000d569f 100644 (file)
@@ -33,6 +33,7 @@
 #include "fcrondyn.h"
 #include "allow.h"
 #include "read_string.h"
+#include "mem.h"
 
 char rcs_info[] = "$Id: fcrondyn.c,v 1.19 2007-04-14 18:04:11 thib Exp $";
 
@@ -149,7 +150,7 @@ void
 xexit(int exit_val)
     /* clean & exit */
 {
-    free_safe(cmd_str);
+    Free_safe(cmd_str);
 
     exit(exit_val);
 }
@@ -371,7 +372,7 @@ authenticate_user_password(int fd)
     send(fd, buf, len, 0);
     Overwrite(buf);
     Overwrite(password);
-    free_safe(password);
+    Free_safe(password);
     
     tv.tv_sec = MAX_WAIT_TIME;
     tv.tv_usec = 0;
@@ -501,7 +502,7 @@ talk_fcron(char *cmd_str, int fd)
        return ERR;
 
     send(fd, cmd, cmd_len * sizeof(long int), 0);
-    free_safe(cmd);
+    Free_safe(cmd);
     cmd_len = 0;
 
     tv.tv_sec = MAX_WAIT_TIME;
@@ -598,7 +599,7 @@ parseopt(int argc, char *argv[])
            break;
 
        case 'i':
-           free_safe(cmd_str);
+           Free_safe(cmd_str);
            break;
 
        case 'x':
index f62a42d5bd289685921e1b4d58c38d995635d1ae..d0c2a9849548e0b071fee1f107595a2b73488693 100644 (file)
@@ -30,6 +30,7 @@
    in Makefile to create .o files which needs the corresponding .h to exist. */
 
 #include "global.h"
+#include "mem.h"
 
 extern uid_t rootuid;
 extern gid_t rootgid;
index 5e0636f4e057dddfc49225051a1f313a78822b48..0f73334acf534ca8a673cfff85d0c2e17abc83b7 100644 (file)
@@ -27,6 +27,7 @@
 #define __FCRONTAB_H__
 
 #include "global.h"
+#include "mem.h"
 #include "fcronconf.h"
 
 /* global variables */
index 6839b7aa2deb1824bc632f31804ad4cb6be14fa9..fa5040e0c039fd9608abd07a16830b1e65be5b27 100644 (file)
@@ -111,7 +111,7 @@ fifo_list_resize_array(fifo_list_t *l)
        
     e = alloc_safe(l->array_size*l->entry_size, "larger fifo_list_t array");
     memcpy(e, l->entries_array, (l->entry_size * old_size));
-    free_safe(l->entries_array);
+    Free_safe(l->entries_array);
     l->entries_array = e;    
 
     if ( l->cur_entry != NULL )
@@ -237,7 +237,7 @@ fifo_list_destroy(fifo_list_t *list)
     if ( list == NULL )
        die("Invalid argument for fifo_list_destroy(): list=%d", list);
 
-    free_safe(list->entries_array);
-    free_safe(list);
+    Free_safe(list->entries_array);
+    Free_safe(list);
     return NULL;
 }
index 0faf0ac0d5804d7ffa113a4809c35ca348fd45ab..b3c58603ef8aa588247afbc23e26d93374ead18a 100644 (file)
@@ -157,7 +157,7 @@ init_default_line(cl_t *cl, cf_t *cf)
     bzero(cl, sizeof(cl_t));
     Set(cl->cl_runas, runas);
     Set(cl->cl_mailto, runas);
-    free_safe(cl->cl_tz);
+    Free_safe(cl->cl_tz);
     set_default_opt(cl->cl_option);
     cl->cl_file = cf;
 }
@@ -282,9 +282,9 @@ read_file(char *filename, int fd)
     if (fflush(file) != 0)
         error_e("could not fflush() file_name");
     
-    free_safe(default_line.cl_runas);
-    free_safe(default_line.cl_mailto);
-    free_safe(default_line.cl_tz);
+    Free_safe(default_line.cl_runas);
+    Free_safe(default_line.cl_mailto);
+    Free_safe(default_line.cl_tz);
 
     if ( ! need_correction )
        return OK;
@@ -362,7 +362,7 @@ read_env(char *ptr, cf_t *cf)
         env_list_setenv(cf->cf_env_list, name, val, 1);
     }
     
-    free_safe(val);
+    Free_safe(val);
 
     return;
 
@@ -516,7 +516,7 @@ read_opt(char *ptr, cl_t *cl)
                buf[i++] = *ptr++;
            
            if ( strcmp(buf, "\0") == 0 ) {
-               free_safe(cl->cl_tz);
+               Free_safe(cl->cl_tz);
            }
            else {
                Set(cl->cl_tz, buf);
@@ -1293,7 +1293,7 @@ read_shortcut(char *ptr, cf_t *cf)
     if ( strcmp(cl->cl_shell, "\0") == 0 ) {
        fprintf(stderr, "%s:%d: No shell command: skipping line.\n",
                file_name, line);
-       free_safe(cl->cl_shell);
+       Free_safe(cl->cl_shell);
        goto exiterr;
     }
 
@@ -1310,7 +1310,7 @@ read_shortcut(char *ptr, cf_t *cf)
     return 1;
 
   exiterr:
-    free_safe(cl);
+    Free_safe(cl);
     need_correction = 1;
     return 1;
 }
@@ -1383,7 +1383,7 @@ read_freq(char *ptr, cf_t *cf)
     if ( strcmp(cl->cl_shell, "\0") == 0 ) {
        fprintf(stderr, "%s:%d: No shell command: skipping line.\n",
                file_name, line);
-       free_safe(cl->cl_shell);
+       Free_safe(cl->cl_shell);
        goto exiterr;
     }
 
@@ -1415,7 +1415,7 @@ read_freq(char *ptr, cf_t *cf)
           fprintf(stderr, "\n"); \
       fprintf(stderr, "%s:%d: Error while reading " DESCRP " field: " \
              "skipping line.\n", file_name, line); \
-      free_safe(cl); \
+      Free_safe(cl); \
       return; \
   }
 
@@ -1490,7 +1490,7 @@ read_arys(char *ptr, cf_t *cf)
     if ( strcmp(cl->cl_shell, "\0") == 0 ) {
        fprintf(stderr, "%s:%d: No shell command: skipping line.\n",
                file_name, line);
-       free_safe(cl->cl_shell);
+       Free_safe(cl->cl_shell);
        goto exiterr;
     }
 
@@ -1584,7 +1584,7 @@ read_period(char *ptr, cf_t *cf)
     if ( strcmp(cl->cl_shell, "\0") == 0 ) {
        fprintf(stderr, "%s:%d: No shell command: skipping line.\n",
                file_name, line);
-       free_safe(cl->cl_shell);
+       Free_safe(cl->cl_shell);
        goto exiterr;
     } 
     else if ( cl->cl_shell[0] == '*' || isdigit( (int) cl->cl_shell[0]) )
@@ -1862,11 +1862,11 @@ free_line(cl_t *cl)
     /* free a line, including its fields */
 {
     if (cl != NULL) {
-        free_safe(cl->cl_shell);
-        free_safe(cl->cl_runas);
-        free_safe(cl->cl_mailto);
-        free_safe(cl->cl_tz);
-        free_safe(cl);
+        Free_safe(cl->cl_shell);
+        Free_safe(cl->cl_runas);
+        Free_safe(cl->cl_mailto);
+        Free_safe(cl->cl_tz);
+        Free_safe(cl);
     }
 }
 
@@ -1912,8 +1912,8 @@ delete_file(const char *user_name)
     env_list_destroy(file->cf_env_list);
 
     /* finally free file itself */
-    free_safe(file->cf_user);
-    free_safe(file);
+    Free_safe(file->cf_user);
+    Free_safe(file);
 
 }
 
index 7952c7a7f90ecc989215d960282a10aed1853b82..c701a8fb5d7aa6cd428b5fa49132ae0eac774e05 100644 (file)
--- a/global.h
+++ b/global.h
 #define setegid(arg) setresgid(-1,(arg),-1)
 #endif
 
-#define Alloc(PTR, TYPE) \
-        if( (PTR = calloc(1, sizeof(TYPE))) == NULL ) \
-            die_e("Could not calloc.");
-
-#define Set(VAR, VALUE) \
-        { \
-          free_safe(VAR); \
-          VAR = strdup2(VALUE); \
-        }
-
 #define Skip_blanks(PTR) \
         while((*(PTR) == ' ') || (*(PTR) == '\t')) \
            (PTR)++;
diff --git a/job.c b/job.c
index 7f4daf2af2298ea83f20404a0db06096da4e2c92..6cd71e9f8adbd1f9a5da169947c955f0eb3ce8ad 100644 (file)
--- a/job.c
+++ b/job.c
@@ -253,7 +253,7 @@ change_user_setup_env(struct cl_t *cl,
     setup_user_and_env(cl, pas, sendmailenv, jobenv, curshell, curhome,
                        content_type, encoding);
     become_user(cl, pas, *curhome);
-    free_safe(*curhome);
+    Free_safe(*curhome);
 }
 
 void
@@ -568,7 +568,7 @@ run_job(struct exe_t *exeent)
        }
 
         become_user(line, pas, curhome);
-        free_safe(curhome);
+        Free_safe(curhome);
 
        /* restore umask to default */
        umask (saved_umask);
diff --git a/log.c b/log.c
index bbf16c3f5e2899cb50de049ba170fa9e9fb07e96..0560a2bd705a4c15a906ab693b67922a2bdc6b62 100644 (file)
--- a/log.c
+++ b/log.c
@@ -153,7 +153,7 @@ log_syslog(int priority, int fd, char *fmt, va_list args)
     log_console_str(msg);
     log_fd_str(fd, msg);
 
-    free_safe(msg);
+    Free_safe(msg);
 }
 
 /* Same as log_syslog(), but also appends an error description corresponding
@@ -172,7 +172,7 @@ log_e(int priority, char *fmt, va_list args)
     log_syslog_str(priority, msg);
     log_console_str(msg);
 
-    free_safe(msg);
+    Free_safe(msg);
 }
 
 
@@ -192,7 +192,7 @@ log_pame(int priority, pam_handle_t *pamh, int pamerrno, char *fmt, va_list args
 
     xcloselog();
 
-    free_safe(msg);
+    Free_safe(msg);
 }
 #endif
 
@@ -413,7 +413,7 @@ send_msg_fd_debug(int fd, char *fmt, ...)
 
     log_fd_str(fd, msg);
 
-    free_safe(msg);
+    Free_safe(msg);
 
     va_end(args);
 }
@@ -433,7 +433,7 @@ send_msg_fd(int fd, char *fmt, ...)
 
     log_fd_str(fd, msg);
 
-    free_safe(msg);
+    Free_safe(msg);
 
     va_end(args);
 }
diff --git a/mem.c b/mem.c
new file mode 100644 (file)
index 0000000..33839c5
--- /dev/null
+++ b/mem.c
@@ -0,0 +1,87 @@
+/*
+ * FCRON - periodic command scheduler
+ *
+ *  Copyright 2000-2010 Thibault Godouet <fcron@free.fr>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ *  The GNU General Public License can also be found in the file
+ *  `LICENSE' that comes with the fcron source distribution.
+ */
+
+#include "global.h"
+#include "mem.h"
+
+char *
+strdup2(const char *str)
+{
+    char *ptr;
+
+    if ( str == NULL )
+        return NULL;
+
+    ptr = strdup(str);
+
+    if ( ! ptr)
+        die_e("Could not strdup()");
+
+    return(ptr);
+}
+
+char *
+strndup2(const char *str, size_t n)
+{
+    char *ptr;
+
+    if ( str == NULL )
+       return NULL;
+
+    ptr = strndup(str, n);
+
+    if ( ! ptr)
+        die_e("Could not strdup()");
+
+    return(ptr);
+}
+
+void *
+alloc_safe(size_t len, const char * desc)
+/* allocate len-bytes of memory, and return the pointer.
+ * Die with a log message if there is any error */
+{
+    void *ptr = NULL;
+
+    ptr = calloc(1, len);
+    if ( ptr == NULL ) {
+        die_e("Could not allocate %d bytes of memory%s%s", len, (desc)? "for " : "", desc);
+    }
+    return ptr;
+}
+
+void *
+realloc_safe(void *cur, size_t len, const char * desc)
+/* allocate len-bytes of memory, and return the pointer.
+ * Die with a log message if there is any error */
+{
+    void *new = NULL;
+
+    new = realloc(cur, len);
+    if ( new == NULL ) {
+        die_e("Could not reallocate %d bytes of memory%s%s", len, (desc)? "for " : "", desc);
+    }
+    return new;
+}
+
+
diff --git a/mem.h b/mem.h
new file mode 100644 (file)
index 0000000..ea24d3f
--- /dev/null
+++ b/mem.h
@@ -0,0 +1,61 @@
+/*
+ * FCRON - periodic command scheduler
+ *
+ *  Copyright 2000-2010 Thibault Godouet <fcron@free.fr>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ *  The GNU General Public License can also be found in the file
+ *  `LICENSE' that comes with the fcron source distribution.
+ */
+
+/* mem: manage memory (de)allocation.
+ * Mostly wrappers around standard functions to check for errors.
+ * We also always set variable to NULL after free()ing them, and check
+ * if a variable is NULL before attempting to free it. */
+
+#ifndef __MEM_H__
+#define __MEM_H__
+
+/* macros */
+#define Alloc(PTR, TYPE) \
+{ \
+    if ( ( (PTR)=calloc(1, sizeof(TYPE)) ) == NULL ) { \
+        die_e("Could not calloc."); \
+    } \
+}
+
+#define Free_safe(PTR) \
+{ \
+    if ((PTR) != NULL) { \
+        free((PTR)); \
+        (PTR) = NULL; \
+    } \
+}
+
+#define Set(VAR, VALUE) \
+{ \
+    Free_safe((VAR)); \
+    (VAR) = strdup2((VALUE)); \
+}
+
+
+/* functions prototypes */
+extern char *strdup2(const char *);
+extern char *strndup2(const char *, size_t n);
+extern void *alloc_safe(size_t len, const char * desc);
+extern void *realloc_safe(void *ptr, size_t len, const char * desc);
+
+#endif /* __MEM_H__ */
index d5665eda0aad77076ad3e347f876b7378c7575c4..e8d0c9fce8dc0fca148bd5fccd5f2c25d36de612 100644 (file)
@@ -29,6 +29,7 @@
 #include "global.h"
 #include "read_string.h"
 #include "log.h"
+#include "mem.h"
 
 extern char debug_opt;
 
index 08429b1629262838f4cb60582eb556a973735726..9d1dcb888a5028fe72804a1342f5536baac136bd 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -850,14 +850,14 @@ and make client points to the next entry */
     debug("connection closed : fd : %d", (*client)->fcl_sock_fd);
     if (prev_client == NULL ) {
        fcrondyn_cl_base = (*client)->fcl_next;
-       free_safe((*client)->fcl_user);
-       free_safe(*client);
+       Free_safe((*client)->fcl_user);
+       Free_safe(*client);
        *client = fcrondyn_cl_base;
     }
     else {
        prev_client->fcl_next = (*client)->fcl_next;
-       free_safe((*client)->fcl_user);
-       free_safe(*client);
+       Free_safe((*client)->fcl_user);
+       Free_safe(*client);
        *client = prev_client->fcl_next;
     }
     fcrondyn_cl_num -= 1;
@@ -995,7 +995,7 @@ close_socket(void)
            close(client->fcl_sock_fd);
 
            client_buf = client->fcl_next;
-           free_safe(client);
+           Free_safe(client);
            fcrondyn_cl_num -= 1;
            client = client_buf;
        }
diff --git a/subs.c b/subs.c
index 151bd51c5b2ecc3f92691108e8908f2a22138f7b..5a44cbec36da932de899406a899e8932c15da83e 100644 (file)
--- a/subs.c
+++ b/subs.c
@@ -343,59 +343,6 @@ strcmp_until(const char *left, const char *right, char until)
     return (*left - *right);
 }
 
-
-char *
-strdup2(const char *str)
-{
-    char *ptr;
-
-    if ( str == NULL )
-       return NULL;
-
-    ptr = strdup(str);
-    
-    if ( ! ptr)
-        die_e("Could not strdup()");
-
-    return(ptr);
-}
-
-void *
-alloc_safe(size_t len, const char * desc)
-/* allocate len-bytes of memory, and return the pointer.
- * Die with a log message if there is any error */
-{
-    void *ptr = NULL;
-
-    ptr = calloc(1, len);
-    if ( ptr == NULL ) {
-        die_e("Could not allocate %d bytes of memory%s%s", len, (desc)? "for " : "", desc);
-    }
-    return ptr;
-}
-
-void *
-realloc_safe(void *cur, size_t len, const char * desc)
-/* allocate len-bytes of memory, and return the pointer.
- * Die with a log message if there is any error */
-{
-    void *new = NULL;
-
-    new = realloc(cur, len);
-    if ( new == NULL ) {
-        die_e("Could not reallocate %d bytes of memory%s%s", len, (desc)? "for " : "", desc);
-    }
-    return new;
-}
-
-void
-free_safe(void *ptr)
-    /* free() p and set it to NULL to prevent errors if it is free()ed again */
-{
-    free(ptr);
-    ptr = NULL;
-}
-
 int
 get_word(char **str)
     /* make str point the next word and return word length */
diff --git a/subs.h b/subs.h
index e35eb425c0f453192353f852a226fd709e10972c..cca1229026d5328eac49acd1453f8de203867669 100644 (file)
--- a/subs.h
+++ b/subs.h
@@ -38,10 +38,6 @@ extern int rename_as_user(const char *oldpath, const char *newpath, uid_t rename
 
 extern int remove_blanks(char *str);
 extern int strcmp_until(const char *left, const char *right, char until);
-extern char *strdup2(const char *);
-extern void *alloc_safe(size_t len, const char * desc);
-extern void *realloc_safe(void *ptr, size_t len, const char * desc);
-extern void free_safe(void *ptr);
 extern int get_word(char **str);
 extern void my_unsetenv(const char *name);
 extern void my_setenv_overwrite(const char *name, const char *value);
index 0fdf059521b4a129d098c519939fe1d11825ea3b..cd00e3c5eb32807930e321da2b298b7bfc24c7c9 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "global.h"
 #include "temp_file.h"
+#include "mem.h"
 
 extern char *tmp_path;
 extern char debug_opt;
index 69085bbdebea826d5384c4eb32f6f77197fbc9b7..8199374b1d6cbcd2bcb4460da51a2ba37a3d281c 100644 (file)
--- a/u_list.c
+++ b/u_list.c
@@ -28,6 +28,7 @@
  */
 
 #include "global.h"
+#include "mem.h"
 #include "log.h"
 #include "u_list.h"
 
@@ -259,7 +260,7 @@ u_list_destroy(u_list_t *list)
     if ( list == NULL )
        die("Invalid argument for u_list_destroy(): list=%d", list);
 
-    free_safe(list->entries_array);
-    free_safe(list);
+    Free_safe(list->entries_array);
+    Free_safe(list);
     return NULL;
 }