Decompiling code problems

2 posts, 0 answers
  1. Riccardo
    Riccardo avatar
    3 posts
    Member since:
    Jul 2011

    Posted 19 Nov 2011 Link to this post

    Hello, i'll share with you some problems i noticed in decompiling one of my assemblies (so i have the original code to compare with).

    1)
    Original code:
    public abstract class AbStruct
        {
     
            public virtual string NomeStruttura { get { return "AbStruct"; } }
     
        }
     
    public class ConcStruct : AbStruct
        {
     
            protected XmlDocument xd = new XmlDocument();
     
            internal ConcStruct(string xml)
            {
                this.XmlString = xml;
            }
     
            public string XmlString { get; set; }
     
            public override string NomeStruttura { get { return "ConcStruct"; } }
     
        }

    Decompiled code:
    public abstract class AbStruct
    {
        public string NomeStruttura
        {
            get
            {
                return "AbStruct";
            }
        }
     
        protected AbStruct()
        {
        }
    }
     
    public class ConcStruct : AbStruct
    {
        protected XmlDocument xd;
     
        public string NomeStruttura
        {
            get
            {
                return "ConcStruct";
            }
        }
     
        public string XmlString
        {
            get;
            set;
        }
     
        internal ConcStruct(string xml)
        {
            this.xd = new XmlDocument();
            base();
            this.XmlString = xml;
        }
    }

    As you see, in AbStruct, property NomeStruttura has no virtual keyword and in ConcStruct, NomeStruttura has no override keyword.
    Plus, in ConcStruct constructor, there's a call to base() (that produces a compiler error) that disappears if i instantiate the xd variable inside the constructor instead of doing it in the same line where it's decleared.

    2)
    Original code
    public string IntToString(int n)
    {
        return n.ToString();
    }

    Decompiled code
    public string IntToString(int n)
    {
        return &n.ToString();
    }

    You can notice the & before n, maybe because int is a value type, but this generates a compiler error unless using unsafe context (and it has no sense to use it in this case)

    3)
    Original code
    public class Class
        {
     
            public void Method(XmlTextWriter writer)
            {
                writer.WriteElementString("Node", this.Field.ToString());
            }
     
     
            public long Field { get; set; }
     
        }

    Decompiled code
    public class Class
    {
        public long Field
        {
            get;
            set;
        }
     
        public Class()
        {
        }
     
        public void Method(XmlTextWriter writer)
        {
            "Node".WriteElementString(long field = this.Field, field.ToString());
        }
    }

    The body of Method is completely wrong

    4)
    Original code
    public class Class
        {
     
            public void Method()
            {
                string a = "a";
                string b = "b";
                this.flag = (a == b);
            }
     
            public bool flag { get; set; }
     
        }

    Decompiled code
    public class Class
    {
        public bool flag
        {
            get;
            set;
        }
     
        public Class()
        {
        }
     
        public void Method()
        {
            string a = "a";
            string b = "b";
            this.flag = string.op_Equality(a, b);
        }
    }

    this.flag assignment, generates a compiler error because the == operator can't be called explicitly (i translated the error description from italian but i think it's similar to english)

    5)
    Original code
    public class Class
        {
     
            public void Method()
            {
                int b = 0;
                const string a = "k";
                switch (this.Tipo)
                {
                    case a:
                        b = 1;
                        break;
                    case "b":
                        b = 2;
                        break;
                }
            }
     
            public string Tipo { get; set; }
     
        }

    Decompiled code
    public class Class
    {
        public string Tipo
        {
            get;
            set;
        }
     
        public Class()
        {
        }
     
        public void Method()
        {
            int b = 0;
            string tipo = this.Tipo;
            if (tipo != null && (tipo == "k" || tipo != "b"))
            {
                b = 1;
            }
        }
    }

    There are a lot of problems with the switch instruction, this is just one of the cases but i think you are aware of this

    6)
    Original code
    public virtual string ToXml()
            {
                string xmlstring;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
                    {
                        // Questo metodo dovrà essere definito nella sottoclasse
                        ToXml(writer);
                        // Utilizzo questo metodo per convertire in stringa, altrimenti il web service non la accetta (ad esempio usando Encoding.GetString)
                        using (StreamReader reader = new StreamReader(ms, Encoding.UTF8))
                        {
                            ms.Position = 0;
                            xmlstring = reader.ReadToEnd();
                        }
                    }
                }
                return xmlstring;
            }

    Decompiled code
    public virtual string ToXml()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
                {
                    this.ToXml(writer);
                    using (StreamReader reader = new StreamReader(ms, Encoding.UTF8))
                    {
                        ms.Position = (long)0;
                    }
                }
            }
        }

    The decompiled method is missing the instruction after ms.Position = 0 and the return statement

    This is all i found out till now, if i notice something more i'll let you know.
    I hope this helps you improving your outstanding product!

    Riccardo
  2. Nikolay G Rusev
    Admin
    Nikolay G Rusev avatar
    44 posts

    Posted 25 Nov 2011 Link to this post

    Hello Riccardo,

    Thank you for the feedback and the great examples. We are currently doing some major changes in our decompilation engine, that will address multiple issues, some of which are shown in your examples.
    This is how these changes will affect the problems you mentioned:

    2, 3, 5, 6) The changes we are making right now should fix this issues.

    1, 4) Not going to be fixed after the forthcoming major changes. This does not mean that we are not going to fix them, just that they will be taken care of once we are ready with the engine.

    Also I've updated your Telerik points for helping us with these examples.

    Stay tuned for our new versions which will bring fixes for many of the issues.

    Greetings,
    Nikolay G Rusev
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
Back to Top