Handle empty strings correctly.
authorBruno Haible <bruno@clisp.org>
Sun, 22 Feb 2009 12:15:52 +0000 (13:15 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 22 Feb 2009 12:15:52 +0000 (13:15 +0100)
14 files changed:
ChangeLog
lib/uninorm/u-normalize-internal.h
tests/uninorm/test-u16-nfc.c
tests/uninorm/test-u16-nfd.c
tests/uninorm/test-u16-nfkc.c
tests/uninorm/test-u16-nfkd.c
tests/uninorm/test-u32-nfc.c
tests/uninorm/test-u32-nfd.c
tests/uninorm/test-u32-nfkc.c
tests/uninorm/test-u32-nfkd.c
tests/uninorm/test-u8-nfc.c
tests/uninorm/test-u8-nfd.c
tests/uninorm/test-u8-nfkc.c
tests/uninorm/test-u8-nfkd.c

index 43b2c8f..4a07193 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2009-02-22  Bruno Haible  <bruno@clisp.org>
+
+       * lib/uninorm/u-normalize-internal.h (FUNC): At the end, handle
+       zero-length results and shrink excess allocated memory.
+       * tests/uninorm/test-u8-nfc.c (test_u8_nfc): Check empty string result.
+       * tests/uninorm/test-u8-nfd.c (test_u8_nfd): Likewise.
+       * tests/uninorm/test-u8-nfkc.c (test_u8_nfkc): Likewise.
+       * tests/uninorm/test-u8-nfkd.c (test_u8_nfkd): Likewise.
+       * tests/uninorm/test-u16-nfc.c (test_u16_nfc): Likewise.
+       * tests/uninorm/test-u16-nfd.c (test_u16_nfd): Likewise.
+       * tests/uninorm/test-u16-nfkc.c (test_u16_nfkc): Likewise.
+       * tests/uninorm/test-u16-nfkd.c (test_u16_nfkd): Likewise.
+       * tests/uninorm/test-u32-nfc.c (test_u32_nfc): Likewise.
+       * tests/uninorm/test-u32-nfd.c (test_u32_nfd): Likewise.
+       * tests/uninorm/test-u32-nfkc.c (test_u32_nfkc): Likewise.
+       * tests/uninorm/test-u32-nfkd.c (test_u32_nfkd): Likewise.
+
 2009-02-21  Bruno Haible  <bruno@clisp.org>
 
        * doc/gnulib.texi: Include safe-alloc.texi earlier.
index 7dc83ad..70c3255 100644 (file)
@@ -331,6 +331,29 @@ FUNC (uninorm_t nf, const UNIT *s, size_t n,
       }
   }
 
+  if (length == 0)
+    {
+      if (result == NULL)
+       {
+         /* Return a non-NULL value.  NULL means error.  */
+         result = (UNIT *) malloc (1);
+         if (result == NULL)
+           {
+             errno = ENOMEM;
+             goto fail;
+           }
+       }
+    }
+  else if (result != resultbuf && length < allocated)
+    {
+      /* Shrink the allocated memory if possible.  */
+      UNIT *memory;
+
+      memory = (UNIT *) realloc (result, length * sizeof (UNIT));
+      if (memory != NULL)
+       result = memory;
+    }
+
   if (sortbuf_count > 0)
     abort ();
   if (sortbuf != sortbuf_preallocated)
index 397914c..70767df 100644 (file)
@@ -103,6 +103,9 @@ check (const uint16_t *input, size_t input_length,
 void
 test_u16_nfc (void)
 {
+  { /* Empty string.  */
+    ASSERT (check (NULL, 0, NULL, 0) == 0);
+  }
   { /* SPACE */
     static const uint16_t input[]    = { 0x0020 };
     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
index 84499f0..161abd4 100644 (file)
@@ -103,6 +103,9 @@ check (const uint16_t *input, size_t input_length,
 void
 test_u16_nfd (void)
 {
+  { /* Empty string.  */
+    ASSERT (check (NULL, 0, NULL, 0) == 0);
+  }
   { /* SPACE */
     static const uint16_t input[]    = { 0x0020 };
     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
index aa7bc2c..df64804 100644 (file)
@@ -103,6 +103,9 @@ check (const uint16_t *input, size_t input_length,
 void
 test_u16_nfkc (void)
 {
+  { /* Empty string.  */
+    ASSERT (check (NULL, 0, NULL, 0) == 0);
+  }
   { /* SPACE */
     static const uint16_t input[]    = { 0x0020 };
     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
index 3cc1fac..587a704 100644 (file)
@@ -103,6 +103,9 @@ check (const uint16_t *input, size_t input_length,
 void
 test_u16_nfkd (void)
 {
+  { /* Empty string.  */
+    ASSERT (check (NULL, 0, NULL, 0) == 0);
+  }
   { /* SPACE */
     static const uint16_t input[]    = { 0x0020 };
     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
index 0c7cd79..3aafb96 100644 (file)
@@ -103,6 +103,9 @@ check (const uint32_t *input, size_t input_length,
 void
 test_u32_nfc (void)
 {
+  { /* Empty string.  */
+    ASSERT (check (NULL, 0, NULL, 0) == 0);
+  }
   { /* SPACE */
     static const uint32_t input[]    = { 0x0020 };
     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
index 9aa820a..426ad9d 100644 (file)
@@ -103,6 +103,9 @@ check (const uint32_t *input, size_t input_length,
 void
 test_u32_nfd (void)
 {
+  { /* Empty string.  */
+    ASSERT (check (NULL, 0, NULL, 0) == 0);
+  }
   { /* SPACE */
     static const uint32_t input[]    = { 0x0020 };
     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
index 0ef65f5..831e490 100644 (file)
@@ -103,6 +103,9 @@ check (const uint32_t *input, size_t input_length,
 void
 test_u32_nfkc (void)
 {
+  { /* Empty string.  */
+    ASSERT (check (NULL, 0, NULL, 0) == 0);
+  }
   { /* SPACE */
     static const uint32_t input[]    = { 0x0020 };
     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
index 17768f7..408c593 100644 (file)
@@ -103,6 +103,9 @@ check (const uint32_t *input, size_t input_length,
 void
 test_u32_nfkd (void)
 {
+  { /* Empty string.  */
+    ASSERT (check (NULL, 0, NULL, 0) == 0);
+  }
   { /* SPACE */
     static const uint32_t input[]    = { 0x0020 };
     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
index 0e19157..5a86e59 100644 (file)
@@ -103,6 +103,9 @@ check (const uint8_t *input, size_t input_length,
 void
 test_u8_nfc (void)
 {
+  { /* Empty string.  */
+    ASSERT (check (NULL, 0, NULL, 0) == 0);
+  }
   { /* SPACE */
     static const uint8_t input[]    = { 0x20 };
     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
index 9dfde02..2338e8f 100644 (file)
@@ -103,6 +103,9 @@ check (const uint8_t *input, size_t input_length,
 void
 test_u8_nfd (void)
 {
+  { /* Empty string.  */
+    ASSERT (check (NULL, 0, NULL, 0) == 0);
+  }
   { /* SPACE */
     static const uint8_t input[]    = { 0x20 };
     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
index 6792e82..1d9f0e1 100644 (file)
@@ -103,6 +103,9 @@ check (const uint8_t *input, size_t input_length,
 void
 test_u8_nfkc (void)
 {
+  { /* Empty string.  */
+    ASSERT (check (NULL, 0, NULL, 0) == 0);
+  }
   { /* SPACE */
     static const uint8_t input[]    = { 0x20 };
     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
index c2a2d14..498f42e 100644 (file)
@@ -103,6 +103,9 @@ check (const uint8_t *input, size_t input_length,
 void
 test_u8_nfkd (void)
 {
+  { /* Empty string.  */
+    ASSERT (check (NULL, 0, NULL, 0) == 0);
+  }
   { /* SPACE */
     static const uint8_t input[]    = { 0x20 };
     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);