Bug 28344 - FCallCompare (mono_decimal_compare) returns incorrect result on arm and s390x
Summary: FCallCompare (mono_decimal_compare) returns incorrect result on arm and s390x
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: unspecified
Hardware: Other Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-03-23 17:59 UTC by Neale Ferguson
Modified: 2015-03-25 01:33 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Neale Ferguson 2015-03-23 17:59:28 UTC
The following program works on 3.10.1 but fails on trunk:

using System;
using System.Reflection;
using System.IO;
using System.Text;

public class Class1
{
	static void Main()
	{
		decimal value = 100;
		if (value < (decimal) Int32.MinValue || value > (decimal) Int32.MaxValue)
			Console.WriteLine("Broken");
		else
			Console.WriteLine("Working");
	}
}

I've narrowed it down to the use of FCallCompare (mono_decimal_compare) that returns an incorrect value on arm and s390x. The code in question is the comparison of signs:

--- a/mono/metadata/decimal-ms.c
+++ b/mono/metadata/decimal-ms.c
@@ -2242,9 +2242,9 @@ mono_decimal_compare (MonoDecimal *left, MonoDecimal *right)
        }
 
        //
-       // Signs are different.  Used signed byte compares
+       // Signs are different.  Use signed byte comparison
        //
-       if ((char)left_sign > (char)right_sign)
+       if ((signed char)left_sign > (signed char)right_sign)
                return MONO_DECIMAL_CMP_GT;
        return MONO_DECIMAL_CMP_LT;
 }


Note the comment said "Used signed byte compares". However, it assumes that (char) == (signed chars) which for s390x is not true (it defaults to unsigned). By explicitly using (signed char) casting, the problem is fixed.

I pushed this change under commit ca5bebb67ce72ab267bee11685dcb5db13885474.

The purpose of this report is to highlight that some architectures default to unsigned char instead of signed.
Comment 1 Zoltan Varga 2015-03-25 01:33:45 UTC
-> FIXED.