Bugs - Enum.TryParse - C# .NET 4.0

mltgamesmltgames Posts: 17 Bronze 1
edited December 13, 2014 12:52PM in SmartAssembly
I have a big issue with Smart Assembly 6.8.0.121...

I have visual studio 2013 with update 3 and it's a C# .NET 4.0 project.

I solved the issue by removing the offuscation but this is a bad idea if we want our project secure. So i found a workaround...

The issue is with only Enum.TryParse... (This feature is new Inside .NET 4.0) and if you don't have any ToString() to a value Inside the enum. (Take note that Enum.Parse is working)



Here is my code
//////////////////////////////////////
// Result type.
//////////////////////////////////////
public enum eResult_Field
{
RESULT, // Si ça fonctionner. SUCCESS= approuvé
ACCOUNT, // # de carte
ACCOUNTTYPE, // TYPE DE CARTE
APPROVEDAMOUNT, // Montant approuver
APPROVALCODE, // Code d'approbation
TIPAMOUNT, // Quantité de tip
EXPIRATION, // Expiration de la carte.
RECEIPT, // Si on a un recu.
DESCRIPTION // Message
};

//////////////////////////////////////
// Check error fct
//////////////////////////////////////
public override string Check_Error()
{
eResult_Field eNode;
string sError = "";
if (Enum.TryParse("DESCRIPTION", true, out eNode)) <
This fonction return false even if i entered "DESCRIPTION" !
{
switch (eNode)
{
case eResult_Field.DESCRIPTION:
sError = "MY BIG ERROR";
break;
}
}

return sError;
}

When i execute this code it should find the enum DESCRIPTION and return true but it's not!

I also found something to fix the issue. (I also tried parse instead of try parse and it work.)

If by example, i add to my project
// PATCH FOR OFFUSCATION.
static string DeofucationTemp = eResult_Field.RESULT.ToString();

The first call ToString() to whatever enum value Inside it fix the issue.

Now if I execute the same fonction Check_Error() it return true and I get my error "MY BIG ERROR" instead of an empty string.

It look like the offuscator change the name Inside the enum but it make TryParse to do not work and do not reconize my string DESCRIPTION. But why it work with Parse? This is really strange.

I don't know why it's only TryParse having this issue but i think this issue should be solved because it can create alot of strange problem if people are using Enum.TryParse Inside their project.

Comments

  • Jessica RJessica R Posts: 1,319 Rose Gold 4
    Hello,

    Many thanks for your post and apologies for the delay in response!

    After looking into this, it looks like the error happens because TryParse is looking for a literal string value "DESCRIPTION" from the enum, but because "DESCRIPTION" has been renamed from obfuscation, it doesn't exist and TryParse fails.

    I'm afraid the only way to get around this is to exclude the enum from obfuscation (either by adding an exclusion from the UI or using the custom attributes - http://documentation.red-gate.com/displ ... attributes)

    The reason why using Parse or calling ToString on the enum first works is because SmartAssembly knows that calling either of these on the enum will fail if it is obfuscated. Because of this, it automatically excludes it from obfuscation - http://documentation.red-gate.com/displ ... not-enough

    I've added a feature request to automatically exclude enums called on by TryParse as well (internal reference SA-1867) as it seems we haven't added to the list since it was introduced with .NET 4.

    Apologies for any troubles and confusion caused by this! I hope this information helps.

    Jessica Ramos | Product Support Engineer | Redgate Software

    Have you visited our Help Center?


  • mltgamesmltgames Posts: 17 Bronze 1
    Thanks for your response, it worked! :)
  • TsherbertTsherbert Posts: 1 New member
    it looks like Switch("enum" ) also has an issue.

    Version 6.13.3.1314
    using the [DoNotObfuscateType] attribute fixes the issue, but the enum should automatically be excluded. 
Sign In or Register to comment.