From 3bf3df8d5624f6c9dc05b356f439400d1dccd36f Mon Sep 17 00:00:00 2001 From: Brendan Cully Date: Sat, 21 Jul 2012 19:51:31 -0700 Subject: [PATCH] Split mutt_buffer_new out of mutt_buffer_init. Currently, no callers were providing a non-NULL buffer to mutt_buffer_init, and splitting it will allow more sane semantics for buffer allocation, initialization, and destruction in a later patch. --- imap/imap.c | 2 +- imap/util.c | 2 +- mbyte.c | 2 +- muttlib.c | 42 +++++++++++++++++++++++++----------------- parse.c | 4 ++-- protos.h | 3 ++- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/imap/imap.c b/imap/imap.c index 6fb720f1..35dac43d 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -963,7 +963,7 @@ int imap_exec_msgset (IMAP_DATA* idata, const char* pre, const char* post, int rc; int count = 0; - if (! (cmd = mutt_buffer_init (NULL))) + if (! (cmd = mutt_buffer_new ())) { dprint (1, (debugfile, "imap_exec_msgset: unable to allocate buffer\n")); return -1; diff --git a/imap/util.c b/imap/util.c index f7e0ef5a..c8b002ef 100644 --- a/imap/util.c +++ b/imap/util.c @@ -374,7 +374,7 @@ IMAP_DATA* imap_new_idata (void) if (!idata) return NULL; - if (!(idata->cmdbuf = mutt_buffer_init (NULL))) + if (!(idata->cmdbuf = mutt_buffer_new ())) FREE (&idata); idata->cmdslots = ImapPipelineDepth + 2; diff --git a/mbyte.c b/mbyte.c index 6e450246..fb96a714 100644 --- a/mbyte.c +++ b/mbyte.c @@ -534,7 +534,7 @@ int mutt_filter_unprintable (char **s) char *p = *s; mbstate_t mbstate1, mbstate2; - if (!(b = mutt_buffer_init (b))) + if (!(b = mutt_buffer_new ())) return -1; memset (&mbstate1, 0, sizeof (mbstate1)); memset (&mbstate2, 0, sizeof (mbstate2)); diff --git a/muttlib.c b/muttlib.c index f93fccd5..725a105d 100644 --- a/muttlib.c +++ b/muttlib.c @@ -1105,10 +1105,10 @@ void mutt_FormatString (char *dest, /* output buffer */ srccopy[n-1] = '\0'; /* prepare BUFFERs */ - srcbuf = mutt_buffer_from(NULL, srccopy); + srcbuf = mutt_buffer_from (srccopy); srcbuf->dptr = srcbuf->data; - word = mutt_buffer_init(NULL); - command = mutt_buffer_init(NULL); + word = mutt_buffer_new (); + command = mutt_buffer_new (); /* Iterate expansions across successive arguments */ do { @@ -1643,7 +1643,18 @@ void mutt_sleep (short s) if (SleepTime > s) sleep (SleepTime); else if (s) - sleep (s); + sleep(s); +} + +/* creates and initializes a BUFFER */ +BUFFER *mutt_buffer_new(void) { + BUFFER *b; + + b = safe_malloc(sizeof(BUFFER)); + + mutt_buffer_init(b); + + return b; } /* @@ -1653,16 +1664,12 @@ void mutt_sleep (short s) * Disregards the 'destroy' flag, which seems reserved for caller. * This is bad, but there's no apparent protocol for it. */ -BUFFER * mutt_buffer_init(BUFFER *b) -{ - if (!b) - { +BUFFER *mutt_buffer_init (BUFFER *b) { + if (!b) { b = safe_malloc(sizeof(BUFFER)); if (!b) - return NULL; - } - else - { + return NULL ; + } else { FREE(&b->data); } memset(b, 0, sizeof(BUFFER)); @@ -1677,14 +1684,15 @@ BUFFER * mutt_buffer_init(BUFFER *b) * Disregards the 'destroy' flag, which seems reserved for caller. * This is bad, but there's no apparent protocol for it. */ -BUFFER * mutt_buffer_from(BUFFER *b, char *seed) -{ +BUFFER *mutt_buffer_from (char *seed) { + BUFFER *b; + if (!seed) return NULL; - b = mutt_buffer_init(b); - b->data = safe_strdup (seed); - b->dsize = mutt_strlen (seed); + b = mutt_buffer_new (); + b->data = safe_strdup(seed); + b->dsize = mutt_strlen(seed); b->dptr = (char *) b->data + b->dsize; return b; } diff --git a/parse.c b/parse.c index 9f227a8c..ebfd057c 100644 --- a/parse.c +++ b/parse.c @@ -1392,13 +1392,13 @@ ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs, /* spam tag is new, and match expr is non-empty; copy */ else if (!e->spam && *buf) { - e->spam = mutt_buffer_from(NULL, buf); + e->spam = mutt_buffer_from (buf); } /* match expr is empty; plug in null string if no existing tag */ else if (!e->spam) { - e->spam = mutt_buffer_from(NULL, ""); + e->spam = mutt_buffer_from(""); } if (e->spam && e->spam->data) diff --git a/protos.h b/protos.h index 95611ed1..4abd66bb 100644 --- a/protos.h +++ b/protos.h @@ -39,8 +39,9 @@ struct hdr_format_info void mutt_make_string_info (char *, size_t, const char *, struct hdr_format_info *, format_flag); int mutt_extract_token (BUFFER *, BUFFER *, int); +BUFFER *mutt_buffer_new (void); BUFFER * mutt_buffer_init (BUFFER *); -BUFFER * mutt_buffer_from (BUFFER *, char *); +BUFFER * mutt_buffer_from (char *); void mutt_buffer_free(BUFFER **); int mutt_buffer_printf (BUFFER*, const char*, ...); void mutt_buffer_add (BUFFER*, const char*, size_t); -- 2.40.0