* replace rewrite_ctdl_sieve_config()s use of malloc/realloc/strlen/sprintf by StrBuf...
authorWilfried Göesgens <willi@citadel.org>
Sun, 7 Mar 2010 09:48:49 +0000 (09:48 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sun, 7 Mar 2010 09:48:49 +0000 (09:48 +0000)
citadel/modules/sieve/serv_sieve.c

index 88c72e9943556e8f759745fc9bfc8230123c40a3..4ca061eae79afe883ad94705a018e3f64bc4caee 100644 (file)
@@ -355,6 +355,7 @@ int ctdl_vacation(sieve2_context_t *s, void *my)
        /* If we get to this point, create a new record.
         */
        vptr = malloc(sizeof(struct sdm_vacation));
+       memset(vptr, 0, sizeof(struct sdm_vacation));
        vptr->timestamp = time(NULL);
        safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr);
        vptr->next = cs->u->first_vacation;
@@ -761,33 +762,28 @@ void get_sieve_config_backend(long msgnum, void *userdata) {
  * otherwise it just frees the data structures.)
  */
 void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
-       char *text;
+       StrBuf *text;
        struct sdm_script *sptr;
        struct sdm_vacation *vptr;
-       size_t tsize;
-
-       text = malloc(1024);
-       tsize = 1024;
-       snprintf(text, 1024,
-               "Content-type: application/x-citadel-sieve-config\n"
-               "\n"
-               CTDLSIEVECONFIGSEPARATOR
-               "lastproc|%ld"
-               CTDLSIEVECONFIGSEPARATOR
-       ,
-               u->lastproc
-       );
+       
+       text = NewStrBufPlain(NULL, SIZ);
+       StrBufPrintf(text,
+                    "Content-type: application/x-citadel-sieve-config\n"
+                    "\n"
+                    CTDLSIEVECONFIGSEPARATOR
+                    "lastproc|%ld"
+                    CTDLSIEVECONFIGSEPARATOR
+                    ,
+                    u->lastproc
+               );
 
        while (u->first_script != NULL) {
-               size_t tlen;
-               tlen = strlen(text);
-               tsize = tlen + strlen(u->first_script->script_content) +256;
-               text = realloc(text, tsize);
-               sprintf(&text[strlen(text)], "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
-                       u->first_script->script_name,
-                       u->first_script->script_active,
-                       u->first_script->script_content
-               );
+               StrBufAppendPrintf(text,
+                                  "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
+                                  u->first_script->script_name,
+                                  u->first_script->script_active,
+                                  u->first_script->script_content
+                       );
                sptr = u->first_script;
                u->first_script = u->first_script->next;
                free(sptr->script_content);
@@ -796,32 +792,26 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
 
        if (u->first_vacation != NULL) {
 
-               tsize = strlen(text) + 256;
-               for (vptr = u->first_vacation; vptr != NULL; vptr = vptr->next) {
-                       tsize += strlen(vptr->fromaddr + 32);
-               }
-               text = realloc(text, tsize);
-
-               sprintf(&text[strlen(text)], "vacation|\n");
+               StrBufAppendPrintf(text, "vacation|\n");
                while (u->first_vacation != NULL) {
                        if ( (time(NULL) - u->first_vacation->timestamp) < (MAX_VACATION * 86400)) {
-                               sprintf(&text[strlen(text)], "%s|%ld\n",
-                                       u->first_vacation->fromaddr,
-                                       u->first_vacation->timestamp
-                               );
+                               StrBufAppendPrintf(text, "%s|%ld\n",
+                                                  u->first_vacation->fromaddr,
+                                                  u->first_vacation->timestamp
+                                       );
                        }
                        vptr = u->first_vacation;
                        u->first_vacation = u->first_vacation->next;
                        free(vptr);
                }
-               sprintf(&text[strlen(text)], CTDLSIEVECONFIGSEPARATOR);
+               StrBufAppendPrintf(text, CTDLSIEVECONFIGSEPARATOR);
        }
 
        if (yes_write_to_disk)
        {
                /* Save the config */
                quickie_message("Citadel", NULL, NULL, u->config_roomname,
-                               text,
+                               ChrPtr(text),
                                4,
                                "Sieve configuration"
                );
@@ -832,7 +822,7 @@ void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
                }
        }
 
-       free (text);
+       FreeStrBuf (&text);
 
 }