summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Josefsson <simon@josefsson.org>2016-01-14 14:31:33 +0100
committerSimon Josefsson <simon@josefsson.org>2016-01-14 14:32:34 +0100
commit1fbee57ef3c72db2206dd87e4162108b2f425555 (patch)
treeec0aa68d735227e26b8cf59bdc8aa63b117def47
parent11abd0e02c16f9e0b6944aea4ef0f2df44b42dd4 (diff)
downloadlibidn-1fbee57ef3c72db2206dd87e4162108b2f425555.tar.gz
stringprep_utf8_nfkc_normalize: Reject invalid UTF8 instead of crashing.
Also add regression self check. Reported by Hanno Böck.
-rw-r--r--.gitignore2
-rw-r--r--lib/nfkc.c12
-rw-r--r--tests/Makefile.am5
-rw-r--r--tests/tst_badutf8nfkc.c41
4 files changed, 57 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore
index ee53963..d207b3c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -619,6 +619,8 @@ tests/Makefile.in
tests/libutils.a
tests/tst_badutf8
tests/tst_badutf8.o
+tests/tst_badutf8nfkc
+tests/tst_badutf8nfkc.o
tests/tst_idna
tests/tst_idna.o
tests/tst_idna2
diff --git a/lib/nfkc.c b/lib/nfkc.c
index 4992074..edc62c4 100644
--- a/lib/nfkc.c
+++ b/lib/nfkc.c
@@ -1,5 +1,5 @@
/* nfkc.c --- Unicode normalization utilities.
- Copyright (C) 2002-2015 Simon Josefsson
+ Copyright (C) 2002-2016 Simon Josefsson
This file is part of GNU Libidn.
@@ -1086,6 +1086,16 @@ stringprep_ucs4_to_utf8 (const uint32_t * str, ssize_t len,
char *
stringprep_utf8_nfkc_normalize (const char *str, ssize_t len)
{
+ size_t n;
+
+ if (len < 0)
+ n = strlen (str);
+ else
+ n = len;
+
+ if (u8_check ((const uint8_t *) str, n))
+ return NULL;
+
return g_utf8_normalize (str, len, G_NORMALIZE_NFKC);
}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9130c32..5f4cb24 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,5 +1,5 @@
## Process this file with automake to produce Makefile.in
-# Copyright (C) 2002-2015 Simon Josefsson
+# Copyright (C) 2002-2016 Simon Josefsson
#
# This file is part of GNU Libidn.
#
@@ -27,7 +27,8 @@ libutils_a_SOURCES = utils.h utils.c
ctests = tst_stringprep tst_punycode tst_idna tst_idna2 tst_idna3 \
tst_idna4 tst_nfkc tst_pr29 tst_strerror tst_toutf8 \
- tst_symbols tst_badutf8 tst_utf8crash tst_toascii64oob
+ tst_symbols tst_badutf8 tst_utf8crash tst_toascii64oob \
+ tst_badutf8nfkc
if TLD
ctests += tst_tld
endif
diff --git a/tests/tst_badutf8nfkc.c b/tests/tst_badutf8nfkc.c
new file mode 100644
index 0000000..e67076b
--- /dev/null
+++ b/tests/tst_badutf8nfkc.c
@@ -0,0 +1,41 @@
+/* tst_badutf8nfkc.c --- Self tests for malformed UTF-8 NFKC input.
+ * Copyright (C) 2016 Simon Josefsson
+ *
+ * This file is part of GNU Libidn.
+ *
+ * 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 of the License, 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+
+#include <stringprep.h>
+
+#include "utils.h"
+
+void
+doit (void)
+{
+ char *badutf8 = strdup ("\xe4");
+ char *s = NULL;
+
+ s = stringprep_utf8_nfkc_normalize (badutf8, -1);
+ free (s);
+ free (badutf8);
+}