]> git.lizzy.rs Git - rust.git/commitdiff
Don't loop infinitely on 0-size structs in repr
authorAlex Crichton <alex@alexcrichton.com>
Wed, 10 Jul 2013 07:01:58 +0000 (00:01 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 10 Jul 2013 07:07:03 +0000 (00:07 -0700)
Closes #7625

src/libstd/repr.rs

index dd5075f8e66887a2913c5ff6036ddee219e1eb6e..79d11ddca6139b0546d4613bf1f34c4023b16099 100644 (file)
@@ -206,11 +206,13 @@ pub fn write_vec_range(&self,
                            inner: *TyDesc)
                            -> bool {
         let mut p = ptr;
-        let end = ptr::offset(p, len);
         let (sz, al) = unsafe { ((*inner).size, (*inner).align) };
         self.writer.write_char('[');
         let mut first = true;
-        while (p as uint) < (end as uint) {
+        let mut left = len;
+        // unit structs have 0 size, and don't loop forever.
+        let dec = if sz == 0 {1} else {sz};
+        while left > 0 {
             if first {
                 first = false;
             } else {
@@ -219,6 +221,7 @@ pub fn write_vec_range(&self,
             self.write_mut_qualifier(mtbl);
             self.visit_ptr_inner(p as *c_void, inner);
             p = align(ptr::offset(p, sz) as uint, al) as *u8;
+            left -= dec;
         }
         self.writer.write_char(']');
         true
@@ -635,4 +638,7 @@ fn exact_test<T>(t: &T, e:&str) {
                "(10, ~\"hello\")");
     exact_test(&(10_u64, ~"hello"),
                "(10, ~\"hello\")");
+
+    struct Foo;
+    exact_test(&(~[Foo, Foo, Foo]), "~[{}, {}, {}]");
 }