[2947] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[PATCH 2/2] Replace the remaining snprintfs with g_strdup_printf

daemon@ATHENA.MIT.EDU (David Benjamin)
Mon Feb 28 19:17:54 2011

From: David Benjamin <davidben@MIT.EDU>
To: barnowl-dev@mit.edu
Date: Mon, 28 Feb 2011 19:17:25 -0500
In-Reply-To: <1298938645-8001-1-git-send-email-davidben@mit.edu>

Hard-coded buffer sizes are silly.
---
 functions.c |    4 +---
 logging.c   |   33 ++++++++++++++++++++-------------
 zephyr.c    |   15 ++++++++-------
 3 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/functions.c b/functions.c
index 64df043..2863f65 100644
--- a/functions.c
+++ b/functions.c
@@ -2257,7 +2257,6 @@ void owl_function_show_zpunts(void)
 {
   const owl_filter *f;
   const owl_list *fl;
-  char buff[5000];
   char *tmp;
   owl_fmtext fm;
   int i, j;
@@ -2270,8 +2269,7 @@ void owl_function_show_zpunts(void)
 
   for (i=0; i<j; i++) {
     f=owl_list_get_element(fl, i);
-    snprintf(buff, sizeof(buff), "[% 2d] ", i+1);
-    owl_fmtext_append_normal(&fm, buff);
+    owl_fmtext_appendf_normal(&fm, "[% 2d] ", i+1);
     tmp = owl_filter_print(f);
     owl_fmtext_append_normal(&fm, tmp);
     g_free(tmp);
diff --git a/logging.c b/logging.c
index ff48112..8042386 100644
--- a/logging.c
+++ b/logging.c
@@ -125,7 +125,7 @@ void owl_log_append(const owl_message *m, const char *filename) {
 
 void owl_log_outgoing(const owl_message *m)
 {
-  char filename[MAXPATHLEN], *logpath;
+  char *filename, *logpath;
   char *to, *temp;
   GList *cc;
 
@@ -138,9 +138,10 @@ void owl_log_outgoing(const owl_message *m)
     cc = owl_message_get_cc_without_recipient(m);
     while (cc != NULL) {
       temp = short_zuser(cc->data);
-      snprintf(filename, MAXPATHLEN, "%s/%s", logpath, temp);
+      filename = g_strdup_printf("%s/%s", logpath, temp);
       owl_log_append(m, filename);
 
+      g_free(filename);
       g_free(temp);
       g_free(cc->data);
       cc = g_list_delete_link(cc, cc);
@@ -161,20 +162,22 @@ void owl_log_outgoing(const owl_message *m)
     to = g_strdup("loopback");
   }
 
-  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, to);
+  filename = g_strdup_printf("%s/%s", logpath, to);
   owl_log_append(m, filename);
   g_free(to);
+  g_free(filename);
 
-  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
+  filename = g_strdup_printf("%s/all", logpath);
   owl_log_append(m, filename);
   g_free(logpath);
+  g_free(filename);
 }
 
 
 void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
 {
   FILE *file;
-  char filename[MAXPATHLEN], *logpath;
+  char *filename, *logpath;
   char *tobuff;
   owl_message *m;
 
@@ -195,8 +198,9 @@ void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
   /* expand ~ in path names */
   logpath = owl_util_makepath(owl_global_get_logpath(&g));
 
-  snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff);
+  filename = g_strdup_printf("%s/%s", logpath, tobuff);
   file=fopen(filename, "a");
+  g_free(filename);
   if (!file) {
     owl_function_error("Unable to open file for outgoing logging");
     g_free(logpath);
@@ -209,9 +213,10 @@ void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
   }
   fclose(file);
 
-  snprintf(filename, MAXPATHLEN, "%s/all", logpath);
+  filename = g_strdup_printf("%s/all", logpath);
   g_free(logpath);
   file=fopen(filename, "a");
+  g_free(filename);
   if (!file) {
     owl_function_error("Unable to open file for outgoing logging");
     g_free(tobuff);
@@ -228,7 +233,7 @@ void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text)
 
 void owl_log_incoming(const owl_message *m)
 {
-  char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath;
+  char *filename, *allfilename, *logpath;
   const char *from=NULL;
   char *frombuff=NULL;
   int len, ch, i, personal;
@@ -310,16 +315,17 @@ void owl_log_incoming(const owl_message *m)
   /* create the filename (expanding ~ in path names) */
   if (personal) {
     logpath = owl_util_makepath(owl_global_get_logpath(&g));
-    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
-    snprintf(allfilename, MAXPATHLEN, "%s/all", logpath);
+    filename = g_strdup_printf("%s/%s", logpath, from);
+    allfilename = g_strdup_printf("%s/all", logpath);
     owl_log_append(m, allfilename);
-
+    g_free(allfilename);
   } else {
     logpath = owl_util_makepath(owl_global_get_classlogpath(&g));
-    snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from);
+    filename = g_strdup_printf("%s/%s", logpath, from);
   }
 
   owl_log_append(m, filename);
+  g_free(filename);
 
   if (personal && owl_message_is_type_zephyr(m)) {
     /* We want to log to all of the CC'd people who were not us, or
@@ -331,8 +337,9 @@ void owl_log_incoming(const owl_message *m)
     while (cc != NULL) {
       temp = short_zuser(cc->data);
       if (strcasecmp(temp, frombuff) != 0) {
-        snprintf(filename, MAXPATHLEN, "%s/%s", logpath, temp);
+	filename = g_strdup_printf("%s/%s", logpath, temp);
         owl_log_append(m, filename);
+	g_free(filename);
       }
 
       g_free(temp);
diff --git a/zephyr.c b/zephyr.c
index 560bb72..f0bc25b 100644
--- a/zephyr.c
+++ b/zephyr.c
@@ -761,16 +761,16 @@ void owl_zephyr_handle_ack(const ZNotice_t *retnotice)
       }
     }
   } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
-    #define BUFFLEN 1024
     if (retnotice->z_recipient == NULL
         || *retnotice->z_recipient == 0
         || *retnotice->z_recipient == '@') {
-      char buff[BUFFLEN];
+      char *buff;
       owl_function_error("No one subscribed to class %s", retnotice->z_class);
-      snprintf(buff, BUFFLEN, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
+      buff = g_strdup_printf("Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
       owl_function_adminmsg("", buff);
+      g_free(buff);
     } else {
-      char buff[BUFFLEN];
+      char *buff;
       owl_zwrite zw;
       char *realm;
 
@@ -782,20 +782,20 @@ void owl_zephyr_handle_ack(const ZNotice_t *retnotice)
        * terminals or who don't care, not splitting saves a line in the UI
        */
       if(strcasecmp(retnotice->z_class, "message")) {
-        snprintf(buff, BUFFLEN,
+        buff = g_strdup_printf(
                  "Could not send message to %s: "
                  "not logged in or subscribing to class %s, instance %s.\n",
                  tmp,
                  retnotice->z_class,
                  retnotice->z_class_inst);
       } else if(strcasecmp(retnotice->z_class_inst, "personal")) {
-        snprintf(buff, BUFFLEN,
+        buff = g_strdup_printf(
                  "Could not send message to %s: "
                  "not logged in or subscribing to instance %s.\n",
                  tmp,
                  retnotice->z_class_inst);
       } else {
-        snprintf(buff, BUFFLEN,
+        buff = g_strdup_printf(
                  "Could not send message to %s: "
                  "not logged in or subscribing to messages.\n",
                  tmp);
@@ -819,6 +819,7 @@ void owl_zephyr_handle_ack(const ZNotice_t *retnotice)
       owl_log_outgoing_zephyr_error(&zw, buff);
 
       owl_zwrite_cleanup(&zw);
+      g_free(buff);
       g_free(tmp);
     }
   } else {
-- 
1.7.3.3.399.g06ddf


home help back first fref pref prev next nref lref last post