From 031c3cd85e9c9577bd504584edef4021f1f541c2 Mon Sep 17 00:00:00 2001
From: tkoenig <tkoenig@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Thu, 1 Jun 2006 19:23:56 +0000
Subject: [PATCH] 2006-06-01  Thomas Koenig  <Thomas.Koenig@online.de>

	PR fortran/27715
	* arith.c:  Cast the characters from the strings to unsigned
	char to avoid values less than 0 for extended ASCII.

2006-06-01  Thomas Koenig  <Thomas.Koenig@online.de>

	PR fortran/27715
	* gfortran.dg/extended_char_comparison_1.f:  New test.



git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@114317 138bc75d-0d04-0410-961f-82ee72b054a4
---
 gcc/fortran/ChangeLog                         |  6 ++++++
 gcc/fortran/arith.c                           |  6 ++++--
 gcc/testsuite/ChangeLog                       |  5 +++++
 .../gfortran.dg/extended_char_comparison_1.f  | 20 +++++++++++++++++++
 4 files changed, 35 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/extended_char_comparison_1.f

diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog
index eb093a75b6d1..44c61b68bd55 100644
--- a/gcc/fortran/ChangeLog
+++ b/gcc/fortran/ChangeLog
@@ -1,3 +1,9 @@
+2006-06-01  Thomas Koenig  <Thomas.Koenig@online.de>
+
+	PR fortran/27715
+	* arith.c:  Cast the characters from the strings to unsigned
+	char to avoid values less than 0 for extended ASCII.
+
 2006-06-01  Per Bothner  <bothner@bothner.com>
 
 	* data.c (gfc_assign_data_value): Handle USE_MAPPED_LOCATION.
diff --git a/gcc/fortran/arith.c b/gcc/fortran/arith.c
index a65447a92333..348b87fe136a 100644
--- a/gcc/fortran/arith.c
+++ b/gcc/fortran/arith.c
@@ -1133,8 +1133,10 @@ gfc_compare_string (gfc_expr * a, gfc_expr * b, const int *xcoll_table)
 
   for (i = 0; i < len; i++)
     {
-      ac = (i < alen) ? a->value.character.string[i] : ' ';
-      bc = (i < blen) ? b->value.character.string[i] : ' ';
+      /* We cast to unsigned char because default char, if it is signed,
+         would lead to ac<0 for string[i] > 127.  */
+      ac = (unsigned char) ((i < alen) ? a->value.character.string[i] : ' ');
+      bc = (unsigned char) ((i < blen) ? b->value.character.string[i] : ' ');
 
       if (xcoll_table != NULL)
 	{
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 7743fa08e9b3..fdce4c49228d 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2006-06-01  Thomas Koenig  <Thomas.Koenig@online.de>
+
+	PR fortran/27715
+	* gfortran.dg/extended_char_comparison_1.f:  New test.
+
 2006-06-01  Paul Thomas  <pault@gcc.gnu.org>
 
 	PR fortran/25098
diff --git a/gcc/testsuite/gfortran.dg/extended_char_comparison_1.f b/gcc/testsuite/gfortran.dg/extended_char_comparison_1.f
new file mode 100644
index 000000000000..311567dceb0c
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/extended_char_comparison_1.f
@@ -0,0 +1,20 @@
+! { dg-do run }
+! PR 27715 - the front end and the library used to have different ideas
+! about ordering for characters whose encoding is above 127.
+
+      program main
+      character*1 c1, c2
+      logical a1, a2
+      c1 = 'ç';
+      c2 = 'c';
+      a1 = c1 > c2;
+      call setval(c1, c2)
+      a2 = c1 > c2
+      if (a1 .neqv. a2) call abort
+      end
+
+      subroutine setval(c1, c2)
+      character*1 c1, c2
+      c1 = 'ç';
+      c2 = 'c';
+      end
-- 
GitLab