[glib: 1/2] giochannel: Fix read_line() when nul is not a line terminator



commit c8811da56a6628d9df8a9fa0051c42e6bff94cd6
Author: Philip Withnall <withnall endlessm com>
Date:   Tue Jan 14 19:55:36 2020 +0000

    giochannel: Fix read_line() when nul is not a line terminator
    
    And add a test for it.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    Fixes: #2002

 glib/giochannel.c       | 10 ++++++-
 glib/tests/io-channel.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++
 glib/tests/meson.build  |  1 +
 3 files changed, 89 insertions(+), 1 deletion(-)
---
diff --git a/glib/giochannel.c b/glib/giochannel.c
index ed8546331..d16399846 100644
--- a/glib/giochannel.c
+++ b/glib/giochannel.c
@@ -1669,8 +1669,16 @@ g_io_channel_read_line (GIOChannel  *channel,
 
   if (status == G_IO_STATUS_NORMAL)
     {
+      gchar *line;
+
+      /* Copy the read bytes (including any embedded nuls) and nul-terminate.
+       * `USE_BUF (channel)->str` is guaranteed to be nul-terminated as it’s a
+       * #GString, so it’s safe to call g_memdup() with +1 length to allocate
+       * a nul-terminator. */
       g_assert (USE_BUF (channel));
-      *str_return = g_strndup (USE_BUF (channel)->str, got_length);
+      line = g_memdup (USE_BUF (channel)->str, got_length + 1);
+      line[got_length] = '\0';
+      *str_return = g_steal_pointer (&line);
       g_string_erase (USE_BUF (channel), 0, got_length);
     }
   else
diff --git a/glib/tests/io-channel.c b/glib/tests/io-channel.c
new file mode 100644
index 000000000..ff53fcef7
--- /dev/null
+++ b/glib/tests/io-channel.c
@@ -0,0 +1,79 @@
+/* GLib testing framework examples and tests
+ *
+ * Copyright © 2020 Endless Mobile, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Philip Withnall <withnall endlessm com>
+ */
+
+#include <glib.h>
+#include <glib/gstdio.h>
+
+static void
+test_read_line_embedded_nuls (void)
+{
+  const guint8 test_data[] = { 'H', 'i', '!', '\0', 'y', 'o', 'u', '\n', ':', ')', '\n' };
+  gint fd;
+  gchar *filename = NULL;
+  GIOChannel *channel = NULL;
+  GError *local_error = NULL;
+  gchar *line = NULL;
+  gsize line_length, terminator_pos;
+  GIOStatus status;
+
+  g_test_summary ("Test that reading a line containing embedded nuls works "
+                  "when using non-standard line terminators.");
+
+  /* Write out a temporary file. */
+  fd = g_file_open_tmp ("glib-test-io-channel-XXXXXX", &filename, &local_error);
+  g_assert_no_error (local_error);
+  g_close (fd, NULL);
+  fd = -1;
+
+  g_file_set_contents (filename, (const gchar *) test_data, sizeof (test_data), &local_error);
+  g_assert_no_error (local_error);
+
+  /* Create the channel. */
+  channel = g_io_channel_new_file (filename, "r", &local_error);
+  g_assert_no_error (local_error);
+
+  /* Only break on newline characters, not nuls. */
+  g_io_channel_set_line_term (channel, "\n", 1);
+  g_io_channel_set_encoding (channel, NULL, &local_error);
+  g_assert_no_error (local_error);
+
+  status = g_io_channel_read_line (channel, &line, &line_length,
+                                   &terminator_pos, &local_error);
+  g_assert_no_error (local_error);
+  g_assert_cmpint (status, ==, G_IO_STATUS_NORMAL);
+  g_assert_cmpuint (line_length, ==, 8);
+  g_assert_cmpuint (terminator_pos, ==, 7);
+  g_assert_cmpmem (line, line_length, test_data, 8);
+
+  g_free (line);
+  g_io_channel_unref (channel);
+  g_free (filename);
+}
+
+int
+main (int   argc,
+      char *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/io-channel/read-line/embedded-nuls", test_read_line_embedded_nuls);
+
+  return g_test_run ();
+}
diff --git a/glib/tests/meson.build b/glib/tests/meson.build
index 480053288..3dedafcdb 100644
--- a/glib/tests/meson.build
+++ b/glib/tests/meson.build
@@ -35,6 +35,7 @@ glib_tests = {
   'hmac' : {},
   'hook' : {},
   'hostutils' : {},
+  'io-channel' : {},
   'keyfile' : {},
   'list' : {},
   'logging' : {},


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]