From 47b3dd4ed61927c7dfc9ba870d9c0fa763ac57d8 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 11 Oct 2005 12:47:44 +0000 Subject: [PATCH] Avoid integer overflow on exotic platforms. --- lib/ChangeLog | 10 +++++++++- lib/strcasecmp.c | 9 ++++++++- lib/strncasecmp.c | 11 +++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index d51d99e81..9b0ed2c6a 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,11 @@ +2005-10-11 Bruno Haible + + * strcasecmp.c: Include limits.h. + (strcasecmp): Avoid integer overflow on exotic platforms. + * strncasecmp.c: Include limits.h. + (strncasecmp): Avoid integer overflow on exotic platforms. + Reported by Paul Eggert. + 2005-10-06 Simon Josefsson * hmac-md5.c: New file. @@ -2803,7 +2811,7 @@ 2004-09-08 Bruno Haible - * stdint_.h.in: New file, taken from GNU clisp. + * stdint_.h: New file, taken from GNU clisp. 2004-09-08 Oskar Liljeblad diff --git a/lib/strcasecmp.c b/lib/strcasecmp.c index 71f2eca7c..c1bac0a5a 100644 --- a/lib/strcasecmp.c +++ b/lib/strcasecmp.c @@ -25,6 +25,7 @@ #include "strcase.h" #include +#include #if HAVE_MBRTOWC # include "mbuiter.h" @@ -93,6 +94,12 @@ strcasecmp (const char *s1, const char *s2) } while (c1 == c2); - return c1 - c2; + if (UCHAR_MAX <= INT_MAX) + return c1 - c2; + else + /* On machines where 'char' and 'int' are types of the same size, the + difference of two 'unsigned char' values - including the sign bit - + doesn't fit in an 'int'. */ + return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0); } } diff --git a/lib/strncasecmp.c b/lib/strncasecmp.c index 72ead013b..0209c39ea 100644 --- a/lib/strncasecmp.c +++ b/lib/strncasecmp.c @@ -1,5 +1,5 @@ /* strncasecmp.c -- case insensitive string comparator - Copyright (C) 1998, 1999 Free Software Foundation, Inc. + Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc. 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 @@ -23,6 +23,7 @@ #include "strcase.h" #include +#include #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) @@ -54,5 +55,11 @@ strncasecmp (const char *s1, const char *s2, size_t n) } while (c1 == c2); - return c1 - c2; + if (UCHAR_MAX <= INT_MAX) + return c1 - c2; + else + /* On machines where 'char' and 'int' are types of the same size, the + difference of two 'unsigned char' values - including the sign bit - + doesn't fit in an 'int'. */ + return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0); } -- 2.11.0