{"action":"closed","issue":{"url":"https://api.github.com/repos/Microsoft/Microsoft.IO.RecyclableMemoryStream/issues/26","repository_url":"https://api.github.com/repos/Microsoft/Microsoft.IO.RecyclableMemoryStream","labels_url":"https://api.github.com/repos/Microsoft/Microsoft.IO.RecyclableMemoryStream/issues/26/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/Microsoft.IO.RecyclableMemoryStream/issues/26/comments","events_url":"https://api.github.com/repos/Microsoft/Microsoft.IO.RecyclableMemoryStream/issues/26/events","html_url":"https://github.com/Microsoft/Microsoft.IO.RecyclableMemoryStream/issues/26","id":159830425,"number":26,"title":"RecyclableMemoryStream exhausts all available RAM if requestedSize > (int.MaxValue - BlockSize)","user":{"login":"elgonzo","id":6129198,"avatar_url":"https://avatars.githubusercontent.com/u/6129198?v=3","gravatar_id":"","url":"https://api.github.com/users/elgonzo","html_url":"https://github.com/elgonzo","followers_url":"https://api.github.com/users/elgonzo/followers","following_url":"https://api.github.com/users/elgonzo/following{/other_user}","gists_url":"https://api.github.com/users/elgonzo/gists{/gist_id}","starred_url":"https://api.github.com/users/elgonzo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elgonzo/subscriptions","organizations_url":"https://api.github.com/users/elgonzo/orgs","repos_url":"https://api.github.com/users/elgonzo/repos","events_url":"https://api.github.com/users/elgonzo/events{/privacy}","received_events_url":"https://api.github.com/users/elgonzo/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-06-12T14:36:46Z","updated_at":"2016-09-12T21:55:42Z","closed_at":"2016-09-12T21:55:42Z","body":"When a RecyclableMemoryStream is created where _requestedSize_ satisfies the condition:\r\n\r\n`(numberOfRequiredBlocks * BlockSize) > int.MaxValue`\r\nwhere\r\n`numberOfRequiredBlocks = Ceil( requestedSize / blockSize )`\r\n\r\nthen the method _RecyclableMemoryStream.EnsureCapacity_ loops until all available virtual address space is exhausted (running x64 code with much more than 10GB RAM available; thus it is not a problem with regard to available RAM or 32bit virtual address space).\r\n\r\nThe problem occurs at the execution of the following [while loop](https://github.com/Microsoft/Microsoft.IO.RecyclableMemoryStream/blob/master/src/RecyclableMemoryStream.cs#L843-L846):\r\n\r\n```\r\nwhile (this.Capacity < newCapacity)\r\n{\r\n    blocks.Add((this.memoryManager.GetBlock()));\r\n}\r\n```\r\n\r\nWhen given a _requestedSize_ that satisfied the condition shown at the beginning of my post, the property _Capacity_ will eventually overflow, thus not allowing the while-loop to exit. (1)\r\n\r\nProposed fix: Either make the _Capacity_ property a _long_ type (2), or add a sanity check that prevents _Capacity_ from overflowing...\r\n\r\n\r\n____\r\nFoot notes:\r\n\r\n(1) Strictly speaking, whether the while-loop eventually exits depends -- aside from the virtual memory size -- on the chosen block size and an \"appropriate\" _requestedSize_ value. With the default block size of 128K, the Capacity property will overflow and eventually reach the value 0 again, thus turning the while-loop effectively into an infinite loop. When choosing some other 'odd' block size, Capacity property will still overflow, but not necessarily reach precisely 0 again, thus possibly allowing the while loop to eventually exit -- but chances are good that all RAM has been exhausted already before that would happen.\r\n\r\n(2) It would be nice if RecyclableMemoryStream would support stream sizes larger than 2GB (i.e. not using int types but long types for all concerned method arguments/variables/fields/properties). It is not really a show stopper though, as one can split large data blobs into multiple MemoryStream objects that are wrapped into a custom Stream class representing those multiple MemoryStreams as one continuous stream..."}}
{"actor":{"display_login":"doubleyewdee"}}