]> granicus.if.org Git - libexpat/commitdiff
Test bad start of name with an unknown encoding
authorRhodri James <rhodri@kynesim.co.uk>
Thu, 8 Jun 2017 18:48:43 +0000 (19:48 +0100)
committerSebastian Pipping <sebastian@pipping.org>
Sat, 22 Jul 2017 20:49:17 +0000 (22:49 +0200)
expat/tests/runtests.c

index d56be2dd0bec2debc25f325f5405f39d25f4bbc0..6498444e96bf7120c904c63d48c3a7e13ebee400 100644 (file)
@@ -5283,8 +5283,11 @@ failing_converter(void *UNUSED_P(data), const char *UNUSED_P(s))
 static int XMLCALL
 prefix_converter(void *UNUSED_P(data), const char *s)
 {
+    /* If the first byte is 0xff, raise an error */
+    if (s[0] == -1)
+        return -1;
     /* Just add the low bits of the first byte to the second */
-    return s[1] + (s[0] & 0x7f);
+    return (s[1] + (s[0] & 0x7f)) & 0x01ff;
 }
 
 static int XMLCALL
@@ -5367,6 +5370,20 @@ START_TEST(test_unknown_encoding_success)
 }
 END_TEST
 
+/* Test bad name character in unknown encoding */
+START_TEST(test_unknown_encoding_bad_name)
+{
+    const char *text =
+        "<?xml version='1.0' encoding='experimental'?>\n"
+        "<\xff\x64oc>Hello, world</\xff\x64oc>";
+
+    XML_SetUnknownEncodingHandler(parser, BadEncodingHandler,
+                                  (void *)PREFIX_CONVERTER);
+    expect_failure(text, XML_ERROR_INVALID_TOKEN,
+                   "Bad name start in unknown encoding not faulted");
+}
+END_TEST
+
 /* Be tidy */
 #undef NO_CONVERTER
 #undef FAILING_CONVERTER
@@ -10631,6 +10648,7 @@ make_suite(void)
     tcase_add_test(tc_basic, test_missing_encoding_conversion_fn);
     tcase_add_test(tc_basic, test_failing_encoding_conversion_fn);
     tcase_add_test(tc_basic, test_unknown_encoding_success);
+    tcase_add_test(tc_basic, test_unknown_encoding_bad_name);
 
     suite_add_tcase(s, tc_namespace);
     tcase_add_checked_fixture(tc_namespace,