Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Back Room
  4. Interesting way to bugger up your code

Interesting way to bugger up your code

Scheduled Pinned Locked Moved The Back Room
security
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #1

    Try the following code if you want to confuse other people (and yourself!):

    using System;

    class Test
    {
    const string A = "Hello!";

    /// <summary>
    /// I need a :beer:!
    /// </summary>
    static void Main()
    {
        string B = A;
        string C = "Hello!";
        
        buggerUpMyCode(B);
        
        Console.WriteLine("This is bad enough...");
        Console.WriteLine("A={0}", A);
        Console.WriteLine("B={0}", B);
        Console.WriteLine("A==B: {0}", A==B);
        Console.WriteLine("C={0}", C);
        Console.WriteLine("A==C: {0}", A==C);
        Console.WriteLine("\\"Hello!\\" = {0}", "Hello!");
        Console.WriteLine("A==\\"Hello!\\": {0}", A=="Hello!");
        Console.WriteLine("A==\\"Yellow\\": {0}", A=="Yellow");
        //
        // :wtf:
        //
        
        buggerUpMyCodeSomeMore("True", "Faux");
        buggerUpMyCodeSomeMore("False", "Vrai ");
        
        Console.WriteLine("\\n...but what about this!");
        Console.WriteLine("A==B: {0}", A==B);
        Console.WriteLine("A==\\"Test\\": {0}", A=="Test");
        //
        // :omg:
        //
    }
    
    /// <summary>
    /// D'Oh!
    /// </summary>
    unsafe static void buggerUpMyCode(string A)
    {
        fixed(char\* pA = A)
        {
            pA\[0\] = 'Y';
            pA\[5\] = 'w';
        }
    }
    
    /// <summary>
    /// Hate world. Revenge soon. Take out on everybody.
    /// </summary>
    unsafe static void buggerUpMyCodeSomeMore(string A, string B)
    {
        fixed(char\* pA = A)
        {
            for (int i=0; i  < A.Length && i < B.Length; i++)
                pA\[i\] = B\[i\];
        }
    }
    

    }

    Just imagine the hours of "fun" you could cause for other developers by misusing this in a component! * Maybe it's time to review those code access security permissions. Or maybe it's just time I got a life! ;P * Note: In accordance with Microsoft specifications, "fun" means "mind-numbing, folicle-wrenching agony".

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    PJ ArendsP 1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      Try the following code if you want to confuse other people (and yourself!):

      using System;

      class Test
      {
      const string A = "Hello!";

      /// <summary>
      /// I need a :beer:!
      /// </summary>
      static void Main()
      {
          string B = A;
          string C = "Hello!";
          
          buggerUpMyCode(B);
          
          Console.WriteLine("This is bad enough...");
          Console.WriteLine("A={0}", A);
          Console.WriteLine("B={0}", B);
          Console.WriteLine("A==B: {0}", A==B);
          Console.WriteLine("C={0}", C);
          Console.WriteLine("A==C: {0}", A==C);
          Console.WriteLine("\\"Hello!\\" = {0}", "Hello!");
          Console.WriteLine("A==\\"Hello!\\": {0}", A=="Hello!");
          Console.WriteLine("A==\\"Yellow\\": {0}", A=="Yellow");
          //
          // :wtf:
          //
          
          buggerUpMyCodeSomeMore("True", "Faux");
          buggerUpMyCodeSomeMore("False", "Vrai ");
          
          Console.WriteLine("\\n...but what about this!");
          Console.WriteLine("A==B: {0}", A==B);
          Console.WriteLine("A==\\"Test\\": {0}", A=="Test");
          //
          // :omg:
          //
      }
      
      /// <summary>
      /// D'Oh!
      /// </summary>
      unsafe static void buggerUpMyCode(string A)
      {
          fixed(char\* pA = A)
          {
              pA\[0\] = 'Y';
              pA\[5\] = 'w';
          }
      }
      
      /// <summary>
      /// Hate world. Revenge soon. Take out on everybody.
      /// </summary>
      unsafe static void buggerUpMyCodeSomeMore(string A, string B)
      {
          fixed(char\* pA = A)
          {
              for (int i=0; i  < A.Length && i < B.Length; i++)
                  pA\[i\] = B\[i\];
          }
      }
      

      }

      Just imagine the hours of "fun" you could cause for other developers by misusing this in a component! * Maybe it's time to review those code access security permissions. Or maybe it's just time I got a life! ;P * Note: In accordance with Microsoft specifications, "fun" means "mind-numbing, folicle-wrenching agony".

      PJ ArendsP Offline
      PJ ArendsP Offline
      PJ Arends
      wrote on last edited by
      #2

      Weird:confused: Care to explain why? Output is:

      This is bad enough...
      A=Yellow
      B=Yellow
      A==B: True
      C=Yellow
      A==C: True
      "Hello!" = Yellow
      A=="Hello!": True
      A=="Yellow": False

      ...but what about this!
      A==B: Faux
      A=="Test": Vrai
      Press any key to continue

      Oh well, just another reason to stick with C++.


      CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!

      Within you lies the power for good; Use it!

      J 1 Reply Last reply
      0
      • PJ ArendsP PJ Arends

        Weird:confused: Care to explain why? Output is:

        This is bad enough...
        A=Yellow
        B=Yellow
        A==B: True
        C=Yellow
        A==C: True
        "Hello!" = Yellow
        A=="Hello!": True
        A=="Yellow": False

        ...but what about this!
        A==B: Faux
        A=="Test": Vrai
        Press any key to continue

        Oh well, just another reason to stick with C++.


        CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!

        J Offline
        J Offline
        James T Johnson
        wrote on last edited by
        #3

        Welcome to string interning :) What happens is the C# compiler automatically adds any string literals to a table of strings. Each value in this table is a unique string and all hardcoded uses of that string point back to that particular string in the table. When he uses unsafe to code to modify the actual values of the string he is making modifications to the string table. The only way to accomplish this is to use unsafe code because part of the basic contract of a string object is that its contents do not change.

        James Sig code stolen from David Wulff

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups