From 781c0d657fffe80e45d0fda6ed5ddbf5f796fefb Mon Sep 17 00:00:00 2001 From: Jaakko Heinonen Date: Sat, 13 Aug 2005 10:49:25 +0000 Subject: [PATCH] - add xstrdup to have proper checking for memory allocation failures --- abook.c | 8 ++++---- abook.h | 1 - database.c | 16 +++++++++------- edit.c | 14 +++++++++----- filter.c | 24 ++++++++++++------------ list.c | 3 ++- options.c | 3 ++- ui.c | 3 ++- xmalloc.c | 13 +++++++++++++ xmalloc.h | 1 + 10 files changed, 54 insertions(+), 32 deletions(-) diff --git a/abook.c b/abook.c index 46094b7..557ddb2 100644 --- a/abook.c +++ b/abook.c @@ -259,7 +259,7 @@ set_filename(char **var, char *path) assert(path != NULL); if(*path == '/') { - *var = strdup(path); + *var = xstrdup(path); return; } @@ -503,7 +503,7 @@ make_mailstr(int item) ret = *database[item][EMAIL] ? mkstr("%s <%s>", name, email) : - strdup(name); + xstrdup(name); free(name); @@ -745,8 +745,8 @@ add_email_add_item(int quiet, char *name, char *email) } memset(item, 0, sizeof(item)); - item[NAME] = strdup(name); - item[EMAIL] = strdup(email); + item[NAME] = xstrdup(name); + item[EMAIL] = xstrdup(email); add_item2database(item); return 1; diff --git a/abook.h b/abook.h index 1c1e29d..0cdc513 100644 --- a/abook.h +++ b/abook.h @@ -38,7 +38,6 @@ int strncasecmp (const char *, const char *, size_t); #define safe_atoi(X) ((X == NULL) ? 0 : atoi(X)) #define safe_str(X) ((X == NULL) ? "" : X) -#define safe_strdup(X) ((X == NULL) ? NULL : strdup(X)) #ifndef min # define min(x,y) (((x)<(y)) ? (x):(y)) diff --git a/database.c b/database.c index 0b9b58e..ab851b9 100644 --- a/database.c +++ b/database.c @@ -123,7 +123,7 @@ parse_database(FILE *in) *tmp++ = '\0'; for(i = 0; i < ITEM_FIELDS; i++) if(!strcmp(abook_fields[i].key, line)) { - item[i] = strdup(tmp); + item[i] = xstrdup(tmp); goto next; } } @@ -250,13 +250,13 @@ validate_item(list_item item) char *tmp; if(item[EMAIL] == NULL) - item[EMAIL] = strdup(""); + item[EMAIL] = xstrdup(""); for(i=0; i _MAX_FIELD_LEN(i) ) ) { tmp = item[i]; item[i][_MAX_FIELD_LEN(i)-1] = 0; - item[i] = strdup(item[i]); + item[i] = xstrdup(item[i]); free(tmp); } } @@ -337,7 +337,7 @@ get_surname(char *s) while(p > s && *(p - 1) != ' ') p--; - return strdup(p); + return xstrdup(p); } static int @@ -437,13 +437,15 @@ find_item(char *str, int start, int search_fields[]) if(list_is_empty() || !is_valid_item(start)) return -2; /* error */ - findstr = strdup(str); + findstr = xstrdup(str); findstr = strlower(findstr); e.item = start - 1; /* must be "real start" - 1 */ db_enumerate_items(e) { - for( i = 0; search_fields[i] >= 0; i++ ) { - tmp = safe_strdup(database[e.item][search_fields[i]]); + for(i = 0; search_fields[i] >= 0; i++) { + if(database[e.item][search_fields[i]] == NULL) + continue; + tmp = xstrdup(database[e.item][search_fields[i]]); if( tmp && strstr(strlower(tmp), findstr) ) { ret = e.item; goto out; diff --git a/edit.c b/edit.c index 4502e10..8f77472 100644 --- a/edit.c +++ b/edit.c @@ -136,7 +136,11 @@ edit_undo(int item, int mode) } backup = xmalloc(sizeof(list_item)); for(i = 0; i < ITEM_FIELDS; i++) - backup[0][i] = safe_strdup(database[item][i]); + if(database[item][i] == NULL) + backup[0][i] = NULL; + else + backup[0][i] = + xstrdup(database[item][i]); break; case RESTORE_ITEM: if(backup) { @@ -276,12 +280,12 @@ change_name_field(char **field) { char *tmp; - tmp = strdup(*field); + tmp = xstrdup(*field); change_field("Name: ", field); if(*field == NULL || ! **field) { xfree(*field); - *field = strdup(tmp); + *field = xstrdup(tmp); } xfree(tmp); @@ -304,7 +308,7 @@ edit_emails(char c, int item) int email_num = c - '2'; split_emailstr(item, emails); - field = strdup(emails[email_num]); + field = xstrdup(emails[email_num]); if(change_field("E-mail: ", &field)) return; /* user cancelled ( C-g ) */ @@ -328,7 +332,7 @@ edit_emails(char c, int item) if(tmp[len -1] == ',') tmp[len-1] =0; - database[item][EMAIL] = strdup(tmp); + database[item][EMAIL] = xstrdup(tmp); } static int diff --git a/filter.c b/filter.c index 03ad954..e1111e3 100644 --- a/filter.c +++ b/filter.c @@ -145,13 +145,13 @@ get_real_name() pwent = getpwnam(username); - if((tmp = strdup(pwent->pw_gecos)) == NULL) - return strdup(username); + if((tmp = xstrdup(pwent->pw_gecos)) == NULL) + return xstrdup(username); rtn = sscanf(pwent->pw_gecos, "%[^,]", tmp); if (rtn == EOF || rtn == 0) { free(tmp); - return strdup(username); + return xstrdup(username); } else return tmp; } @@ -532,7 +532,7 @@ ldif_add_item(ldif_item li) for(i=0; i < LDIF_ITEM_FIELDS; i++) { if(ldif_conv_table[i] >= 0 && li[i] && *li[i] ) - abook_item[ldif_conv_table[i]] = strdup(li[i]); + abook_item[ldif_conv_table[i]] = xstrdup(li[i]); } add_item2database(abook_item); @@ -560,7 +560,7 @@ ldif_convert(ldif_item item, char *type, char *value) break; if(item[i]) xfree(item[i]); - item[i] = strdup(value); + item[i] = xstrdup(value); } } } @@ -656,7 +656,7 @@ mutt_read_line(FILE *in, char **alias, char **rest) while(ISSPACE(*ptr)) ptr++; - *rest = strdup(ptr); + *rest = xstrdup(ptr); free(line); return 0; @@ -948,7 +948,7 @@ pine_parse_buf(char *buf) strncpy(tmp, start, len); tmp[len] = 0; if(*tmp) - item[pine_conv_table[i]] = strdup(tmp); + item[pine_conv_table[i]] = xstrdup(tmp); } start = end + 1; } @@ -1098,7 +1098,7 @@ csv_remove_quotes(char *s) char *copy, *trimmed; int len; - copy = trimmed = strdup(s); + copy = trimmed = xstrdup(s); strtrim(trimmed); len = strlen(trimmed); @@ -1109,13 +1109,13 @@ csv_remove_quotes(char *s) } trimmed[len - 1] = 0; trimmed++; - trimmed = strdup(trimmed); + trimmed = xstrdup(trimmed); free(copy); return trimmed; } xfree(copy); - return strdup(s); + return xstrdup(s); } static void @@ -1576,9 +1576,9 @@ mutt_alias_genalias(int i) char *tmp, *pos; if(database[i][NICK]) - return strdup(database[i][NICK]); + return xstrdup(database[i][NICK]); - tmp = strdup(database[i][NAME]); + tmp = xstrdup(database[i][NAME]); if( ( pos = strchr(tmp, ' ') ) ) *pos = 0; diff --git a/list.c b/list.c index da6635e..de47d6b 100644 --- a/list.c +++ b/list.c @@ -17,6 +17,7 @@ #include "list.h" #include "misc.h" #include "options.h" +#include "xmalloc.h" #define MIN_EXTRA_COLUMN ADDRESS /* 2 */ #define MAX_EXTRA_COLUMN LAST_FIELD @@ -354,7 +355,7 @@ duplicate_item() return 1; for(i = 0; i < ITEM_FIELDS; i++) - item[i] = database[curitem][i] ? strdup(database[curitem][i]) : + item[i] = database[curitem][i] ? xstrdup(database[curitem][i]) : NULL; if(add_item2database(item)) diff --git a/options.c b/options.c index 7d18f55..f698fb8 100644 --- a/options.c +++ b/options.c @@ -16,6 +16,7 @@ #include "options.h" #include "abook.h" #include "misc.h" +#include "xmalloc.h" #ifndef FALSE # define FALSE 0 @@ -99,7 +100,7 @@ set_str(enum str_opts opt, char *value) if(str_opts[opt]) free(str_opts[opt]); - str_opts[opt] = strdup(value); + str_opts[opt] = xstrdup(value); } int diff --git a/ui.c b/ui.c index bde07e4..62b5e8f 100644 --- a/ui.c +++ b/ui.c @@ -22,6 +22,7 @@ #include "misc.h" #include "options.h" #include "filter.h" +#include "xmalloc.h" #ifdef HAVE_CONFIG_H # include "config.h" #endif @@ -605,7 +606,7 @@ ui_open_datafile() load_database(datafile); } else { free(datafile); - datafile = strdup(filename); + datafile = xstrdup(filename); } refresh_screen(); diff --git a/xmalloc.c b/xmalloc.c index d291f4d..2b7fda6 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -135,3 +135,16 @@ xrealloc_inc(void *ptr, size_t size, size_t inc) return ptr; } +char * +xstrdup(const char *s) +{ + size_t len = strlen(s); + void *new; + + new = xmalloc_inc(len, 1); + if(new == NULL) + return NULL; + + return (char *)memcpy(new, s, len + 1); +} + diff --git a/xmalloc.h b/xmalloc.h index 61ea5b5..9b0f6de 100644 --- a/xmalloc.h +++ b/xmalloc.h @@ -16,6 +16,7 @@ void * xmalloc0(size_t); void * xmalloc_inc(size_t, size_t); void * xrealloc(void *, size_t); void * xrealloc_inc(void *, size_t, size_t); +char * xstrdup(const char *s); #define xfree(ptr) do { free(ptr); ptr = NULL; } while(0) -- 2.39.2