From: Thibault Godouet Date: Wed, 21 Nov 2012 22:16:25 +0000 (+0000) Subject: Replaced function free_safe by macro Free_safe, and moved memory management functions... X-Git-Tag: ver3_1_0~21 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2fb87b0fe62c5bc9e70c91f13df1ca9e30939b4f;p=fcron Replaced function free_safe by macro Free_safe, and moved memory management functions to new mem.h/mem.c. 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. --- diff --git a/Makefile.in b/Makefile.in index b18ec41..1f906dd 100644 --- a/Makefile.in +++ b/Makefile.in @@ -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 c45b449..019aa71 100644 --- 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); } diff --git a/convert-fcrontab.c b/convert-fcrontab.c index 7fae425..9e04d9e 100644 --- a/convert-fcrontab.c +++ b/convert-fcrontab.c @@ -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); diff --git a/database.c b/database.c index 55ce409..4008cae 100644 --- a/database.c +++ b/database.c @@ -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; } } diff --git a/env_list.c b/env_list.c index 8a768d0..11c5586 100644 --- a/env_list.c +++ b/env_list.c @@ -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 380020e..c36690e 100644 --- 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 00527f4..70a65c0 100644 --- 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" diff --git a/fcronconf.c b/fcronconf.c index ae46636..0286128 100644 --- a/fcronconf.c +++ b/fcronconf.c @@ -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 diff --git a/fcrondyn.c b/fcrondyn.c index 3661d34..5ea8db9 100644 --- a/fcrondyn.c +++ b/fcrondyn.c @@ -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': diff --git a/fcronsighup.h b/fcronsighup.h index f62a42d..d0c2a98 100644 --- a/fcronsighup.h +++ b/fcronsighup.h @@ -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; diff --git a/fcrontab.h b/fcrontab.h index 5e0636f..0f73334 100644 --- a/fcrontab.h +++ b/fcrontab.h @@ -27,6 +27,7 @@ #define __FCRONTAB_H__ #include "global.h" +#include "mem.h" #include "fcronconf.h" /* global variables */ diff --git a/fifo_list.c b/fifo_list.c index 6839b7a..fa5040e 100644 --- a/fifo_list.c +++ b/fifo_list.c @@ -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; } diff --git a/fileconf.c b/fileconf.c index 0faf0ac..b3c5860 100644 --- a/fileconf.c +++ b/fileconf.c @@ -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); } diff --git a/global.h b/global.h index 7952c7a..c701a8f 100644 --- a/global.h +++ b/global.h @@ -156,16 +156,6 @@ #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 7f4daf2..6cd71e9 100644 --- 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 bbf16c3..0560a2b 100644 --- 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 index 0000000..33839c5 --- /dev/null +++ b/mem.c @@ -0,0 +1,87 @@ +/* + * FCRON - periodic command scheduler + * + * Copyright 2000-2010 Thibault Godouet + * + * 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 index 0000000..ea24d3f --- /dev/null +++ b/mem.h @@ -0,0 +1,61 @@ +/* + * FCRON - periodic command scheduler + * + * Copyright 2000-2010 Thibault Godouet + * + * 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__ */ diff --git a/read_string.c b/read_string.c index d5665ed..e8d0c9f 100644 --- a/read_string.c +++ b/read_string.c @@ -29,6 +29,7 @@ #include "global.h" #include "read_string.h" #include "log.h" +#include "mem.h" extern char debug_opt; diff --git a/socket.c b/socket.c index 08429b1..9d1dcb8 100644 --- 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 151bd51..5a44cbe 100644 --- 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 e35eb42..cca1229 100644 --- 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); diff --git a/temp_file.c b/temp_file.c index 0fdf059..cd00e3c 100644 --- a/temp_file.c +++ b/temp_file.c @@ -25,6 +25,7 @@ #include "global.h" #include "temp_file.h" +#include "mem.h" extern char *tmp_path; extern char debug_opt; diff --git a/u_list.c b/u_list.c index 69085bb..8199374 100644 --- 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; }