From a07cb85c2f451f8d3222c243586bdf07887e4285 Mon Sep 17 00:00:00 2001 From: Thomas Koenig Date: Tue, 30 Aug 2011 21:36:48 +0000 Subject: [PATCH] backport: re PR libfortran/50192 (Wrong character comparision with wide strings) 2011-08-30 Thomas Koenig Backport from trunk PR libfortran/50192 * intrinsics/string_intrinsics.c (memcmp_char4): New function. * intrinsics/string_intrinsics_inc.c: New macro MEMCMP, either set to memcmp or memcmp_char4. (compare_string): Use MEMCMP, with correct size for it. * libgfortran.h: Add prototype for memcmp_char4. 2011-08-30 Thomas Koenig Backport from trunk PR libfortran/50192 * gfortran.dg/widechar_compare_1.f90: New test. From-SVN: r178341 --- gcc/testsuite/ChangeLog | 6 ++++++ .../gfortran.dg/widechar_compare_1.f90 | 10 +++++++++ libgfortran/ChangeLog | 10 +++++++++ libgfortran/intrinsics/string_intrinsics.c | 21 +++++++++++++++++++ .../intrinsics/string_intrinsics_inc.c | 2 +- libgfortran/libgfortran.h | 4 ++++ 6 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/widechar_compare_1.f90 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 668b0eeb99f1..81fcfe153441 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2011-08-30 Thomas Koenig + + Backport from trunk + PR libfortran/50192 + * gfortran.dg/widechar_compare_1.f90: New test. + 2011-08-30 Tobias Burnus PR fortran/50163 diff --git a/gcc/testsuite/gfortran.dg/widechar_compare_1.f90 b/gcc/testsuite/gfortran.dg/widechar_compare_1.f90 new file mode 100644 index 000000000000..44101104cc11 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/widechar_compare_1.f90 @@ -0,0 +1,10 @@ +! { dg-do run } +! PR 50192 - on little-endian systems, this used to fail. +program main + character(kind=4,len=2) :: c1, c2 + c1 = 4_' ' + c2 = 4_' ' + c1(1:1) = transfer(257, mold=c1(1:1)) + c2(1:1) = transfer(64, mold=c2(1:1)) + if (c1 < c2) call abort +end program main diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog index a2da9b235d33..4db2645500c9 100644 --- a/libgfortran/ChangeLog +++ b/libgfortran/ChangeLog @@ -1,3 +1,13 @@ +2011-08-30 Thomas Koenig + + Backport from trunk + PR libfortran/50192 + * intrinsics/string_intrinsics.c (memcmp_char4): New function. + * intrinsics/string_intrinsics_inc.c: New macro MEMCMP, either + set to memcmp or memcmp_char4. + (compare_string): Use MEMCMP, with correct size for it. + * libgfortran.h: Add prototype for memcmp_char4. + 2011-08-19 Tobias Burnus Backport from mainline diff --git a/libgfortran/intrinsics/string_intrinsics.c b/libgfortran/intrinsics/string_intrinsics.c index e2e14e0d0dbf..a1d3b31abbbe 100644 --- a/libgfortran/intrinsics/string_intrinsics.c +++ b/libgfortran/intrinsics/string_intrinsics.c @@ -51,6 +51,23 @@ memset_char4 (gfc_char4_t *b, gfc_char4_t c, size_t len) return b; } +/* Compare wide character types, which are handled internally as + unsigned 4-byte integers. */ +int +memcmp_char4 (const void *a, const void *b, size_t len) +{ + const GFC_UINTEGER_4 *pa = a; + const GFC_UINTEGER_4 *pb = b; + while (len-- > 0) + { + if (*pa != *pb) + return *pa < *pb ? -1 : 1; + pa ++; + pb ++; + } + return 0; +} + /* All other functions are defined using a few generic macros in string_intrinsics_inc.c, so we avoid code duplication between the @@ -64,6 +81,8 @@ memset_char4 (gfc_char4_t *b, gfc_char4_t c, size_t len) #define SUFFIX(x) x #undef MEMSET #define MEMSET memset +#undef MEMCMP +#define MEMCMP memcmp #include "string_intrinsics_inc.c" @@ -76,6 +95,8 @@ memset_char4 (gfc_char4_t *b, gfc_char4_t c, size_t len) #define SUFFIX(x) x ## _char4 #undef MEMSET #define MEMSET memset_char4 +#undef MEMCMP +#define MEMCMP memcmp_char4 #include "string_intrinsics_inc.c" diff --git a/libgfortran/intrinsics/string_intrinsics_inc.c b/libgfortran/intrinsics/string_intrinsics_inc.c index 64f5cd75a85c..8335a38d9491 100644 --- a/libgfortran/intrinsics/string_intrinsics_inc.c +++ b/libgfortran/intrinsics/string_intrinsics_inc.c @@ -90,7 +90,7 @@ compare_string (gfc_charlen_type len1, const CHARTYPE *s1, gfc_charlen_type len; int res; - res = memcmp (s1, s2, ((len1 < len2) ? len1 : len2) * sizeof (CHARTYPE)); + res = MEMCMP (s1, s2, ((len1 < len2) ? len1 : len2)); if (res != 0) return res; diff --git a/libgfortran/libgfortran.h b/libgfortran/libgfortran.h index 7080b1d81f96..d7f9ffd41b8a 100644 --- a/libgfortran/libgfortran.h +++ b/libgfortran/libgfortran.h @@ -1286,6 +1286,10 @@ extern int compare_string_char4 (gfc_charlen_type, const gfc_char4_t *, gfc_charlen_type, const gfc_char4_t *); iexport_proto(compare_string_char4); +extern int memcmp_char4 (const void *, const void *, size_t); +internal_proto(memcmp_char4); + + /* random.c */ extern void random_seed_i4 (GFC_INTEGER_4 * size, gfc_array_i4 * put, -- 2.47.2