{"action":"opened","issue":{"url":"https://api.github.com/repos/SlapperAutoMapper/Slapper.AutoMapper/issues/22","repository_url":"https://api.github.com/repos/SlapperAutoMapper/Slapper.AutoMapper","labels_url":"https://api.github.com/repos/SlapperAutoMapper/Slapper.AutoMapper/issues/22/labels{/name}","comments_url":"https://api.github.com/repos/SlapperAutoMapper/Slapper.AutoMapper/issues/22/comments","events_url":"https://api.github.com/repos/SlapperAutoMapper/Slapper.AutoMapper/issues/22/events","html_url":"https://github.com/SlapperAutoMapper/Slapper.AutoMapper/issues/22","id":133930404,"number":22,"title":"Slapper.AutoMapper vulnerable to collisions","user":{"login":"KooKiz","id":11140081,"avatar_url":"https://avatars.githubusercontent.com/u/11140081?v=3","gravatar_id":"","url":"https://api.github.com/users/KooKiz","html_url":"https://github.com/KooKiz","followers_url":"https://api.github.com/users/KooKiz/followers","following_url":"https://api.github.com/users/KooKiz/following{/other_user}","gists_url":"https://api.github.com/users/KooKiz/gists{/gist_id}","starred_url":"https://api.github.com/users/KooKiz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KooKiz/subscriptions","organizations_url":"https://api.github.com/users/KooKiz/orgs","repos_url":"https://api.github.com/users/KooKiz/repos","events_url":"https://api.github.com/users/KooKiz/events{/privacy}","received_events_url":"https://api.github.com/users/KooKiz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-02-16T09:39:22Z","updated_at":"2016-02-16T09:39:22Z","closed_at":null,"body":"Hello,\r\n\r\nI met an annoying bug with Slapper.AutoMapper. Here is a small repro:\r\n\r\n`    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            var id2 = typeof(Employee).GetHashCode() - typeof(Contract).GetHashCode();\r\n\r\n            var source = new List<object>();\r\n\r\n            dynamic obj1 = new ExpandoObject();\r\n\r\n            obj1.Id = 1;\r\n            obj1.Contracts_Id = id2;\r\n\r\n            source.Add(obj1);\r\n\r\n            dynamic obj2 = new ExpandoObject();\r\n\r\n            obj2.Id = 1;\r\n            obj2.Contracts_Id = id2 + 1;\r\n\r\n            source.Add(obj2);\r\n            \r\n            var result = Slapper.AutoMapper.MapDynamic<Employee>(source).First();\r\n        }\r\n    }\r\n\r\n    public class Employee\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public List<Contract> Contracts { get; set; }\r\n\r\n        public override int GetHashCode()\r\n        {\r\n            return this.Id;\r\n        }\r\n    }\r\n\r\n    public class Contract\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public override int GetHashCode()\r\n        {\r\n            return this.Id;\r\n        }\r\n    }\r\n`\r\n\r\nThe problem here is that both the parent object (Employee) and the child object (Contract) will have the same computed key in the instance cache. Therefore, the method Slapper.AutoMapper.InternalHelpers.GetInstance will retrieve the previous Employee instance when checking if there's a previous Contract in the cache, and it'll result in a cast error.\r\n\r\nThe repro project may look unlikely (with all the GetHashCode overriding) but the collision issue actually happened to me with real code (without any overriding). \r\n\r\nDigging into this code actually got me worried. This time the collision resulted in a cast error so I could notice it easily. But what if computing the key resulted in a collision for two objects of the same type? It's very unlikely for an object with a single identifier (still possible though, GetHashCode provides no guarantee to be absolutely unique: http://stackoverflow.com/a/11607221/869621), the probability increases as objects use multiple identifiers, since you just sum the hashcodes of each of the identifier (thus increasing the probability of collision). \r\n\r\nI'm sorry to say, but the code of Slapper.AutoMapper.InternalHelpers.GetInstance is fundamentally broken because it's based on false assumptions about the behavior of the GetHashCode method.\r\n"}}