[xml] [PATCH] Signed vs. unsigned comparison warnings in dict.c

2015-07-21 Thread Peter Kasting
When compiled in MSVC, dict.c triggers warning C4018 for comparing signed
and unsigned values in two places.

In both cases, the code basically does:

if (pool-end - pool-free  unsigned int) ...

The type of the LHS here is ptrdiff_t, which is signed.  When comparing
signed to unsigned values, the compiler will convert the signed value to an
unsigned value.  So if pool-end  pool-free, the comparison will almost
always succeed, which was probably not the intent.

The attached patch is one conservative way to fix this, which should be
correct in all cases on all platforms.  Another route, if you know that
pool-end = pool-free in all cases, would be to simply cast to size_t
without additional checks.  Yet another route, if you knew the above AND
that the difference would fit in an unsigned int, would be to
unconditionally cast to unsigned int.

This seems to be the only place preventing Chromium from compiling libxml
with this warning enabled (which is the default state for this warning), so
it would be nice to fix.  The attached patch was generated from an older
copy of the libxml sources, but I think should still apply (possibly with a
slight offset) to the current tree.

PK


xml_signedness.patch
Description: Binary data
___
xml mailing list, project page  http://xmlsoft.org/
xml@gnome.org
https://mail.gnome.org/mailman/listinfo/xml


Re: [xml] [PATCH] Signed vs. unsigned comparison warnings in dict.c

2015-07-21 Thread Peter Kasting
On Tue, Jul 21, 2015 at 11:19 AM, Peter Kasting pkast...@google.com wrote:

 The attached patch is one conservative way to fix this, which should be
 correct in all cases on all platforms.


For some reason on the archives the patch downloads as a .bin file instead
of a text file.  While you can simply rename the patch or apply it anyway
(the contents were OK), here's a re-attached version with a .txt file
ending in hopes the name won't get mangled.

PK
diff --git a/dict.c b/dict.c
index 5f71d55..a9ff53e 100644
--- a/dict.c
+++ b/dict.c
@@ -249,7 +249,7 @@ xmlDictAddString(xmlDictPtr dict, const xmlChar *name, 
unsigned int namelen) {
 #endif
 pool = dict-strings;
 while (pool != NULL) {
-   if (pool-end - pool-free  namelen)
+   if (pool-end  pool-free  (size_t)(pool-end - pool-free)  
namelen)
goto found_pool;
if (pool-size  size) size = pool-size;
 limit += pool-size;
@@ -317,7 +317,8 @@ xmlDictAddQString(xmlDictPtr dict, const xmlChar *prefix, 
unsigned int plen,
 #endif
 pool = dict-strings;
 while (pool != NULL) {
-   if (pool-end - pool-free  namelen + plen + 1)
+   if (pool-end  pool-free 
+  (size_t)(pool-end - pool-free)  namelen + plen + 1)
goto found_pool;
if (pool-size  size) size = pool-size;
 limit += pool-size;
___
xml mailing list, project page  http://xmlsoft.org/
xml@gnome.org
https://mail.gnome.org/mailman/listinfo/xml