From 24281f1506de2ae0815b2eaa82bb892d2ca0a2c9 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 22 Feb 2009 13:15:52 +0100 Subject: [PATCH] Handle empty strings correctly. --- ChangeLog | 17 +++++++++++++++++ lib/uninorm/u-normalize-internal.h | 23 +++++++++++++++++++++++ tests/uninorm/test-u16-nfc.c | 3 +++ tests/uninorm/test-u16-nfd.c | 3 +++ tests/uninorm/test-u16-nfkc.c | 3 +++ tests/uninorm/test-u16-nfkd.c | 3 +++ tests/uninorm/test-u32-nfc.c | 3 +++ tests/uninorm/test-u32-nfd.c | 3 +++ tests/uninorm/test-u32-nfkc.c | 3 +++ tests/uninorm/test-u32-nfkd.c | 3 +++ tests/uninorm/test-u8-nfc.c | 3 +++ tests/uninorm/test-u8-nfd.c | 3 +++ tests/uninorm/test-u8-nfkc.c | 3 +++ tests/uninorm/test-u8-nfkd.c | 3 +++ 14 files changed, 76 insertions(+) diff --git a/ChangeLog b/ChangeLog index 43b2c8f0b..4a07193d4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2009-02-22 Bruno Haible + + * 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 * doc/gnulib.texi: Include safe-alloc.texi earlier. diff --git a/lib/uninorm/u-normalize-internal.h b/lib/uninorm/u-normalize-internal.h index 7dc83ad9d..70c325513 100644 --- a/lib/uninorm/u-normalize-internal.h +++ b/lib/uninorm/u-normalize-internal.h @@ -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) diff --git a/tests/uninorm/test-u16-nfc.c b/tests/uninorm/test-u16-nfc.c index 397914c5f..70767df5a 100644 --- a/tests/uninorm/test-u16-nfc.c +++ b/tests/uninorm/test-u16-nfc.c @@ -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); diff --git a/tests/uninorm/test-u16-nfd.c b/tests/uninorm/test-u16-nfd.c index 84499f0d3..161abd474 100644 --- a/tests/uninorm/test-u16-nfd.c +++ b/tests/uninorm/test-u16-nfd.c @@ -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); diff --git a/tests/uninorm/test-u16-nfkc.c b/tests/uninorm/test-u16-nfkc.c index aa7bc2cee..df6480448 100644 --- a/tests/uninorm/test-u16-nfkc.c +++ b/tests/uninorm/test-u16-nfkc.c @@ -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); diff --git a/tests/uninorm/test-u16-nfkd.c b/tests/uninorm/test-u16-nfkd.c index 3cc1fac8f..587a704ae 100644 --- a/tests/uninorm/test-u16-nfkd.c +++ b/tests/uninorm/test-u16-nfkd.c @@ -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); diff --git a/tests/uninorm/test-u32-nfc.c b/tests/uninorm/test-u32-nfc.c index 0c7cd790f..3aafb96a8 100644 --- a/tests/uninorm/test-u32-nfc.c +++ b/tests/uninorm/test-u32-nfc.c @@ -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); diff --git a/tests/uninorm/test-u32-nfd.c b/tests/uninorm/test-u32-nfd.c index 9aa820a3d..426ad9dd5 100644 --- a/tests/uninorm/test-u32-nfd.c +++ b/tests/uninorm/test-u32-nfd.c @@ -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); diff --git a/tests/uninorm/test-u32-nfkc.c b/tests/uninorm/test-u32-nfkc.c index 0ef65f593..831e490a4 100644 --- a/tests/uninorm/test-u32-nfkc.c +++ b/tests/uninorm/test-u32-nfkc.c @@ -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); diff --git a/tests/uninorm/test-u32-nfkd.c b/tests/uninorm/test-u32-nfkd.c index 17768f74b..408c593b0 100644 --- a/tests/uninorm/test-u32-nfkd.c +++ b/tests/uninorm/test-u32-nfkd.c @@ -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); diff --git a/tests/uninorm/test-u8-nfc.c b/tests/uninorm/test-u8-nfc.c index 0e1915708..5a86e59f2 100644 --- a/tests/uninorm/test-u8-nfc.c +++ b/tests/uninorm/test-u8-nfc.c @@ -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); diff --git a/tests/uninorm/test-u8-nfd.c b/tests/uninorm/test-u8-nfd.c index 9dfde02c2..2338e8f8f 100644 --- a/tests/uninorm/test-u8-nfd.c +++ b/tests/uninorm/test-u8-nfd.c @@ -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); diff --git a/tests/uninorm/test-u8-nfkc.c b/tests/uninorm/test-u8-nfkc.c index 6792e826d..1d9f0e1a9 100644 --- a/tests/uninorm/test-u8-nfkc.c +++ b/tests/uninorm/test-u8-nfkc.c @@ -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); diff --git a/tests/uninorm/test-u8-nfkd.c b/tests/uninorm/test-u8-nfkd.c index c2a2d1453..498f42e4b 100644 --- a/tests/uninorm/test-u8-nfkd.c +++ b/tests/uninorm/test-u8-nfkd.c @@ -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); -- 2.11.0