From ef09dbabd409f7b4d42c40c03b74f718aa1b17c1 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Thu, 19 Mar 2020 15:22:58 +0100 Subject: [PATCH 64/68] xmlparse.c: Fix undefined behavior for XML_UNICODE Pointer arithmetic with NULL is undefined behavior. This reverts c71f27573bd0205558a78792b554764f9c962179 --- Changes | 4 ++++ lib/xmlparse.c | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Changes b/Changes index 1680489..a801d7f 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,10 @@ NOTE: We are looking for help with a few things: If you can help, please get in touch. Thanks! Release 2.2.9 Wed Septemper 25 2019 + Bug fixes: + #390 #395 Fix undefined behavior during parsing when compiled with + -DXML_UNICODE that was introduced with Expat 2.0.1 + Other changes: examples: Drop executable bits from elements.c #349 Windows: Change the name of the Windows DLLs from expat*.dll diff --git a/lib/xmlparse.c b/lib/xmlparse.c index 8b8c6f0..638ea52 100644 --- a/lib/xmlparse.c +++ b/lib/xmlparse.c @@ -48,6 +48,17 @@ #include /* fprintf */ #include /* getenv, rand_s */ +#if defined(_WIN32) && defined(_MSC_VER) && (_MSC_VER < 1600) +/* vs2008/9.0 and earlier lack stdint.h; _MSC_VER 1600 is vs2010/10.0 */ +# if defined(_WIN64) +typedef unsigned __int64 uintptr_t; +# else +typedef unsigned __int32 uintptr_t; +# endif +#else +# include /* uintptr_t */ +#endif + #ifdef _WIN32 # define getpid GetCurrentProcessId #else @@ -121,9 +132,7 @@ # define XmlGetInternalEncoding XmlGetUtf16InternalEncoding # define XmlGetInternalEncodingNS XmlGetUtf16InternalEncodingNS # define XmlEncode XmlUtf16Encode -/* Using pointer subtraction to convert to integer type. */ -# define MUST_CONVERT(enc, s) \ - (! (enc)->isUtf16 || (((char *)(s) - (char *)NULL) & 1)) +# define MUST_CONVERT(enc, s) (! (enc)->isUtf16 || (((uintptr_t)(s)) & 1)) typedef unsigned short ICHAR; #else # define XML_ENCODE_MAX XML_UTF8_ENCODE_MAX -- 1.8.3.1