[geany/geany] 0eec77: Fix truncation of the data retrieved with --list-documents

Colomban Wendling git-noreply at xxxxx
Tue Dec 17 15:17:21 UTC 2013


Branch:      refs/heads/master
Author:      Colomban Wendling <ban at herbesfolles.org>
Committer:   Colomban Wendling <ban at herbesfolles.org>
Date:        Tue, 17 Dec 2013 15:17:21 UTC
Commit:      0eec7764af5cbcf70e10e44d2eb7b486e5c7cd53
             https://github.com/geany/geany/commit/0eec7764af5cbcf70e10e44d2eb7b486e5c7cd53

Log Message:
-----------
Fix truncation of the data retrieved with --list-documents

Do not truncate the document list to the size of the buffer used for
communication.

Since we now read multiple times, we need to make sure to always send
ETX so a reader don't hang reading the next chunk if the data sent had
exactly the size of the buffer.


Modified Paths:
--------------
    src/socket.c

Modified: src/socket.c
28 files changed, 15 insertions(+), 13 deletions(-)
===================================================================
@@ -195,21 +195,25 @@ static void remove_socket_link_full(void)
 
 static void socket_get_document_list(gint sock)
 {
-	gchar doc_list[BUFFER_LENGTH];
-	gint doc_list_len;
+	gchar buf[BUFFER_LENGTH];
+	gint n_read;
 
 	if (sock < 0)
 		return;
 
 	socket_fd_write_all(sock, "doclist\n", 8);
 
-	doc_list_len = socket_fd_read(sock, doc_list, sizeof(doc_list));
-	if (doc_list_len >= BUFFER_LENGTH)
-		doc_list_len = BUFFER_LENGTH -1;
-	doc_list[doc_list_len] = '\0';
-	/* if we received ETX (end-of-text), there were no open files, so print only otherwise */
-	if (! utils_str_equal(doc_list, "\3"))
-		printf("%s", doc_list);
+	do
+	{
+		n_read = socket_fd_read(sock, buf, sizeof(buf));
+		/* if we received ETX (end-of-text), there is nothing else to read, so cut that
+		 * byte not to output it and to be sure not to validate the loop condition */
+		if (n_read > 0 && buf[n_read - 1] == '\3')
+			n_read--;
+		if (n_read > 0)
+			fwrite(buf, 1, n_read, stdout);
+	}
+	while (n_read >= sizeof(buf));
 }
 
 
@@ -625,10 +629,8 @@ gboolean socket_lock_input_cb(GIOChannel *source, GIOCondition condition, gpoint
 			gchar *doc_list = build_document_list();
 			if (!EMPTY(doc_list))
 				socket_fd_write_all(sock, doc_list, strlen(doc_list));
-			else
-				/* send ETX (end-of-text) in case we have no open files, we must send anything
-				 * otherwise the client would hang on reading */
-				socket_fd_write_all(sock, "\3", 1);
+			/* send ETX (end-of-text) so reader knows to stop reading */
+			socket_fd_write_all(sock, "\3", 1);
 			g_free(doc_list);
 		}
 		else if (strncmp(buf, "line", 4) == 0)



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list