Hi,
Please find the following patch which cleans up some malloc, strcpy and
strcat calls and replaces them with asprintf. There are more calls which
need to be cleaned up, and if you like I'll continue working on those as
well.
I pasted the patch below but you can be sure the paste will turn out
horrible, so please focus on the attached file.
--- imapd.c Sat Apr 19 15:43:20 2014
+++ imapd_new.c Sat Apr 19 16:06:46 2014
@@ -346,9 +346,8 @@
if (q)
{
- r=malloc(strlen(q)+sizeof("/."));
- if (!r) write_error_exit(0);
- strcat(strcpy(r, q), "/.");
+ if(asprintf(&r, "%s%s", q, "/.") == -1)
+ write_error_exit(0);
if (access(r, 0) == 0)
{
free(r);
@@ -1383,12 +1382,12 @@
static char *alloc_filename(const char *mbox, const char *name)
{
-char *p=malloc(strlen(mbox)+strlen(name)+sizeof("/cur/"));
+ char *p;
- if (!p) write_error_exit(0);
-
- strcat(strcat(strcpy(p, mbox), "/cur/"), name);
- return (p);
+ if(asprintf(&p, "%s%s%s", mbox, "/cur/", name) == -1)
+ write_error_exit(0);
+ else
+ return (p);
}
/****************************************************************************/
@@ -1927,10 +1926,9 @@
char *p;
int fd;
-
+
if(asprintf(&p, "%s%s", folder, "/new") == -1)
write_error_exit(0);
-
fd=open(p, O_RDONLY);
if (fd >= 0)
@@ -1938,7 +1936,6 @@
fsync(fd);
close(fd);
}
-
if(asprintf(&p, "%s%s", folder, "/cur") == -1)
write_error_exit(0);
@@ -2325,9 +2322,8 @@
struct imapscaninfo minfo;
char *p;
- if(asprintf(&p, "%s%s", new_path, "/" IMAPDB) == -1)
+ if(asprintf(&p, "%s%s", "/" IMAPDB) == -1)
write_error_exit(0);
-
unlink(p);
free(p);
imapscan_init(&minfo);
@@ -2558,14 +2554,12 @@
}
return 0;
}
- q=malloc(strlen(p)+sizeof("/new"));
- if (!q)
+ if(asprintf(&q, "%s%s", p, "/new") == -1)
{
free(p);
maildir_aclt_list_destroy(aclt_list);
return -1;
}
- strcat(strcpy(q, p), "/new");
free(p);
if (maildir_aclt_list_add(aclt_list,
@@ -4334,11 +4328,10 @@
if (p && (p=maildir_shareddir(".", p+1)) != NULL)
{
int rc;
- char *q=malloc(strlen(p)+sizeof("/shared"));
+ char *q;
+ if(asprintf(&q, "%s%s", p, "/shared") == -1)
+ write_error_exit(0);
- if (!q) write_error_exit(0);
-
- strcat(strcpy(q, p), "/shared");
free(p);
rc=append(tag, tok->tokenbuf, q);
free(q);
@@ -6159,10 +6152,9 @@
isshared=0;
if (is_sharedsubdir(cqinfo.destmailbox))
{
- char *p=malloc(strlen(cqinfo.destmailbox)+sizeof("/shared"));
-
- if (!p) write_error_exit(0);
- strcat(strcpy(p, cqinfo.destmailbox), "/shared");
+ char *p;
+ if(asprintf(&p, "%s%s", cqinfo.destmailbox, "/shared") == -1)
+ write_error_exit(0);
free(mailbox);
mailbox=cqinfo.destmailbox=p;
--- imapd.c Sat Apr 19 15:43:20 2014
+++ imapd_new.c Sat Apr 19 16:06:46 2014
@@ -346,9 +346,8 @@
if (q)
{
- r=malloc(strlen(q)+sizeof("/."));
- if (!r) write_error_exit(0);
- strcat(strcpy(r, q), "/.");
+ if(asprintf(&r, "%s%s", q, "/.") == -1)
+ write_error_exit(0);
if (access(r, 0) == 0)
{
free(r);
@@ -1383,12 +1382,12 @@
static char *alloc_filename(const char *mbox, const char *name)
{
-char *p=malloc(strlen(mbox)+strlen(name)+sizeof("/cur/"));
+ char *p;
- if (!p) write_error_exit(0);
-
- strcat(strcat(strcpy(p, mbox), "/cur/"), name);
- return (p);
+ if(asprintf(&p, "%s%s%s", mbox, "/cur/", name) == -1)
+ write_error_exit(0);
+ else
+ return (p);
}
/****************************************************************************/
@@ -1927,10 +1926,9 @@
char *p;
int fd;
-
+
if(asprintf(&p, "%s%s", folder, "/new") == -1)
write_error_exit(0);
-
fd=open(p, O_RDONLY);
if (fd >= 0)
@@ -1938,7 +1936,6 @@
fsync(fd);
close(fd);
}
-
if(asprintf(&p, "%s%s", folder, "/cur") == -1)
write_error_exit(0);
@@ -2325,9 +2322,8 @@
struct imapscaninfo minfo;
char *p;
- if(asprintf(&p, "%s%s", new_path, "/" IMAPDB) == -1)
+ if(asprintf(&p, "%s%s", "/" IMAPDB) == -1)
write_error_exit(0);
-
unlink(p);
free(p);
imapscan_init(&minfo);
@@ -2558,14 +2554,12 @@
}
return 0;
}
- q=malloc(strlen(p)+sizeof("/new"));
- if (!q)
+ if(asprintf(&q, "%s%s", p, "/new") == -1)
{
free(p);
maildir_aclt_list_destroy(aclt_list);
return -1;
}
- strcat(strcpy(q, p), "/new");
free(p);
if (maildir_aclt_list_add(aclt_list,
@@ -4334,11 +4328,10 @@
if (p && (p=maildir_shareddir(".", p+1)) != NULL)
{
int rc;
- char *q=malloc(strlen(p)+sizeof("/shared"));
+ char *q;
+ if(asprintf(&q, "%s%s", p, "/shared") == -1)
+ write_error_exit(0);
- if (!q) write_error_exit(0);
-
- strcat(strcpy(q, p), "/shared");
free(p);
rc=append(tag, tok->tokenbuf, q);
free(q);
@@ -6159,10 +6152,9 @@
isshared=0;
if (is_sharedsubdir(cqinfo.destmailbox))
{
- char *p=malloc(strlen(cqinfo.destmailbox)+sizeof("/shared"));
-
- if (!p) write_error_exit(0);
- strcat(strcpy(p, cqinfo.destmailbox), "/shared");
+ char *p;
+ if(asprintf(&p, "%s%s", cqinfo.destmailbox, "/shared") == -1)
+ write_error_exit(0);
free(mailbox);
mailbox=cqinfo.destmailbox=p;
------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/NeoTech
_______________________________________________
Courier-imap mailing list
Courier-imap@lists.sourceforge.net
Unsubscribe: https://lists.sourceforge.net/lists/listinfo/courier-imap