about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2013-07-10 00:01:58 -0700
committerAlex Crichton <alex@alexcrichton.com>2013-07-10 00:07:03 -0700
commit6d4d2c9a33ea0b92e98c22e84e76d3116dcd7444 (patch)
treeedd57744338137f22d6a50e6017d418d33ebbc6f /src/libstd
parente388a80c234d628c4d1fab77dc3e3f2c04cbefc5 (diff)
downloadrust-6d4d2c9a33ea0b92e98c22e84e76d3116dcd7444.tar.gz
rust-6d4d2c9a33ea0b92e98c22e84e76d3116dcd7444.zip
Don't loop infinitely on 0-size structs in repr
Closes #7625
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/repr.rs10
1 files changed, 8 insertions, 2 deletions
diff --git a/src/libstd/repr.rs b/src/libstd/repr.rs
index dd5075f8e66..79d11ddca61 100644
--- a/src/libstd/repr.rs
+++ b/src/libstd/repr.rs
@@ -206,11 +206,13 @@ impl ReprVisitor {
                            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 @@ impl ReprVisitor {
             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 test_repr() {
                "(10, ~\"hello\")");
     exact_test(&(10_u64, ~"hello"),
                "(10, ~\"hello\")");
+
+    struct Foo;
+    exact_test(&(~[Foo, Foo, Foo]), "~[{}, {}, {}]");
 }