Subscribe to The Madman Scribblings        RSS Feed
-----

Invoking Evil Code via the Portal To Hell method _()

Icon Leave Comment
Invoking Evil Code via the Portal To Hell method _()

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Evil_Overloading_Bounds;

namespace EVIL_Overload
{
  class Program
  {
    static void Main(string[] args)
    {
      var x = 4;
      if (0._() < x < 10) { Console.WriteLine("It is between"); }
    }
  }


}



So you probably guessed that it doing a between bounds check.
< has type signature int * int -> bool
thus the second must fail because you can do bool < int .

Normally you'd be write, if it wasn't for the "Portal To Hell" extension method _().
At least you know from that point on-wards that coder has done a deal with Beelzebub himself. (Which is nice)



The Portal To Hell (extension method).
[spoiler]

First part step through the Portal.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Evil_Overloading_Bounds
{
  static public class Exts
  {
    public static __0<T> _<T>(this T Value) where T : IComparable<T>
    { return new __0<T>(Value); }
  }



and into Hell

  public class __0<T> where T : IComparable<T>
  {
    private readonly T Value = default(T);
    internal __0(T Value) : base() { this.Value = Value; }

    public static __1<T> operator < (__0<T> Lower, T Value) { return new __1<T>(Lower.Value.CompareTo(Value) <  0, Value); }
    public static __1<T> operator <=(__0<T> Lower, T Value) { return new __1<T>(Lower.Value.CompareTo(Value) <= 0, Value); }
    [Obsolete("Do not use!",true)]
    public static __1<T> operator > (__0<T> Lower, T Value) { return new __1<T>(Lower.Value.CompareTo(Value) >  0, Value); }
    [Obsolete("Do not use!",true)]
    public static __1<T> operator >=(__0<T> Lower, T Value) { return new __1<T>(Lower.Value.CompareTo(Value) >= 0, Value); }


  }
  public class __1<T> where T : IComparable<T>
  {
    private readonly bool State;
    private readonly T Value = default(T);

    internal __1(bool Cmp, T Value) { this.Value = Value; State = Cmp; }
    public static bool operator < (__1<T> Value, T Upper) { return Value.State && (Value.Value.CompareTo(Upper) <  0); }
    public static bool operator <=(__1<T> Value, T Upper) { return Value.State && (Value.Value.CompareTo(Upper) <= 0); }
    [Obsolete("Do not use!",true)]
    public static bool operator > (__1<T> Value, T Upper) { return Value.State && (Value.Value.CompareTo(Upper) >  0); }
    [Obsolete("Do not use!",true)]
    public static bool operator >=(__1<T> Value, T Upper) { return Value.State && (Value.Value.CompareTo(Upper) >= 0); }

  }
}







Why the [Obsolete] ?

Unfortunately in C# you can't just overload < on its own, you also have overload it opposite >=.
Since I don't want the user to be using > and >=, mark them with the Obsolete attribute.
So if the user tries to use them, it result in a compile-time error.

 [Obsolete("Do not use!",true)]   

0 Comments On This Entry