From d6278ae7cd9149aea44322d83ebef5aa67a39004 Mon Sep 17 00:00:00 2001
From: Jim Dishaw <jim@dishaw.org>
Date: Mon, 8 Jun 2015 18:16:43 -0400
Subject: [PATCH 3/3] Fixed a NUL termination bug in plbuf.c

When storing non-unicode strings, the NUL character was not being stored in
the buffer.  This causes problems with drivers that use non-unicode strings
when the plot buffer is replayed.
---
 src/plbuf.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/src/plbuf.c b/src/plbuf.c
index e200a07..67111a3 100644
--- a/src/plbuf.c
+++ b/src/plbuf.c
@@ -382,10 +382,11 @@ plbuf_text( PLStream *pls, EscText *text )
     {
         U_SHORT len;
 
-        len = strlen( text->string );
+		// len + 1 to copy the NUL termination
+        len = strlen( text->string ) + 1;
         wr_data( pls, &len, sizeof ( len ) );
         if ( len > 0 )
-            wr_data( pls, (void *) text->string, sizeof ( char ) * len );
+           wr_data( pls, (void *) text->string, sizeof ( char ) * len );
     }
 }
 
@@ -1118,6 +1119,13 @@ rdbuf_text( PLStream *pls )
     rd_data( pls, &text.refy, sizeof ( text.refy ) );
     rd_data( pls, &text.font_face, sizeof ( text.font_face ) );
 
+	// Initialize text arrays to NULL.  This protects drivers that
+	// determine the text representation by looking at which members
+	// are set.
+	text.unicode_array_len = 0;
+	text.unicode_array = NULL;
+	text.string = NULL;
+	
     // Read in the text
     if ( pls->dev_unicode )
     {
@@ -1136,16 +1144,12 @@ rdbuf_text( PLStream *pls )
                 (void **) ( &text.unicode_array ),
                 sizeof ( PLUNICODE ) * text.unicode_array_len );
         }
-        else
-        {
-            text.unicode_array = NULL;
-        }
     }
     else
     {
         U_SHORT len;
 
-        rd_data( pls, &len, sizeof ( len ) );
+        rd_data( pls, &len, sizeof ( len ));
         if ( len > 0 )
         {
             // Set the pointer to the string data in the buffer.  This avoids
@@ -1155,10 +1159,6 @@ rdbuf_text( PLStream *pls )
                 (void **) ( &text.string ),
                 sizeof ( char ) * len );
         }
-        else
-        {
-            text.string = NULL;
-        }
     }
 
     plP_esc( PLESC_HAS_TEXT, &text );
-- 
1.9.5.github.0

