]> git.deb.at Git - pkg/abook.git/commitdiff
- add xstrdup to have proper checking for memory allocation failures
authorJaakko Heinonen <jheinonen@users.sourceforge.net>
Sat, 13 Aug 2005 10:49:25 +0000 (10:49 +0000)
committerJaakko Heinonen <jheinonen@users.sourceforge.net>
Sat, 13 Aug 2005 10:49:25 +0000 (10:49 +0000)
abook.c
abook.h
database.c
edit.c
filter.c
list.c
options.c
ui.c
xmalloc.c
xmalloc.h

diff --git a/abook.c b/abook.c
index 46094b7f162fb7d340a4a722d093b0c9b8872983..557ddb2f5818f7a4c5f9c8d855278aa8dc418747 100644 (file)
--- 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 1c1e29d6a0b217e90df5b17815a556a24c3a0156..0cdc513405db7b4bfba13cf4833854779b075cc6 100644 (file)
--- 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))
index 0b9b58e6b1c18e750fa508469727f8f73b364569..ab851b93ccecf43c5556ca18996b45691ac092a7 100644 (file)
@@ -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<ITEM_FIELDS; i++)
                if( item[i] && ((int)strlen(item[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 4502e106191571b1a1cd3d5946d2a11b371cb967..8f7747206d5faf9f0699208fbb76778e7e3c80ea 100644 (file)
--- 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
index 03ad954a73cf3ab8c0d44460ce54339ae3d1e060..e1111e3724b9fb4874b011cf5a6376405586242a 100644 (file)
--- 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 da6635e5a26d8da58784cff1bb153d32fe8254ca..de47d6b419246c5df21ebf85837bd9312e5514ac 100644 (file)
--- 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))
index 7d18f552f075cfcfa5f8c1c2083d472951bd410d..f698fb823f089e57c8f3a0a151e42110876e52fe 100644 (file)
--- 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 bde07e4fde9e45288580e6bd00cea188160df046..62b5e8f327d52340f2986c46005a47ee421c9b0a 100644 (file)
--- 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();
index d291f4dd76149b86679cb29e51691ca0fb72cbc6..2b7fda6b666f2b0d715050b3ad486ff19a74c773 100644 (file)
--- 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);
+}
+
index 61ea5b5c69df1e6720093961dacfe77b1dc6bec3..9b0f6de03098c5f18cfd2225ac15c18f87d589da 100644 (file)
--- 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)