Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6005215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:22:07+00:00 2026-05-23T01:22:07+00:00

I stumbled upon this code: static void Main() { typeof(string).GetField(Empty).SetValue(null, evil);//from DailyWTF Console.WriteLine(String.Empty);//check //how

  • 0

I stumbled upon this code:

static void Main()
{
    typeof(string).GetField("Empty").SetValue(null, "evil");//from DailyWTF

    Console.WriteLine(String.Empty);//check

    //how does it behave?
    if ("evil" == String.Empty) Console.WriteLine("equal"); 

    //output: 
    //evil 
    //equal

 }

and I wonder how is it even possible to compile this piece of code. My reasoning is:

According to MSDN String.Empty is read-only therefore changing it should be impossible and compiling should end with “A static readonly field cannot be assigned to” or similar error.

I thought Base Class Library assemblies are somehow protected and signed and whatnot to prevent exactly this kind of attack. Next time someone may change System.Security.Cryptography or another critical class.

I thought Base Class Library assemblies are compiled by NGEN after .NET installation therefore changing fields of String class should require advanced hacking and be much harder.

And yet this code compiles and works. Can somebody please explain what is wrong with my reasoning?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:22:08+00:00Added an answer on May 23, 2026 at 1:22 am

    A static readonly field cannot be assigned to

    You’re not assigning to it. You’re calling public functions in the System.Reflection namespace. No reason for the compiler to complain about that.

    Besides, typeof(string).GetField("Empty") could use variables entered in by the user instead, there’s no sure way for the compiler to tell in all cases whether the argument to GetField will end up being "Empty".

    I think you’re wanting Reflection to see that the field is marked initonly and throw an error at runtime. I can see why you would expect that, yet for white-box testing, even writing to initonly fields has some application.

    The reason NGEN has no effect is that you’re not modifying any code here, only data. Data is stored in memory with .NET just as with any other language. Native programs may use readonly memory sections for things like string constants, but the pointer to the string is generally still writable and that is what is happening here.

    Note that your code must be running with full-trust to use reflection in this questionable way. Also, the change only affect one program, this isn’t any sort of a security vulnerability as you seem to think (if you’re running malicious code inside your process with full trust, that design decision is the security problem, not reflection).


    Further note that the values of initonly fields inside mscorlib.dll are global invariants of the .NET runtime. After breaking them, you can’t even reliably test whether the invariant was broken, because the code to inspect the current value of System.String.Empty has also broken, because you’ve violated its invariants. Start violating system invariants and nothing can be relied on.

    By specifying these values inside the .NET specifications, it enables the compiler to implement a whole bunch of performance optimizations. Just a simple one is that

    s == System.String.Empty
    

    and

    (s != null) && (s.Length == 0)
    

    are equivalent, but the latter is much faster (relatively speaking).

    Also the compiler can determine that

    if (int.Parse(s) > int.MaxValue)
    

    is never true, and generate an unconditional jump to the else block (it still has to call Int32.Parse to have the same exception behavior, but the comparison can be removed).

    System.String.Empty is also used extensively inside BCL implementations. If you overwrite it, all sorts of crazy things can happen, including damage that leaks outside your program (for example you might write to a file whose name is built using string manipulation… when string breaks, you might overwrite the wrong file)


    And the behavior might easily differ between .NET versions. Normally when new optimization opportunities are found, they don’t get backported to previous versions of the JIT compiler (and even if they were, there could be installations from before the backport was implemented). In particular. String.Empty-related optimizations are observably different between .NET 2.x and Mono and .NET 4.5+.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I stumbled upon this code and I am curious as to what use may
I recently stumbled upon Etherpad, it's a collaborative writing tool http://code.google.com/p/etherpad/ - main project
I stumbled upon this question from two years ago. Is there a way to
I've stumbled upon this on ruby code. I know \d{4})\/(\d\d)\/(\d\d)\/(.*)/ means but what is
I stumbled upon this oddity today while playing with some code to go down
I've been reading some OSS code lately and stumbled upon this peculiar piece: class
Hey, I stumbled upon this site looking for solutions for event overlaps in mySQL
I'm revisiting som old code of mine and have stumbled upon a method for
I stumbled across this code and am too proud to go and ask the
So I'm trying to install a web application and I stumbled upon this question:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.