From 1eb23d49880d4f24147ffc0a40d3d7ed291e8065 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Wed, 4 Jun 2014 16:30:11 +0000
Subject: [PATCH] Making use of the macros charalloc() and charealloc(), making
 use of null_at(), adding a cast, and using an unsigned type for a length.
 Patch by David Lawrence Ramsey.

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@4939 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog    |  8 ++++++++
 src/files.c  | 10 +++++-----
 src/global.c |  2 +-
 src/text.c   |  8 ++++----
 4 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index aa24a3b8..41ed1b2a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,14 @@
 2014-06-04  David Lawrence Ramsey  <pooka109@gmail.com>
         * src/*.c: Adjustments of whitespace and comments.
         * doc/nanorc.sample.in: Interpunction tweaks.
+	* src/global.c (add_to_funcs): Add cast to subnfunc* for nmalloc().
+	* src/files.c (do_lockfile): Properly make the variable 'lockfilesize'
+	a size_t instead of a ssize_t, since it holds the result of strlen().
+	And use charalloc() instead of (char *)nmalloc().
+	* src/text.c (do_undo): Use charealloc() and not (char *)nrealloc().
+	* src/text.c (add_undo): Make use of null_at() to both null-terminate
+	the multibyte character and align it to use only the amount of memory
+	necessary.
 
 2014-06-02  Chris Allegretta <chrisa@asty.org>
 	* doc/syntax/default.nanorc: Can't do trailing spaces in the
diff --git a/src/files.c b/src/files.c
index 0fb7708c..557678f3 100644
--- a/src/files.c
+++ b/src/files.c
@@ -244,9 +244,9 @@ int do_lockfile(const char *filename)
 {
     char *lockdir = dirname((char *) mallocstrcpy(NULL, filename));
     char *lockbase = basename((char *) mallocstrcpy(NULL, filename));
-    ssize_t lockfilesize = (sizeof (char *) * (strlen(filename)
-                   + strlen(locking_prefix) + strlen(locking_suffix) + 3));
-    char *lockfilename = (char *) nmalloc(lockfilesize);
+    size_t lockfilesize = strlen(filename) + strlen(locking_prefix)
+		+ strlen(locking_suffix) + 3;
+    char *lockfilename = charalloc(lockfilesize);
     char lockprog[12], lockuser[16];
     struct stat fileinfo;
     int lockfd, lockpid;
@@ -259,8 +259,8 @@ int do_lockfile(const char *filename)
     if (stat(lockfilename, &fileinfo) != -1) {
         ssize_t readtot = 0;
         ssize_t readamt = 0;
-        char *lockbuf = (char *) nmalloc(8192);
-        char *promptstr = (char *) nmalloc(128);
+        char *lockbuf = charalloc(8192);
+        char *promptstr = charalloc(128);
         int ans;
         if ((lockfd = open(lockfilename, O_RDONLY)) < 0) {
             statusbar(_("Error opening lock file %s: %s"),
diff --git a/src/global.c b/src/global.c
index dbaf94e0..007fd030 100644
--- a/src/global.c
+++ b/src/global.c
@@ -296,7 +296,7 @@ function_type strtokeytype(const char *str)
 void add_to_funcs(void (*func)(void), int menus, const char *desc, const char *help,
     bool blank_after, bool viewok)
 {
-    subnfunc *f = nmalloc(sizeof(subnfunc));
+    subnfunc *f = (subnfunc *)nmalloc(sizeof(subnfunc));
 
     if (allfuncs == NULL)
 	allfuncs = f;
diff --git a/src/text.c b/src/text.c
index 66142d3c..ede4fe5f 100644
--- a/src/text.c
+++ b/src/text.c
@@ -473,7 +473,7 @@ void do_undo(void)
 #ifndef DISABLE_WRAPPING
     case SPLIT:
 	undidmsg = _("line wrap");
-	f->data = (char *) nrealloc(f->data, strlen(f->data) + strlen(u->strdata) + 1);
+	f->data = charealloc(f->data, strlen(f->data) + strlen(u->strdata) + 1);
 	strcpy(&f->data[strlen(f->data) - 1], u->strdata);
 	if (u->strdata2 != NULL)
 	    f->next->data = mallocstrcpy(f->next->data, u->strdata2);
@@ -510,7 +510,7 @@ void do_undo(void)
 	undidmsg = _("line break");
 	if (f->next) {
 	    filestruct *foo = f->next;
-	    f->data = (char *) nrealloc(f->data, strlen(f->data) + strlen(&f->next->data[u->mark_begin_x]) + 1);
+	    f->data = charealloc(f->data, strlen(f->data) + strlen(&f->next->data[u->mark_begin_x]) + 1);
 	    strcat(f->data, &f->next->data[u->mark_begin_x]);
 	    unlink_node(foo);
 	    delete_node(foo);
@@ -895,8 +895,8 @@ void add_undo(undo_type current_action)
 	if (u->begin != strlen(fs->current->data)) {
 	    char *char_buf = charalloc(mb_cur_max() + 1);
 	    int char_buf_len = parse_mbchar(&fs->current->data[u->begin], char_buf, NULL);
-	    char_buf[char_buf_len] = '\0';
-	    u->strdata = char_buf;  /* Note: there is likely more memory allocated than necessary. */
+	    null_at(&char_buf, char_buf_len);
+	    u->strdata = char_buf;
 	    u->mark_begin_x += char_buf_len;
 	    break;
 	}
-- 
GitLab