From caf8b1fcfd6fdaefc9e72f6ef14edbc08cf705d6 Mon Sep 17 00:00:00 2001
From: Claudio Matsuoka <cmatsuoka@gmail.com>
Date: Tue, 11 Jan 2011 10:59:54 -0200
Subject: [PATCH] Fix memory violation when smushing at line start

Smushing characters at the line start can cause the next character to
be copied to an offset before the start of the line buffer. In this
case, add an offset to source and copy to a valid destination position.

Signed-off-by: Claudio Matsuoka <cmatsuoka@gmail.com>
---
 figlet.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/figlet.c b/figlet.c
index d325c14..f582423 100644
--- a/figlet.c
+++ b/figlet.c
@@ -1424,7 +1424,7 @@ int smushamt()
 int addchar(c)
 inchr c;
 {
-  int smushamount,row,k;
+  int smushamount,row,k,column,offset;
   char *templine;
 
   getletter(c);
@@ -1434,6 +1434,7 @@ inchr c;
     return 0;
     }
 
+  offset = 0;
   templine = (char*)myalloc(sizeof(char)*(outlinelenlimit+1));
   for (row=0;row<charheight;row++) {
     if (right2left) {
@@ -1447,8 +1448,13 @@ inchr c;
       }
     else {
       for (k=0;k<smushamount;k++) {
-        outputline[row][outlinelen-smushamount+k] =
-          smushem(outputline[row][outlinelen-smushamount+k],currchar[row][k]);
+	column = outlinelen-smushamount+k;
+	if (column < 0) {
+	  offset = -column;
+	  column = 0;
+	  }
+        outputline[row][column] =
+          smushem(outputline[row][column],currchar[row][k + offset]);
         }
       strcat(outputline[row],currchar[row]+smushamount);
       }