[2963] in BarnOwl Developers

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

[PATCH] Punt a number of g_new NULL checks, various minor cleanups

daemon@ATHENA.MIT.EDU (David Benjamin)
Fri Mar 11 00:27:57 2011

From: David Benjamin <davidben@MIT.EDU>
To: barnowl-dev@mit.edu
Date: Fri, 11 Mar 2011 00:27:33 -0500

No real need to check g_new's return value as it always succeeds. (Or
destroys the universe^Wprocess in embarrassment.) Also make owl_global
manage the lifetime of the kill buffer since it's part of owl_global
now. And replace a g_new/strncpy with a g_strndup. It's shorter.
---
 editwin.c |   21 ++++-----------------
 global.c  |    7 ++++---
 keymap.c  |    1 -
 popexec.c |    1 -
 4 files changed, 8 insertions(+), 22 deletions(-)

diff --git a/editwin.c b/editwin.c
index 9af78e8..c22d595 100644
--- a/editwin.c
+++ b/editwin.c
@@ -300,9 +300,7 @@ void owl_editwin_clear(owl_editwin *e)
   char echochar=e->echochar;
 
   if (lock > 0) {
-    locktext = g_new(char, lock+1);
-    strncpy(locktext, e->buff, lock);
-    locktext[lock] = 0;
+    locktext = g_strndup(e->buff, lock);
   }
 
   g_free(e->buff);
@@ -1058,7 +1056,7 @@ void owl_editwin_delete_to_endofline(owl_editwin *e)
 
 void owl_editwin_yank(owl_editwin *e)
 {
-  char *killbuf = owl_global_get_kill_buffer(&g);
+  const char *killbuf = owl_global_get_kill_buffer(&g);
 
   if (killbuf != NULL)
     owl_editwin_replace(e, 0, killbuf);
@@ -1066,19 +1064,8 @@ void owl_editwin_yank(owl_editwin *e)
 
 static const char *oe_copy_buf(owl_editwin *e, const char *buf, int len)
 {
-  char *p;
-  char *killbuf = owl_global_get_kill_buffer(&g);
-
-  p = g_new(char, len + 1);
-
-  if (p != NULL) {
-    g_free(killbuf);
-    memcpy(p, buf, len);
-    p[len] = 0;
-    owl_global_set_kill_buffer(&g,p);
-  }
-
-  return p;
+  owl_global_set_kill_buffer(&g, buf, len);
+  return owl_global_get_kill_buffer(&g);
 }
 
 static int oe_copy_region(owl_editwin *e)
diff --git a/global.c b/global.c
index 86d6299..416ae96 100644
--- a/global.c
+++ b/global.c
@@ -958,10 +958,11 @@ FILE *owl_global_get_debug_file_handle(owl_global *g) {
   return g->debug_file;
 }
 
-char *owl_global_get_kill_buffer(owl_global *g) {
+const char *owl_global_get_kill_buffer(owl_global *g) {
   return g->kill_buffer;
 }
 
-void owl_global_set_kill_buffer(owl_global *g,char *kill) {
-  g->kill_buffer = kill;
+void owl_global_set_kill_buffer(owl_global *g, const char *kill, int len) {
+  g_free(g->kill_buffer);
+  g->kill_buffer = g_strndup(kill, len);
 }
diff --git a/keymap.c b/keymap.c
index 176ad47..adc5f5a 100644
--- a/keymap.c
+++ b/keymap.c
@@ -196,7 +196,6 @@ owl_keymap *owl_keyhandler_create_and_add_keymap(owl_keyhandler *kh, const char
 {
   owl_keymap *km;
   km = g_new(owl_keymap, 1);
-  if (!km) return NULL;
   owl_keymap_init(km, name, desc, default_fn, prealways_fn, postalways_fn);
   owl_keyhandler_add_keymap(kh, km);
   return km;
diff --git a/popexec.c b/popexec.c
index fe230ee..4711add 100644
--- a/popexec.c
+++ b/popexec.c
@@ -22,7 +22,6 @@ owl_popexec *owl_popexec_new(const char *command)
   }
 
   pe = g_new(owl_popexec, 1);
-  if (!pe) return NULL;
   pe->winactive=0;
   pe->pid=0;
   pe->refcount=0;
-- 
1.7.3.3.399.g06ddf


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