diff --git a/gedit/gedit-document-input-stream.c b/gedit/gedit-document-input-stream.c
index 45cba7e..c3b7179 100644
--- a/gedit/gedit-document-input-stream.c
+++ b/gedit/gedit-document-input-stream.c
@@ -330,9 +330,19 @@ read_line (GeditDocumentInputStream *stream,
 
 		while (written < to_write)
 		{
+			gint w;
+
 			ptr = g_utf8_next_char (ptr);
-			written = (ptr - buf);
-			++char_offset;
+			w = (ptr - buf);
+			if (w > to_write)
+			{
+				break;
+			}
+			else
+			{
+				written = w;
+				++char_offset;
+			}
 		}
 
 		memcpy (outbuf, buf, written);
diff --git a/gedit/gedit-gio-document-saver.c b/gedit/gedit-gio-document-saver.c
index 7830c58..52dbd7c 100644
--- a/gedit/gedit-gio-document-saver.c
+++ b/gedit/gedit-gio-document-saver.c
@@ -354,6 +354,8 @@ write_file_chunk (AsyncData *async)
 {
 	GeditGioDocumentSaver *gvsaver;
 
+	gedit_debug (DEBUG_SAVER);
+
 	gvsaver = async->saver;
 
 	g_output_stream_write_async (G_OUTPUT_STREAM (gvsaver->priv->stream),
@@ -372,6 +374,8 @@ read_file_chunk (AsyncData *async)
 	GeditDocumentInputStream *dstream;
 	GError *error = NULL;
 
+	gedit_debug (DEBUG_SAVER);
+
 	gvsaver = async->saver;
 	async->written = 0;
 
