Permalink
Browse files

Icon: Handle bogus ContentLength values returned by the server

  • Loading branch information...
1 parent 0f7adcd commit a05d09515f29f55c991f0e2cd44b137777c93b7e @bl8 bl8 committed Mar 24, 2012
Showing with 8 additions and 2 deletions.
  1. +8 −2 src/Mono.Upnp/Mono.Upnp/Mono.Upnp/Icon.cs
@@ -137,9 +137,15 @@ public virtual byte[] GetData ()
try {
var request = (HttpWebRequest)WebRequest.Create (Url);
using (var response = Helper.GetResponse (request)) {
- data = new byte[response.ContentLength];
+ int response_length;
+ if (response.ContentLength < 1 || response.ContentLength > Int32.MaxValue) {
+ response_length = Int32.MaxValue;
+ } else {
+ response_length = (int)response.ContentLength;
+ }
+ data = new byte[response_length];
@gonzalop

gonzalop Mar 24, 2012

Collaborator

This will try to allocate Int32.MaxValue bytes if there is no content length!

using (var stream = response.GetResponseStream ()) {
- stream.Read (data, 0, (int)response.ContentLength);
+ stream.Read (data, 0, response_length);
@gonzalop

gonzalop Mar 24, 2012

Collaborator

There is no guarantee that stream.Read() will read all of response_length. You need a loop here, perhaps use a MemoryStream to store the data and so on...

}
}
} catch (WebException e) {

1 comment on commit a05d095

Member

bl8 commented on a05d095 Apr 21, 2012

Thanks for pointing out those issues. I was doing a quick fix at the time, but I should have gone for the proper fix.
I finally got around to fix this with this commit: 6278712

Please sign in to comment.