[Mono-bugs] [Bug 45469][Min] New - Bad pointer arithmetic in System.Windows.Froms.SystemInformation._gethostname method

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
Wed, 25 Jun 2003 17:43:16 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by barce@frlp.utn.edu.ar.

http://bugzilla.ximian.com/show_bug.cgi?id=45469

--- shadow/45469	Wed Jun 25 17:43:16 2003
+++ shadow/45469.tmp.12347	Wed Jun 25 17:43:16 2003
@@ -0,0 +1,48 @@
+Bug#: 45469
+Product: Mono/Class Libraries
+Version: unspecified
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Minor
+Component: System
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: barce@frlp.utn.edu.ar               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Bad pointer arithmetic in System.Windows.Froms.SystemInformation._gethostname method
+
+File: /mcs/class/System.Windows.Forms/System.Windows.Forms/SystemInformation.cs
+
+This is the actual method implementation:
+
+static string _gethostname ()
+{
+	byte [] buf = new byte [256];
+	int count;
+	unsafe {
+		fixed (byte *p = &buf [0]){
+			byte *q = p;
+			gethostname (p, 256);
+	Bug Here ----->while (*q != 0 && ((int)(p-q) < 256))
+				q++;
+			count = (int) (q - p);
+		}
+	}
+	return new String(Encoding.UTF8.GetChars (buf, 0, count));
+}
+
+
+The line 
+  while (*q != 0 && ((int)(p-q) < 256))
+should be
+  while (*q != 0 && ((int)(q-p) < 256))
+
+(p-q) is always a negative number so it's always less than 256, the correct
+way to express the length of the string is (q-p).
+This problem manifests only when the host name length is greather than 256
+ bytes, which is not common.