Resending now that I've joined the mailing list...

While building 64-bit Chromium with VC++ 2015 Update 1 I noticed a
significant number of pointer truncation warnings in libxml, especially in
xpath.c. A typical warning is:

warning C4311: 'type cast': pointer truncation from 'xmlChar *' to 'long'

which triggers on the last two lines of this block:

case XML_ELEMENT_NODE:
    if (node2->type == XML_ELEMENT_NODE) {
if ((0 > (long) node1->content) && /* TODO: Would a != 0 suffice here? */
    (0 > (long) node2->content) &&

The intent is not entirely clear but if these are supposed to be NULL
checks then they could easily give the wrong result. In the VC++ world
'long' is always a 32-bit type, so converting a pointer to a long both
throws away the top 32 bits and also makes it a signed type. That means
that pointers to the first 2 GB of address space will behave as expected,
and pointers beyond that will have a 50% chance of behaving incorrectly!

Another example is these two conversions. The code is apparently trying to
sort by content address, but on 64-bit Windows builds it will just be
sorting by the bottom 32 bits of the content address.
l1 = -((long) node1->content);
l2 = -((long) node2->content);
if (l1 < l2)
   return(1);
if (l1 > l2)
   return(-1);
    }

This is not a problem with gcc or clang because their 64-bit builds have
64-bit long, but the C/C++ standard do not mandate and that is not the case
with VC++.

I think that the correct type would be ptrdiff_t, or size_t, or intptr_t,
or uintptr_t. I've attached the full set of warnings in case that is of any
assistance. Even though some of these warnings do not indicate actual bugs
it would still be best to use the correct type in order to avoid confusion
and warnings.

-- 
Bruce Dawson
third_party\libxml\src\parser.c(11675): warning C4311: 'type cast': pointer 
truncation from 'void *' to 'long'
third_party\libxml\src\parser.c(1318): warning C4312: 'type cast': conversion 
from 'long' to 'void *' of greater size
third_party\libxml\src\parser.c(1337): warning C4311: 'type cast': pointer 
truncation from 'void *' to 'long'
third_party\libxml\src\parser.c(1849): warning C4312: 'type cast': conversion 
from 'long' to 'void *' of greater size
third_party\libxml\src\parser.c(9190): warning C4311: 'type cast': pointer 
truncation from 'void *' to 'long'
third_party\libxml\src\relaxng.c(4401): warning C4312: 'type cast': conversion 
from 'long' to 'void *' of greater size
third_party\libxml\src\relaxng.c(4409): warning C4312: 'type cast': conversion 
from 'long' to 'void *' of greater size
third_party\libxml\src\relaxng.c(4413): warning C4312: 'type cast': conversion 
from 'long' to 'void *' of greater size
third_party\libxml\src\relaxng.c(4420): warning C4312: 'type cast': conversion 
from 'long' to 'void *' of greater size
third_party\libxml\src\relaxng.c(4424): warning C4312: 'type cast': conversion 
from 'long' to 'void *' of greater size
third_party\libxml\src\relaxng.c(9384): warning C4311: 'type cast': pointer 
truncation from 'void *' to 'long'
third_party\libxml\src\sax2.c(1911): warning C4312: 'type cast': conversion 
from 'long' to 'void *' of greater size
third_party\libxml\src\tree.c(4579): warning C4311: 'type cast': pointer 
truncation from 'void *const ' to 'long'
third_party\libxml\src\xmlio.c(3006): warning C4312: 'type cast': conversion 
from 'long' to 'void *' of greater size
third_party\libxml\src\xmlio.c(3112): warning C4312: 'type cast': conversion 
from 'long' to 'void *' of greater size
third_party\libxml\src\xmlio.c(829): warning C4311: 'type cast': pointer 
truncation from 'void *' to 'long'
third_party\libxml\src\xmlio.c(850): warning C4311: 'type cast': pointer 
truncation from 'void *' to 'long'
third_party\libxml\src\xmlio.c(868): warning C4311: 'type cast': pointer 
truncation from 'void *' to 'long'
third_party\libxml\src\xmlmemory.c(468): warning C4311: 'type cast': pointer 
truncation from 'void *' to 'unsigned long'
third_party\libxml\src\xpath.c(170): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(171): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(174): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(175): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(220): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(270): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(3236): warning C4312: 'type cast': conversion 
from 'long' to 'void *' of greater size
third_party\libxml\src\xpath.c(3328): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(3329): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(3333): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(3334): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(3391): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(3392): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(3396): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(3397): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(343): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(344): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(347): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(348): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(411): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(412): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(415): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxml\src\xpath.c(416): warning C4311: 'type cast': pointer 
truncation from 'xmlChar *' to 'long'
third_party\libxslt\libxslt\transform.c(5997): warning C4311: 'type cast': 
pointer truncation from 'xmlChar *' to 'long'
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml@gnome.org
https://mail.gnome.org/mailman/listinfo/xml

Reply via email to