X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=common-src%2Falloc.c;h=50cfc084772911587884597618930ffab6302a57;hb=c3820b41ce108fd6c7cbd99b75ec06d1e97ed2b5;hp=0a0cdef3555f72a6b29d115910fdcd533b0bee06;hpb=eefb15c5c04acb3c75f0c704ea664feb1bbae75c;p=debian%2Famanda diff --git a/common-src/alloc.c b/common-src/alloc.c index 0a0cdef..50cfc08 100644 --- a/common-src/alloc.c +++ b/common-src/alloc.c @@ -24,7 +24,7 @@ * file named AUTHORS, in the root directory of this distribution. */ /* - * $Id: alloc.c,v 1.35 2005/12/21 19:07:49 paddy_s Exp $ + * $Id: alloc.c,v 1.37 2006/07/05 10:41:32 martinea Exp $ * * Memory allocators with error handling. If the allocation fails, * errordump() is called, relieving the caller from checking the return @@ -34,7 +34,8 @@ #include "arglist.h" #include "queue.h" -static char *internal_vstralloc P((const char *, va_list)); +#define MIN_ALLOC 64 +static char *internal_vstralloc(const char *, va_list); /* *===================================================================== @@ -63,10 +64,11 @@ static char *internal_vstralloc P((const char *, va_list)); */ const char * -debug_caller_loc(file, line) - const char *file; - int line; +debug_caller_loc( + const char *file, + int line) { + /*@keep@*/ struct loc_str { char *str; LIST_ENTRY(loc_str) le; @@ -78,7 +80,7 @@ debug_caller_loc(file, line) if ((p = strrchr(file, '/')) != NULL) file = p + 1; /* just the last path element */ - snprintf(loc, sizeof(loc), "%s@%d", file, line); + snprintf(loc, SIZEOF(loc), "%s@%d", file, line); for (ls = LIST_FIRST(&root); ls != NULL; ls = LIST_NEXT(ls, le)) { if (strcmp(loc, ls->str) == 0) { @@ -97,7 +99,7 @@ debug_caller_loc(file, line) /* * This is a new entry. Put it at the head of the list. */ - ls = malloc(sizeof(*ls)); + ls = malloc(SIZEOF(*ls)); if (ls == NULL) return ("??"); /* not much better than abort */ ls->str = malloc(strlen(loc) + 1); @@ -138,9 +140,9 @@ static char *saved_file; static int saved_line; int -debug_alloc_push (s, l) - char *s; - int l; +debug_alloc_push( + char *s, + int l) { debug_alloc_loc_info[debug_alloc_ptr].file = s; debug_alloc_loc_info[debug_alloc_ptr].line = l; @@ -162,7 +164,7 @@ debug_alloc_push (s, l) */ void -debug_alloc_pop () +debug_alloc_pop(void) { debug_alloc_ptr = (debug_alloc_ptr + DEBUG_ALLOC_SAVE_MAX - 1) % DEBUG_ALLOC_SAVE_MAX; @@ -174,20 +176,21 @@ debug_alloc_pop () * alloc - a wrapper for malloc. */ void * -debug_alloc(s, l, size) - const char *s; - int l; - size_t size; +debug_alloc( + const char *s, + int l, + size_t size) { void *addr; malloc_enter(debug_caller_loc(s, l)); addr = (void *)malloc(max(size, 1)); if (addr == NULL) { - errordump("%s@%d: memory allocation failed (%u bytes requested)", + errordump("%s@%d: memory allocation failed (" SIZE_T_FMT " bytes requested)", s ? s : "(unknown)", s ? l : -1, - size); + (SIZE_T_FMT_TYPE)size); + /*NOTREACHED*/ } malloc_leave(debug_caller_loc(s, l)); return addr; @@ -198,11 +201,11 @@ debug_alloc(s, l, size) * newalloc - free existing buffer and then alloc a new one. */ void * -debug_newalloc(s, l, old, size) - const char *s; - int l; - void *old; - size_t size; +debug_newalloc( + const char *s, + int l, + void *old, + size_t size) { char *addr; @@ -219,10 +222,10 @@ debug_newalloc(s, l, old, size) * Just like strdup()! */ char * -debug_stralloc(s, l, str) - const char *s; - int l; - const char *str; +debug_stralloc( + const char *s, + int l, + const char *str) { char *addr; @@ -235,9 +238,10 @@ debug_stralloc(s, l, str) /* vstrextend -- Extends the existing string by appending the other * arguments. */ -arglist_function(char *vstrextend, - char **, - oldstr) +/*@ignore@*/ +arglist_function( + char *vstrextend, + char **, oldstr) { char *keep = *oldstr; va_list ap; @@ -252,6 +256,7 @@ arglist_function(char *vstrextend, arglist_end(ap); return *oldstr; } +/*@end@*/ /* * internal_vstralloc - copies up to MAX_STR_ARGS strings into newly @@ -264,21 +269,21 @@ arglist_function(char *vstrextend, #define MAX_VSTRALLOC_ARGS 32 static char * -internal_vstralloc(str, argp) - const char *str; - va_list argp; +internal_vstralloc( + const char *str, + va_list argp) { char *next; char *result; - int a; + int a, b; size_t total_len; const char *arg[MAX_VSTRALLOC_ARGS+1]; size_t len[MAX_VSTRALLOC_ARGS+1]; size_t l; - const char *s; if (str == NULL) { - return NULL; /* probably will not happen */ + errordump("internal_vstralloc: str is NULL"); + /*NOTREACHED*/ } a = 0; @@ -292,24 +297,24 @@ internal_vstralloc(str, argp) continue; /* minor optimisation */ } if (a >= MAX_VSTRALLOC_ARGS) { - errordump("%s@%d: more than %d arg%s to vstralloc", + errordump("%s@%d: more than %d args to vstralloc", saved_file ? saved_file : "(unknown)", saved_file ? saved_line : -1, - MAX_VSTRALLOC_ARGS, - (MAX_VSTRALLOC_ARGS == 1) ? "" : "s"); + MAX_VSTRALLOC_ARGS); + /*NOTREACHED*/ } arg[a] = next; len[a] = l; total_len += l; a++; } - arg[a] = NULL; - len[a] = 0; - next = result = debug_alloc(saved_file, saved_line, total_len+1); - for (a = 0; (s = arg[a]) != NULL; a++) { - memcpy(next, s, len[a]); - next += len[a]; + result = debug_alloc(saved_file, saved_line, total_len+1); + + next = result; + for (b = 0; b < a; b++) { + memcpy(next, arg[b], len[b]); + next += len[b]; } *next = '\0'; @@ -320,7 +325,9 @@ internal_vstralloc(str, argp) /* * vstralloc - copies multiple strings into newly allocated memory. */ -arglist_function(char *debug_vstralloc, const char *, str) +arglist_function( + char *debug_vstralloc, + const char *, str) { va_list argp; char *result; @@ -339,11 +346,11 @@ arglist_function(char *debug_vstralloc, const char *, str) * newstralloc - free existing string and then stralloc a new one. */ char * -debug_newstralloc(s, l, oldstr, newstr) - const char *s; - int l; - char *oldstr; - const char *newstr; +debug_newstralloc( + const char *s, + int l, + char *oldstr, + const char *newstr) { char *addr; @@ -358,11 +365,10 @@ debug_newstralloc(s, l, oldstr, newstr) /* * newvstralloc - free existing string and then vstralloc a new one. */ -arglist_function1(char *debug_newvstralloc, - char *, - oldstr, - const char *, - newstr) +arglist_function1( + char *debug_newvstralloc, + char *, oldstr, + const char *, newstr) { va_list argp; char *result; @@ -378,11 +384,48 @@ arglist_function1(char *debug_newvstralloc, } +/* + * vstrallocf - copies printf formatted string into newly allocated memory. + */ +char * +debug_vstrallocf( + const char *fmt, + ...) +{ + char * result; + size_t size; + va_list argp; + + debug_alloc_pop(); + malloc_enter(debug_caller_loc(saved_file, saved_line)); + + result = debug_alloc(saved_file, saved_line, MIN_ALLOC); + if (result != NULL) { + + arglist_start(argp, fmt); + size = vsnprintf(result, MIN_ALLOC, fmt, argp); + arglist_end(argp); + + if (size >= (size_t)MIN_ALLOC) { + amfree(result); + result = debug_alloc(saved_file, saved_line, size + 1); + + arglist_start(argp, fmt); + (void)vsnprintf(result, size + 1, fmt, argp); + arglist_end(argp); + } + } + + malloc_leave(debug_caller_loc(saved_file, saved_line)); + return result; +} + +extern char **environ; /* * safe_env - build a "safe" environment list. */ char ** -safe_env() +safe_env(void) { static char *safe_env_list[] = { "TZ", @@ -402,7 +445,7 @@ safe_env() * safe_env_list so our result is always a valid, although possibly * empty, environment list. */ -#define SAFE_ENV_CNT (sizeof(safe_env_list) / sizeof(*safe_env_list)) +#define SAFE_ENV_CNT (size_t)(sizeof(safe_env_list) / sizeof(*safe_env_list)) char **envp = safe_env_list + SAFE_ENV_CNT - 1; char **p; @@ -410,8 +453,29 @@ safe_env() char *s; char *v; size_t l1, l2; + char **env; + int env_cnt; + + if (getuid() == geteuid() && getgid() == getegid()) { + env_cnt = 1; + for (env = environ; *env != NULL; env++) + env_cnt++; + if ((q = (char **)malloc(env_cnt*SIZEOF(char *))) != NULL) { + envp = q; + p = envp; + for (env = environ; *env != NULL; env++) { + if (strncmp("LANG=", *env, 5) != 0 && + strncmp("LC_", *env, 3) != 0) { + *p = stralloc(*env); + p++; + } + } + *p = NULL; + } + return envp; + } - if ((q = (char **)malloc(sizeof(safe_env_list))) != NULL) { + if ((q = (char **)malloc(SIZEOF(safe_env_list))) != NULL) { envp = q; for (p = safe_env_list; *p != NULL; p++) { if ((v = getenv(*p)) == NULL) { @@ -447,19 +511,19 @@ safe_env() */ int -debug_amtable_alloc(s, l, table, current, elsize, count, bump, init_func) - const char *s; - int l; - void **table; - int *current; - size_t elsize; - int count; - int bump; - void (*init_func)(void *); +debug_amtable_alloc( + const char *s, + int l, + void **table, + size_t *current, + size_t elsize, + size_t count, + int bump, + void (*init_func)(void *)) { void *table_new; - int table_count_new; - int i; + size_t table_count_new; + size_t i; if (count >= *current) { table_count_new = ((count + bump) / bump) * bump; @@ -492,9 +556,9 @@ debug_amtable_alloc(s, l, table, current, elsize, count, bump, init_func) */ void -amtable_free(table, current) - void **table; - int *current; +amtable_free( + void **table, + size_t *current) { amfree(*table); *current = 0;