[evolution-data-server] Bug 710787 - Replace most uses of sprintf() with g_snprintf()



commit ad79ca023cb34175920a0c0fbebf3218f2f4e9eb
Author: Murray Cumming <murrayc openismus com>
Date:   Fri Oct 25 20:32:03 2013 -0400

    Bug 710787 - Replace most uses of sprintf() with g_snprintf()
    
    sprintf() can potentially overwrite the buffer, but g_snprintf() takes
    a length to stop it from doing that.
    
    Some of these might instead be replaced by g_strdup_printf().  It is
    hard to know if the use of alloca() and sprintf() was just because
    g_strdup_printf() did not exist yet, or if alloca() was used for
    performance.

 calendar/libecal/e-cal-recur.c              |    6 ++-
 camel/camel-certdb.c                        |    6 ++-
 camel/camel-charset-map.c                   |    2 +-
 camel/camel-data-cache.c                    |    6 ++-
 camel/camel-folder-search.c                 |    4 +-
 camel/camel-iconv.c                         |    6 ++-
 camel/camel-imapx-utils.c                   |    5 ++-
 camel/camel-lock-helper.c                   |    6 ++-
 camel/camel-lock.c                          |   19 +++++--
 camel/camel-mime-parser.c                   |   12 +++--
 camel/camel-text-index.c                    |   77 ++++++++++++++++----------
 camel/providers/local/camel-local-summary.c |    2 +-
 camel/providers/local/camel-mbox-store.c    |   10 ++--
 camel/providers/local/camel-mbox-summary.c  |    6 ++-
 camel/providers/local/camel-mh-store.c      |   26 ++++++---
 camel/providers/local/camel-spool-store.c   |    6 ++-
 camel/providers/local/camel-spool-summary.c |    2 +-
 camel/providers/nntp/camel-nntp-store.c     |    6 ++-
 camel/providers/pop3/camel-pop3-store.c     |   10 +++-
 camel/tests/message/test2.c                 |    4 +-
 camel/tests/mime-filter/test-tohtml.c       |    4 +-
 libedataserver/e-source-camel.c             |   12 +++--
 22 files changed, 153 insertions(+), 84 deletions(-)
---
diff --git a/calendar/libecal/e-cal-recur.c b/calendar/libecal/e-cal-recur.c
index 711de27..306ac7f 100644
--- a/calendar/libecal/e-cal-recur.c
+++ b/calendar/libecal/e-cal-recur.c
@@ -3810,11 +3810,13 @@ cal_obj_time_to_string (CalObjTime *cotime)
 
        weekday = cal_obj_time_weekday (cotime);
 
-       sprintf (
-               buffer, "%s %02i/%02i/%04i %02i:%02i:%02i",
+       g_snprintf (
+               buffer, sizeof (buffer),
+               "%s %02i/%02i/%04i %02i:%02i:%02i",
                weekdays[weekday],
                cotime->day, cotime->month + 1, cotime->year,
                cotime->hour, cotime->minute, cotime->second);
+
        return buffer;
 }
 #endif
diff --git a/camel/camel-certdb.c b/camel/camel-certdb.c
index 28ef193..750fae3 100644
--- a/camel/camel-certdb.c
+++ b/camel/camel-certdb.c
@@ -426,6 +426,7 @@ camel_certdb_save (CamelCertDB *certdb)
        CamelCertDBClass *class;
        CamelCert *cert;
        gchar *filename;
+       gsize filename_len;
        gint fd, i;
        FILE *out;
 
@@ -436,8 +437,9 @@ camel_certdb_save (CamelCertDB *certdb)
        if (!certdb->priv->dirty)
                return 0;
 
-       filename = alloca (strlen (certdb->priv->filename) + 4);
-       sprintf (filename, "%s~", certdb->priv->filename);
+       filename_len = strlen (certdb->priv->filename) + 4;
+       filename = alloca (filename_len);
+       g_snprintf (filename, filename_len, "%s~", certdb->priv->filename);
 
        fd = g_open (filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
        if (fd == -1)
diff --git a/camel/camel-charset-map.c b/camel/camel-charset-map.c
index 145dffb..7f26577 100644
--- a/camel/camel-charset-map.c
+++ b/camel/camel-charset-map.c
@@ -226,7 +226,7 @@ gint main (gint argc, gchar **argv)
                        if (!has_bits)
                                continue;
 
-                       sprintf (name, "m%02x%x", i, k);
+                       g_snprintf (name, sizeof (name), "m%02x%x", i, k);
 
                        if ((alias = g_hash_table_lookup (table_hash, block))) {
                                /* this block is identical to an earlier block, just alias it */
diff --git a/camel/camel-data-cache.c b/camel/camel-data-cache.c
index 800f897..08bad33 100644
--- a/camel/camel-data-cache.c
+++ b/camel/camel-data-cache.c
@@ -324,12 +324,14 @@ data_cache_path (CamelDataCache *cdc,
                  const gchar *key)
 {
        gchar *dir, *real, *tmp;
+       gsize dir_len;
        guint32 hash;
 
        hash = g_str_hash (key);
        hash = (hash >> 5) &CAMEL_DATA_CACHE_MASK;
-       dir = alloca (strlen (cdc->priv->path) + strlen (path) + 8);
-       sprintf (dir, "%s/%s/%02x", cdc->priv->path, path, hash);
+       dir_len = strlen (cdc->priv->path) + strlen (path) + 8;
+       dir = alloca (dir_len);
+       g_snprintf (dir, dir_len, "%s/%s/%02x", cdc->priv->path, path, hash);
 
        if (g_access (dir, F_OK) == -1) {
                if (create)
diff --git a/camel/camel-folder-search.c b/camel/camel-folder-search.c
index 8e44872..d991a18 100644
--- a/camel/camel-folder-search.c
+++ b/camel/camel-folder-search.c
@@ -300,7 +300,9 @@ check_header (CamelSExp *sexp,
                        header = camel_message_info_subject (search->current);
                } else if (!g_ascii_strcasecmp (headername, "date")) {
                        /* FIXME: not a very useful form of the date */
-                       sprintf (strbuf, "%d", (gint) camel_message_info_date_sent (search->current));
+                       g_snprintf (
+                               strbuf, sizeof (strbuf), "%d",
+                               (gint) camel_message_info_date_sent (search->current));
                        header = strbuf;
                } else if (!g_ascii_strcasecmp (headername, "from")) {
                        header = camel_message_info_from (search->current);
diff --git a/camel/camel-iconv.c b/camel/camel-iconv.c
index f7de2e9..3cbf712 100644
--- a/camel/camel-iconv.c
+++ b/camel/camel-iconv.c
@@ -379,6 +379,7 @@ camel_iconv_open (const gchar *oto,
 {
        const gchar *to, *from;
        gchar *tofrom;
+       gsize tofrom_len;
        struct _iconv_cache *ic;
        struct _iconv_cache_node *in;
        gint errnosav;
@@ -391,8 +392,9 @@ camel_iconv_open (const gchar *oto,
 
        to = camel_iconv_charset_name (oto);
        from = camel_iconv_charset_name (ofrom);
-       tofrom = g_alloca (strlen (to) + strlen (from) + 2);
-       sprintf (tofrom, "%s%%%s", to, from);
+       tofrom_len = strlen (to) + strlen (from) + 2;
+       tofrom = g_alloca (tofrom_len);
+       g_snprintf (tofrom, tofrom_len, "%s%%%s", to, from);
 
        G_LOCK (iconv);
 
diff --git a/camel/camel-imapx-utils.c b/camel/camel-imapx-utils.c
index e7d2acf..2792f36 100644
--- a/camel/camel-imapx-utils.c
+++ b/camel/camel-imapx-utils.c
@@ -2479,7 +2479,10 @@ camel_imapx_command_add_qresync_parameter (CamelIMAPXCommand *ic,
 
                        /* IMAP sequence numbers are 1-based,
                         * but our folder summary is 0-based. */
-                       sprintf (buf, "%" G_GUINT32_FORMAT, summary_index + 1);
+                       g_snprintf (
+                               buf, sizeof (buf),
+                               "%" G_GUINT32_FORMAT,
+                               summary_index + 1);
 
                        uid = camel_imapx_dup_uid_from_summary_index (
                                folder, summary_index);
diff --git a/camel/camel-lock-helper.c b/camel/camel-lock-helper.c
index bb59515..9efe35b 100644
--- a/camel/camel-lock-helper.c
+++ b/camel/camel-lock-helper.c
@@ -241,11 +241,13 @@ static void
 lock_touch (const gchar *path)
 {
        gchar *name;
+       gsize name_len;
 
        /* we could also check that we haven't had our lock stolen from us here */
 
-       name = alloca (strlen (path) + 10);
-       sprintf (name, "%s.lock", path);
+       name_len = strlen (path) + 10;
+       name = alloca (name_len);
+       g_snprintf (name, name_len, "%s.lock", path);
 
        d (fprintf (stderr, "Updating lock %s\n", name));
        utime (name, NULL);
diff --git a/camel/camel-lock.c b/camel/camel-lock.c
index 628a91a..6cf3578 100644
--- a/camel/camel-lock.c
+++ b/camel/camel-lock.c
@@ -74,6 +74,8 @@ camel_lock_dot (const gchar *path,
 {
 #ifdef USE_DOT
        gchar *locktmp, *lock;
+       gsize lock_len = 0;
+       gsize locktmp_len = 0;
        gint retry = 0;
        gint fdtmp;
        struct stat st;
@@ -82,9 +84,11 @@ camel_lock_dot (const gchar *path,
         * Does it matter?  We will normally also use fcntl too ... */
 
        /* use alloca, save cleaning up afterwards */
-       lock = alloca (strlen (path) + strlen (".lock") + 1);
-       sprintf (lock, "%s.lock", path);
-       locktmp = alloca (strlen (path) + strlen ("XXXXXX") + 1);
+       lock_len = strlen (path) + strlen (".lock") + 1;
+       lock = alloca (lock_len);
+       g_snprintf (lock, lock_len, "%s.lock", path);
+       locktmp_len = strlen (path) + strlen ("XXXXXX") + 1;
+       locktmp = alloca (locktmp_len);
 
        while (retry < CAMEL_LOCK_DOT_RETRY) {
 
@@ -93,7 +97,8 @@ camel_lock_dot (const gchar *path,
                if (retry > 0)
                        sleep (CAMEL_LOCK_DOT_DELAY);
 
-               sprintf (locktmp, "%sXXXXXX", path);
+
+               g_snprintf (locktmp, locktmp_len, "%sXXXXXX", path);
                fdtmp = g_mkstemp (locktmp);
                if (fdtmp == -1) {
                        g_set_error (
@@ -161,9 +166,11 @@ camel_unlock_dot (const gchar *path)
 {
 #ifdef USE_DOT
        gchar *lock;
+       gsize lock_len;
 
-       lock = alloca (strlen (path) + strlen (".lock") + 1);
-       sprintf (lock, "%s.lock", path);
+       lock_len = strlen (path) + strlen (".lock") + 1;
+       lock = alloca (lock_len);
+       g_snprintf (lock, lock_len, "%s.lock", path);
        d (printf ("unlocking %s\n", lock));
        (void) unlink (lock);
 #endif
diff --git a/camel/camel-mime-parser.c b/camel/camel-mime-parser.c
index c491a78..019125a 100644
--- a/camel/camel-mime-parser.c
+++ b/camel/camel-mime-parser.c
@@ -843,12 +843,14 @@ camel_mime_parser_push_state (CamelMimeParser *mp,
 {
        struct _header_scan_stack *h;
        struct _header_scan_state *s = _PRIVATE (mp);
+       gsize boundary_len;
 
        h = g_malloc0 (sizeof (*h));
        h->boundarylen = strlen (boundary) + 2;
        h->boundarylenfinal = h->boundarylen + 2;
-       h->boundary = g_malloc (h->boundarylen + 3);
-       sprintf (h->boundary, "--%s--", boundary);
+       boundary_len = h->boundarylen + 3;
+       h->boundary = g_malloc (boundary_len);
+       g_snprintf (h->boundary, boundary_len, "--%s--", boundary);
        folder_push_part (s, h);
        s->state = newstate;
 }
@@ -1567,6 +1569,7 @@ folder_scan_step (struct _header_scan_state *s,
        CamelContentType *ct = NULL;
        struct _header_scan_filter *f;
        gsize presize;
+       gulong boundary_len;
 
 /*     printf("\nSCAN PASS: state = %d '%s'\n", s->state, states[s->state]);*/
 
@@ -1654,8 +1657,9 @@ tail_recurse:
                                        d (printf ("multipart, boundary = %s\n", bound));
                                        h->boundarylen = strlen (bound) + 2;
                                        h->boundarylenfinal = h->boundarylen + 2;
-                                       h->boundary = g_malloc (h->boundarylen + 3);
-                                       sprintf (h->boundary, "--%s--", bound);
+                                       boundary_len = h->boundarylen + 3;
+                                       h->boundary = g_malloc (boundary_len);
+                                       g_snprintf (h->boundary, boundary_len, "--%s--", bound);
                                        type = CAMEL_MIME_PARSER_STATE_MULTIPART;
                                } else {
                                        /*camel_content_type_unref(ct);
diff --git a/camel/camel-text-index.c b/camel/camel-text-index.c
index 1cc5a79..23f02c5 100644
--- a/camel/camel-text-index.c
+++ b/camel/camel-text-index.c
@@ -401,7 +401,10 @@ text_index_sync (CamelIndex *idx)
        return ret;
 }
 
-static void tmp_name (const gchar *in, gchar *o)
+static void
+tmp_name (const gchar *in,
+          gchar *o,
+          gsize o_len)
 {
        gchar *s;
 
@@ -411,7 +414,7 @@ static void tmp_name (const gchar *in, gchar *o)
                memcpy (o + (s - in + 1), ".#", 2);
                strcpy (o + (s - in + 3), s + 1);
        } else {
-               sprintf (o, ".#%s", in);
+               g_snprintf (o, o_len, ".#%s", in);
        }
 }
 
@@ -457,8 +460,8 @@ text_index_compress_nosync (CamelIndex *idx)
        strcpy (oldpath, idx->path);
        oldpath[strlen (oldpath) - strlen (".index")] = 0;
 
-       tmp_name (oldpath, newpath);
-       sprintf (savepath, "%s~", oldpath);
+       tmp_name (oldpath, newpath, i);
+       g_snprintf (savepath, i, "%s~", oldpath);
 
        d (printf ("Old index: %s\n", idx->path));
        d (printf ("Old path: %s\n", oldpath));
@@ -600,9 +603,9 @@ fail:
        g_hash_table_destroy (remap);
 
        /* clean up temp files always */
-       sprintf (savepath, "%s~.index", oldpath);
+       g_snprintf (savepath, i, "%s~.index", oldpath);
        g_unlink (savepath);
-       sprintf (newpath, "%s.data", savepath);
+       g_snprintf (newpath, i, "%s.data", savepath);
        g_unlink (newpath);
 
        return ret;
@@ -628,20 +631,23 @@ text_index_rename (CamelIndex *idx,
 {
        CamelTextIndexPrivate *p = CAMEL_TEXT_INDEX_GET_PRIVATE (idx);
        gchar *newlink, *newblock;
+       gsize newlink_len, newblock_len;
        gint err, ret;
 
        CAMEL_TEXT_INDEX_LOCK (idx, lock);
 
-       newblock = alloca (strlen (path) + 8);
-       sprintf (newblock, "%s.index", path);
+       newblock_len = strlen (path) + 8;
+       newblock = alloca (newblock_len);
+       g_snprintf (newblock, newblock_len, "%s.index", path);
        ret = camel_block_file_rename (p->blocks, newblock);
        if (ret == -1) {
                CAMEL_TEXT_INDEX_UNLOCK (idx, lock);
                return -1;
        }
 
-       newlink = alloca (strlen (path) + 16);
-       sprintf (newlink, "%s.index.data", path);
+       newlink_len = strlen (path) + 16;
+       newlink = alloca (newlink_len);
+       g_snprintf (newlink, newlink_len, "%s.index.data", path);
        ret = camel_key_file_rename (p->links, newlink);
        if (ret == -1) {
                err = errno;
@@ -875,6 +881,7 @@ camel_text_index_new (const gchar *path,
        CamelTextIndexPrivate *p = CAMEL_TEXT_INDEX_GET_PRIVATE (idx);
        struct _CamelTextIndexRoot *rb;
        gchar *link;
+       gsize link_len;
        CamelBlock *bl;
 
        camel_index_construct ((CamelIndex *) idx, path, flags);
@@ -885,8 +892,9 @@ camel_text_index_new (const gchar *path,
        if (p->blocks == NULL)
                goto fail;
 
-       link = alloca (strlen (idx->parent.path) + 7);
-       sprintf (link, "%s.data", idx->parent.path);
+       link_len = strlen (idx->parent.path) + 7;
+       link = alloca (link_len);
+       g_snprintf (link, link_len, "%s.data", idx->parent.path);
        p->links = camel_key_file_new (link, flags, CAMEL_TEXT_INDEX_KEY_VERSION);
 
        if (p->links == NULL)
@@ -961,18 +969,21 @@ gint
 camel_text_index_check (const gchar *path)
 {
        gchar *block, *key;
+       gsize block_len, key_len;
        CamelBlockFile *blocks;
        CamelKeyFile *keys;
 
-       block = alloca (strlen (path) + 7);
-       sprintf (block, "%s.index", path);
+       block_len = strlen (path) + 7;
+       block = alloca (block_len);
+       g_snprintf (block, block_len, "%s.index", path);
        blocks = camel_block_file_new (block, O_RDONLY, CAMEL_TEXT_INDEX_VERSION, CAMEL_BLOCK_SIZE);
        if (blocks == NULL) {
                io (printf ("Check failed: No block file: %s\n", g_strerror (errno)));
                return -1;
        }
-       key = alloca (strlen (path) + 12);
-       sprintf (key, "%s.index.data", path);
+       key_len = strlen (path) + 12;
+       key = alloca (key_len);
+       g_snprintf (key, key_len, "%s.index.data", path);
        keys = camel_key_file_new (key, O_RDONLY, CAMEL_TEXT_INDEX_KEY_VERSION);
        if (keys == NULL) {
                io (printf ("Check failed: No key file: %s\n", g_strerror (errno)));
@@ -991,25 +1002,28 @@ camel_text_index_rename (const gchar *old,
                          const gchar *new)
 {
        gchar *oldname, *newname;
+       gsize oldname_len, newname_len;
        gint err;
 
        /* TODO: camel_text_index_rename should find out if we have an active index and use that instead */
 
-       oldname = alloca (strlen (old) + 12);
-       newname = alloca (strlen (new) + 12);
-       sprintf (oldname, "%s.index", old);
-       sprintf (newname, "%s.index", new);
+       oldname_len = strlen (old) + 12;
+       newname_len = strlen (new) + 12;
+       oldname = alloca (oldname_len);
+       newname = alloca (newname_len);
+       g_snprintf (oldname, oldname_len, "%s.index", old);
+       g_snprintf (newname, newname_len, "%s.index", new);
 
        if (g_rename (oldname, newname) == -1 && errno != ENOENT)
                return -1;
 
-       sprintf (oldname, "%s.index.data", old);
-       sprintf (newname, "%s.index.data", new);
+       g_snprintf (oldname, oldname_len, "%s.index.data", old);
+       g_snprintf (newname, newname_len, "%s.index.data", new);
 
        if (g_rename (oldname, newname) == -1 && errno != ENOENT) {
                err = errno;
-               sprintf (oldname, "%s.index", old);
-               sprintf (newname, "%s.index", new);
+               g_snprintf (oldname, oldname_len, "%s.index", old);
+               g_snprintf (newname, newname_len, "%s.index", new);
                g_rename (newname, oldname);
                errno = err;
                return -1;
@@ -1022,14 +1036,17 @@ gint
 camel_text_index_remove (const gchar *old)
 {
        gchar *block, *key;
+       gsize block_len, key_len;
        gint ret = 0;
 
        /* TODO: needs to poke any active indices to remain unlinked */
 
-       block = alloca (strlen (old) + 12);
-       key = alloca (strlen (old) + 12);
-       sprintf (block, "%s.index", old);
-       sprintf (key, "%s.index.data", old);
+       block_len = strlen (old) + 12;
+       block = alloca (block_len);
+       key_len = strlen (old) + 12;
+       key = alloca (key_len);
+       g_snprintf (block, block_len, "%s.index", old);
+       g_snprintf (key, key_len, "%s.index.data", old);
 
        if (g_unlink (block) == -1 && errno != ENOENT && errno != ENOTDIR)
                ret = -1;
@@ -1225,7 +1242,7 @@ dump_raw (GHashTable *map,
                len = 1024;
                p = buf;
                do {
-                       sprintf (line, "%08x:                                                                 
     ", total);
+                       g_snprintf (line, sizeof (line), "%08x:                                               
                       ", total);
                        total += 16;
                        o = line + 10;
                        a = o + 16 * 2 + 2;
@@ -1868,7 +1885,7 @@ main (gint argc,
        for (i = 0; i < 100; i++) {
                gchar name[16];
 
-               sprintf (name, "%d", i);
+               g_snprintf (name, sizeof (name), "%d", i);
                printf ("Adding words to name '%s'\n", name);
                idn = camel_index_add_name (idx, name);
                camel_index_name_add_buffer (idn, wordbuffer, sizeof (wordbuffer) - 1);
diff --git a/camel/providers/local/camel-local-summary.c b/camel/providers/local/camel-local-summary.c
index 0b84dc8..14a5d78 100644
--- a/camel/providers/local/camel-local-summary.c
+++ b/camel/providers/local/camel-local-summary.c
@@ -646,7 +646,7 @@ local_summary_decode_x_evolution (CamelLocalSummary *cls,
        if (header && strlen (header) == strlen ("00000000-0000")
            && sscanf (header, "%08x-%04x", &uid, &flags) == 2) {
                if (mi)
-                       sprintf (uidstr, "%u", uid);
+                       g_snprintf (uidstr, sizeof (uidstr), "%u", uid);
        } else {
                g_free (header);
                return -1;
diff --git a/camel/providers/local/camel-mbox-store.c b/camel/providers/local/camel-mbox-store.c
index cc84ad7..2576afe 100644
--- a/camel/providers/local/camel-mbox-store.c
+++ b/camel/providers/local/camel-mbox-store.c
@@ -979,15 +979,17 @@ mbox_store_get_meta_path (CamelLocalStore *ls,
 /*#define USE_HIDDEN_META_FILES*/
 #ifdef USE_HIDDEN_META_FILES
        gchar *name, *slash;
+       gsize name_len;
 
-       name = g_alloca (strlen (full_name) + strlen (ext) + 2);
+       name_len = strlen (full_name) + strlen (ext) + 2;
+       name = g_alloca (name_len);
        if ((slash = strrchr (full_name, '/')))
-               sprintf (
-                       name, "%.*s.%s%s",
+               g_snprintf (
+                       name, name_len, "%.*s.%s%s",
                        slash - full_name + 1,
                        full_name, slash + 1, ext);
        else
-               sprintf (name, ".%s%s", full_name, ext);
+               g_snprintf (name, name_len, ".%s%s", full_name, ext);
 
        return mbox_store_get_full_path (ls, name);
 #else
diff --git a/camel/providers/local/camel-mbox-summary.c b/camel/providers/local/camel-mbox-summary.c
index 8f4f361..ee35107 100644
--- a/camel/providers/local/camel-mbox-summary.c
+++ b/camel/providers/local/camel-mbox-summary.c
@@ -657,6 +657,7 @@ mbox_summary_sync_full (CamelMboxSummary *mbs,
        CamelFolderSummary *s = CAMEL_FOLDER_SUMMARY (mbs);
        gint fd = -1, fdout = -1;
        gchar *tmpname = NULL;
+       gsize tmpname_len = 0;
        guint32 flags = (expunge ? 1 : 0), filemode = 0600;
        struct stat st;
 
@@ -681,8 +682,9 @@ mbox_summary_sync_full (CamelMboxSummary *mbs,
                return -1;
        }
 
-       tmpname = g_alloca (strlen (cls->folder_path) + 5);
-       sprintf (tmpname, "%s.tmp", cls->folder_path);
+       tmpname_len = strlen (cls->folder_path) + 5;
+       tmpname = g_alloca (tmpname_len);
+       g_snprintf (tmpname, tmpname_len, "%s.tmp", cls->folder_path);
        d (printf ("Writing temporary file to %s\n", tmpname));
        fdout = g_open (tmpname, O_LARGEFILE | O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, filemode);
        if (fdout == -1) {
diff --git a/camel/providers/local/camel-mh-store.c b/camel/providers/local/camel-mh-store.c
index dbf0054..c81e9c1 100644
--- a/camel/providers/local/camel-mh-store.c
+++ b/camel/providers/local/camel-mh-store.c
@@ -57,22 +57,28 @@ folders_update (const gchar *root,
                 GCancellable *cancellable)
 {
        gchar *tmp, *tmpnew, *line = NULL;
+       gsize tmpnew_len = 0;
        CamelStream *stream, *in = NULL, *out = NULL;
        gchar *folder_newline;
        gint flen = strlen (folder);
 
        folder_newline = g_strdup_printf ("%s\n", folder);
 
-       tmpnew = g_alloca (strlen (root) + 16);
-       sprintf (tmpnew, "%s" G_DIR_SEPARATOR_S ".folders~", root);
+       tmpnew_len = strlen (root) + 16;
+       tmpnew = g_alloca (tmpnew_len);
+       g_snprintf (
+               tmpnew, tmpnew_len,
+               "%s" G_DIR_SEPARATOR_S ".folders~", root);
 
        out = camel_stream_fs_new_with_name (
                tmpnew, O_WRONLY | O_CREAT | O_TRUNC, 0666, NULL);
        if (out == NULL)
                goto fail;
 
-       tmp = g_alloca (strlen (root) + 16);
-       sprintf (tmp, "%s" G_DIR_SEPARATOR_S ".folders", root);
+       tmp = g_alloca (tmpnew_len);
+       g_snprintf (
+               tmp, tmpnew_len,
+               "%s" G_DIR_SEPARATOR_S ".folders", root);
        stream = camel_stream_fs_new_with_name (tmp, O_RDONLY, 0, NULL);
        if (stream) {
                in = camel_stream_buffer_new (stream, CAMEL_STREAM_BUFFER_READ);
@@ -280,6 +286,7 @@ recursive_scan (CamelStore *store,
                 GCancellable *cancellable)
 {
        gchar *fullpath, *tmp;
+       gsize fullpath_len;
        DIR *dp;
        struct dirent *d;
        struct stat st;
@@ -288,8 +295,9 @@ recursive_scan (CamelStore *store,
 
        /* Open the specified directory. */
        if (path[0]) {
-               fullpath = alloca (strlen (root) + strlen (path) + 2);
-               sprintf (fullpath, "%s/%s", root, path);
+               fullpath_len = strlen (root) + strlen (path) + 2;
+               fullpath = alloca (fullpath_len);
+               g_snprintf (fullpath, fullpath_len, "%s/%s", root, path);
        } else
                fullpath = (gchar *) root;
 
@@ -361,14 +369,16 @@ folders_scan (CamelStore *store,
 {
        CamelFolderInfo *fi;
        gchar  line[512], *path, *tmp;
+       gsize tmp_len;
        CamelStream *stream, *in;
        struct stat st;
        GPtrArray *folders;
        GHashTable *visited;
        gint len;
 
-       tmp = g_alloca (strlen (root) + 16);
-       sprintf (tmp, "%s/.folders", root);
+       tmp_len = strlen (root) + 16;
+       tmp = g_alloca (tmp_len);
+       g_snprintf (tmp, tmp_len, "%s/.folders", root);
        stream = camel_stream_fs_new_with_name (tmp, 0, O_RDONLY, NULL);
        if (stream == NULL)
                return;
diff --git a/camel/providers/local/camel-spool-store.c b/camel/providers/local/camel-spool-store.c
index f434745..1e82a78 100644
--- a/camel/providers/local/camel-spool-store.c
+++ b/camel/providers/local/camel-spool-store.c
@@ -190,6 +190,7 @@ scan_dir (CamelStore *store,
        DIR *dir;
        struct dirent *d;
        gchar *name, *tmp, *fname;
+       gsize name_len;
        CamelFolderInfo *fi = NULL;
        struct stat st;
        CamelFolder *folder;
@@ -200,8 +201,9 @@ scan_dir (CamelStore *store,
 
        /* look for folders matching the right structure, recursively */
        if (path) {
-               name = alloca (strlen (root) + strlen (path) + 2);
-               sprintf (name, "%s/%s", root, path);
+               name_len = strlen (root) + strlen (path) + 2;
+               name = alloca (name_len);
+               g_snprintf (name, name_len, "%s/%s", root, path);
        } else
                name = (gchar *) root;  /* XXX casting away const */
 
diff --git a/camel/providers/local/camel-spool-summary.c b/camel/providers/local/camel-spool-summary.c
index 3f0697b..cfb4eb0 100644
--- a/camel/providers/local/camel-spool-summary.c
+++ b/camel/providers/local/camel-spool-summary.c
@@ -152,7 +152,7 @@ spool_summary_sync_full (CamelMboxSummary *cls,
                return -1;
        }
 
-       sprintf (tmpname, "/tmp/spool.camel.XXXXXX");
+       g_snprintf (tmpname, sizeof (tmpname), "/tmp/spool.camel.XXXXXX");
        fdout = g_mkstemp (tmpname);
 
        d (printf ("Writing tmp file to %s\n", tmpname));
diff --git a/camel/providers/nntp/camel-nntp-store.c b/camel/providers/nntp/camel-nntp-store.c
index 7e3ea07..9063110 100644
--- a/camel/providers/nntp/camel-nntp-store.c
+++ b/camel/providers/nntp/camel-nntp-store.c
@@ -744,6 +744,7 @@ nntp_store_info_update (CamelNNTPStore *nntp_store,
        CamelStoreSummary *store_summary;
        CamelNNTPStoreInfo *si, *fsi;
        gchar *relpath, *tmp;
+       gsize relpath_len = 0;
        guint32 last = 0, first = 0, new = 0;
 
        tmp = strchr (line, ' ');
@@ -759,8 +760,9 @@ nntp_store_info_update (CamelNNTPStore *nntp_store,
                si = (CamelNNTPStoreInfo *)
                        camel_store_summary_info_new (store_summary);
 
-               relpath = g_alloca (strlen (line) + 2);
-               sprintf (relpath, "/%s", line);
+               relpath_len = strlen (line) + 2;
+               relpath = g_alloca (relpath_len);
+               g_snprintf (relpath, relpath_len, "/%s", line);
 
                si->info.path = g_strdup (line);
                si->full_name = g_strdup (line); /* why do we keep this? */
diff --git a/camel/providers/pop3/camel-pop3-store.c b/camel/providers/pop3/camel-pop3-store.c
index b62414c..7ef41c8 100644
--- a/camel/providers/pop3/camel-pop3-store.c
+++ b/camel/providers/pop3/camel-pop3-store.c
@@ -680,6 +680,7 @@ pop3_store_authenticate_sync (CamelService *service,
 
        } else if (strcmp (mechanism, "+APOP") == 0 && pop3_engine->apop) {
                gchar *secret, *md5asc, *d;
+               gsize secret_len;
 
                if (password == NULL) {
                        g_set_error_literal (
@@ -710,10 +711,13 @@ pop3_store_authenticate_sync (CamelService *service,
                        d++;
                }
 
-               secret = g_alloca (
+               secret_len =
                        strlen (pop3_engine->apop) +
-                       strlen (password) + 1);
-               sprintf (secret, "%s%s", pop3_engine->apop, password);
+                       strlen (password) + 1;
+               secret = g_alloca (secret_len);
+               g_snprintf (
+                       secret, secret_len, "%s%s",
+                       pop3_engine->apop, password);
                md5asc = g_compute_checksum_for_string (
                        G_CHECKSUM_MD5, secret, -1);
                pcp = camel_pop3_engine_command_new (
diff --git a/camel/tests/message/test2.c b/camel/tests/message/test2.c
index 6456d2d..7360f64 100644
--- a/camel/tests/message/test2.c
+++ b/camel/tests/message/test2.c
@@ -142,8 +142,8 @@ gint main (gint argc, gchar **argv)
        push ("Test add many");
        for (i = 1; i < 10; i++) {
                gchar name[16], a[32];
-               sprintf (name, "Zed %d", i);
-               sprintf (a, "nowhere here-%d com au", i);
+               g_snprintf (name, sizeof (name), "Zed %d", i);
+               g_snprintf (a, sizeof (a), "nowhere here-%d com au", i);
                camel_internet_address_add (addr, name, a);
                check (camel_address_length (CAMEL_ADDRESS (addr)) == i + 1);
                check (camel_internet_address_get (addr, i, &real, &where) == TRUE);
diff --git a/camel/tests/mime-filter/test-tohtml.c b/camel/tests/mime-filter/test-tohtml.c
index 27b9fdd..50f1774 100644
--- a/camel/tests/mime-filter/test-tohtml.c
+++ b/camel/tests/mime-filter/test-tohtml.c
@@ -155,8 +155,8 @@ main (gint argc,
                GFile *outfile;
                struct stat st;
 
-               sprintf (inname, "data/html.%d.in", i);
-               sprintf (outname, "data/html.%d.out", i);
+               g_snprintf (inname, sizeof (inname), "data/html.%d.in", i);
+               g_snprintf (outname, sizeof (outname), "data/html.%d.out", i);
 
                if (g_stat (inname, &st) == -1)
                        break;
diff --git a/libedataserver/e-source-camel.c b/libedataserver/e-source-camel.c
index 8f7b96d..73fd892 100644
--- a/libedataserver/e-source-camel.c
+++ b/libedataserver/e-source-camel.c
@@ -671,11 +671,13 @@ const gchar *
 e_source_camel_get_type_name (const gchar *protocol)
 {
        gchar *buffer;
+       gsize buffer_len;
 
        g_return_val_if_fail (protocol != NULL, NULL);
 
-       buffer = g_alloca (strlen (protocol) + 16);
-       g_sprintf (buffer, "ESourceCamel%s", protocol);
+       buffer_len = strlen (protocol) + 16;
+       buffer = g_alloca (buffer_len);
+       g_snprintf (buffer, buffer_len, "ESourceCamel%s", protocol);
        buffer[12] = g_ascii_toupper (buffer[12]);
 
        return g_intern_string (buffer);
@@ -700,13 +702,15 @@ const gchar *
 e_source_camel_get_extension_name (const gchar *protocol)
 {
        gchar *buffer;
+       gsize buffer_len;
 
        g_return_val_if_fail (protocol != NULL, NULL);
 
        /* Use the term "backend" for consistency with other
         * calendar and address book backend extension names. */
-       buffer = g_alloca (strlen (protocol) + 16);
-       g_sprintf (buffer, "%s Backend", protocol);
+       buffer_len = strlen (protocol) + 16;
+       buffer = g_alloca (buffer_len);
+       g_snprintf (buffer, buffer_len, "%s Backend", protocol);
        buffer[0] = g_ascii_toupper (buffer[0]);
 
        return g_intern_string (buffer);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]