bug-gnulib
[Top][All Lists]
Advanced

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

getndelim2 improvements


From: Eric Blake
Subject: getndelim2 improvements
Date: Mon, 28 Apr 2008 21:57:08 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080213 Thunderbird/2.0.0.12 Mnenhy/0.7.5.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I was planning on optimizing getndelim2 to use the new extended stdio
functions for speed.  But before I can do that, I need to make sure I
avoid regressions, so I'm checking this in.  This also adds file locking
(similar to getdelim), but I wasn't sure if we need to check for
getc_unlocked and use it instead of getc for optimal behavior?

It also fixes a bug where when the stream is at EOF, sometimes the
function returned 0 and sometimes -1; the patch makes sure EOF always
results in -1 (similar to the Posix 200x specification of getdelim always
returning -1 at EOF).

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgWnJQACgkQ84KuGfSFAYDawgCgqJ4w7mCg3LyR9kCUh32R82XW
ZlsAoMsAH/5S631ZVIFJVyR6PIrr9GBh
=tWGk
-----END PGP SIGNATURE-----
>From 463890eaa2cb1f303b100f6889421761149e936f Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Mon, 28 Apr 2008 21:36:17 -0600
Subject: [PATCH] Test getndelim2.

* modules/getndelim2-tests: New file.
* tests/test-getndelim2.c: Likewise.
* lib/getndelim2.c (getndelim2): Never return 0.  Lock the
stream.
* m4/getndelim2.m4 (gl_GETNDELIM2): Check for lock functions.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog                |    7 ++
 lib/getndelim2.c         |   23 +++++--
 m4/getndelim2.m4         |    6 +-
 modules/getndelim2-tests |   11 +++
 tests/test-getndelim2.c  |  162 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 202 insertions(+), 7 deletions(-)
 create mode 100644 modules/getndelim2-tests
 create mode 100644 tests/test-getndelim2.c

diff --git a/ChangeLog b/ChangeLog
index b7fa000..6f90e61 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2008-04-28  Eric Blake  <address@hidden>
 
+       Test getndelim2.
+       * modules/getndelim2-tests: New file.
+       * tests/test-getndelim2.c: Likewise.
+       * lib/getndelim2.c (getndelim2): Never return 0.  Lock the
+       stream.
+       * m4/getndelim2.m4 (gl_GETNDELIM2): Check for lock functions.
+
        * MODULES.html.sh: Document new module.
 
 2008-04-20  Bruno Haible  <address@hidden>
diff --git a/lib/getndelim2.c b/lib/getndelim2.c
index e1cd40a..8930a5b 100644
--- a/lib/getndelim2.c
+++ b/lib/getndelim2.c
@@ -1,7 +1,7 @@
 /* getndelim2 - Read a line from a stream, stopping at one of 2 delimiters,
    with bounded memory allocation.
 
-   Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003, 2004, 2006 Free
+   Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003, 2004, 2006, 2008 Free
    Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
@@ -29,6 +29,14 @@
 #if USE_UNLOCKED_IO
 # include "unlocked-io.h"
 #endif
+#if !HAVE_FLOCKFILE
+# undef flockfile
+# define flockfile(x) ((void) 0)
+#endif
+#if !HAVE_FUNLOCKFILE
+# undef funlockfile
+# define funlockfile(x) ((void) 0)
+#endif
 
 #include <limits.h>
 #include <stdint.h>
@@ -73,6 +81,8 @@ getndelim2 (char **lineptr, size_t *linesize, size_t offset, 
size_t nmax,
   if (nbytes_avail == 0 && nmax <= size)
     goto done;
 
+  flockfile (stream);
+
   for (;;)
     {
       /* Here always ptr + size == read_pos + nbytes_avail.  */
@@ -95,14 +105,14 @@ getndelim2 (char **lineptr, size_t *linesize, size_t 
offset, size_t nmax,
            {
              size_t newsizemax = offset + GETNDELIM2_MAXIMUM + 1;
              if (size == newsizemax)
-               goto done;
+               goto unlock_done;
              newsize = newsizemax;
            }
 
          nbytes_avail = newsize - (read_pos - ptr);
          newptr = realloc (ptr, newsize);
          if (!newptr)
-           goto done;
+           goto unlock_done;
          ptr = newptr;
          size = newsize;
          read_pos = size - nbytes_avail + ptr;
@@ -113,7 +123,7 @@ getndelim2 (char **lineptr, size_t *linesize, size_t 
offset, size_t nmax,
        {
          /* Return partial line, if any.  */
          if (read_pos == ptr)
-           goto done;
+           goto unlock_done;
          else
            break;
        }
@@ -135,8 +145,11 @@ getndelim2 (char **lineptr, size_t *linesize, size_t 
offset, size_t nmax,
 
   bytes_stored = read_pos - (ptr + offset);
 
+ unlock_done:
+  funlockfile (stream);
+
  done:
   *lineptr = ptr;
   *linesize = size;
-  return bytes_stored;
+  return bytes_stored ? bytes_stored : -1;
 }
diff --git a/m4/getndelim2.m4 b/m4/getndelim2.m4
index f0e7ae2..9aa89a1 100644
--- a/m4/getndelim2.m4
+++ b/m4/getndelim2.m4
@@ -1,5 +1,5 @@
-# getndelim2.m4 serial 5
-dnl Copyright (C) 2003, 2006 Free Software Foundation, Inc.
+# getndelim2.m4 serial 6
+dnl Copyright (C) 2003, 2006, 2008 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
@@ -8,6 +8,8 @@ AC_DEFUN([gl_GETNDELIM2],
 [
   AC_LIBOBJ(getndelim2)
   gl_PREREQ_GETNDELIM2
+  AC_CHECK_FUNCS_ONCE([flockfile])
+  AC_CHECK_FUNCS_ONCE([funlockfile])
 ])
 
 # Prerequisites of lib/getndelim2.h and lib/getndelim2.c.
diff --git a/modules/getndelim2-tests b/modules/getndelim2-tests
new file mode 100644
index 0000000..7081b3a
--- /dev/null
+++ b/modules/getndelim2-tests
@@ -0,0 +1,11 @@
+Files:
+tests/test-getndelim2.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-getndelim2
+check_PROGRAMS += test-getndelim2
+MOSTLYCLEANFILES += test-getndelim2.txt
diff --git a/tests/test-getndelim2.c b/tests/test-getndelim2.c
new file mode 100644
index 0000000..10ec1d8
--- /dev/null
+++ b/tests/test-getndelim2.c
@@ -0,0 +1,162 @@
+/* Test of getndelim2() function.
+   Copyright (C) 2008 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   This program 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Eric Blake <address@hidden>, 2008.  */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "getndelim2.h"
+
+#define ASSERT(expr) \
+  do                                                                        \
+    {                                                                       \
+      if (!(expr))                                                          \
+        {                                                                   \
+          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+          fflush (stderr);                                                  \
+          abort ();                                                         \
+        }                                                                   \
+    }                                                                       \
+  while (0)
+
+int
+main (int argc, char **argv)
+{
+  FILE *f;
+  char *line = NULL;
+  size_t len = 0;
+  ssize_t result;
+
+  /* Create test file.  */
+  f = fopen ("test-getndelim2.txt", "wb+");
+  if (!f || fwrite ("a\nbc\nd\0f", 1, 8, f) != 8)
+    {
+      fputs ("Failed to create sample file.\n", stderr);
+      remove ("test-getndelim2.txt");
+      return 1;
+    }
+  rewind (f);
+
+  /* Unlimited lines.  */
+
+  /* Test initial allocation, which must include trailing NUL.  */
+  result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\n', f);
+  ASSERT (result == 2);
+  ASSERT (strcmp (line, "a\n") == 0);
+  ASSERT (2 < len);
+
+  /* Test growth of buffer, must not leak.  */
+  free (line);
+  line = malloc (1);
+  len = 0;
+  result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, EOF, '\n', f);
+  ASSERT (result == 3);
+  ASSERT (strcmp (line, "bc\n") == 0);
+  ASSERT (3 < len);
+
+  /* Test embedded NULs and EOF behavior.  */
+  result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', EOF, f);
+  ASSERT (result == 3);
+  ASSERT (memcmp (line, "d\0f", 4) == 0);
+  ASSERT (3 < len);
+
+  result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', EOF, f);
+  ASSERT (result == -1);
+
+  /* Using offset and nmax.  */
+
+  /* Limit growth to four bytes, including NUL, but parse to next 'd'.  */
+  free (line);
+  rewind (f);
+  line = malloc (8);
+  memset (line, 'e', 8);
+  len = 8;
+  result = getndelim2 (&line, &len, 6, 10, 'd', 'd', f);
+  ASSERT (result == 3);
+  ASSERT (10 == len);
+  ASSERT (strcmp (line, "eeeeeea\nb") == 0);
+
+  /* No change if offset larger than limit.  */
+  result = getndelim2 (&line, &len, len, 1, EOF, EOF, f);
+  ASSERT (result == -1);
+  ASSERT (10 == len);
+  ASSERT (strcmp (line, "eeeeeea\nb") == 0);
+
+  /* Consume to end of file, including embedded NUL.  */
+  result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, EOF, EOF, f);
+  ASSERT (result == 2);
+  ASSERT (10 == len);
+  ASSERT (memcmp (line, "\0f\0eeea\nb", 10) == 0);
+
+  result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
+  ASSERT (result == -1);
+
+  /* Larger file size.  */
+  rewind (f);
+  {
+    int i;
+    for (i = 0; i < 16; i++)
+      fprintf (f, "%500x%c", i, i % 2 ? '\n' : '\r');
+  }
+  rewind (f);
+  {
+    char buffer[502];
+
+    result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
+    ASSERT (result == 501);
+    ASSERT (501 < len);
+    memset (buffer, ' ', 499);
+    buffer[499] = '0';
+    buffer[500] = '\r';
+    buffer[501] = '\0';
+    ASSERT (strcmp (buffer, line) == 0);
+
+    result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, '\n', '\r', f);
+    ASSERT (result == 501);
+    ASSERT (501 < len);
+    buffer[499] = '1';
+    buffer[500] = '\n';
+    ASSERT (strcmp (buffer, line) == 0);
+
+    result = getndelim2 (&line, &len, 0, GETNLINE_NO_LIMIT, 'g', 'f', f);
+    ASSERT (result == 501 * 14 - 1);
+    ASSERT (501 * 14 < len);
+    buffer[499] = 'f';
+    buffer[500] = '\0';
+    ASSERT (strcmp (buffer, line + 501 * 13) == 0);
+
+    result = getndelim2 (&line, &len, 501 * 14 - 1, GETNLINE_NO_LIMIT,
+                        EOF, EOF, f);
+    ASSERT (result == 1);
+    buffer[500] = '\n';
+    ASSERT (strcmp (buffer, line + 501 * 13) == 0);
+
+    result = getndelim2 (&line, &len, 501 * 14 - 1, GETNLINE_NO_LIMIT,
+                        EOF, EOF, f);
+    buffer[500] = '\0';
+    ASSERT (strcmp (buffer, line + 501 * 13) == 0);
+    ASSERT (result == -1);
+  }
+
+  fclose (f);
+  remove ("test-getndelim2.txt");
+  return 0;
+}
-- 
1.5.5.1


reply via email to

[Prev in Thread] Current Thread [Next in Thread]