From ef60c7cd49d80ef3f1f6a7030d4439024359f37a Mon Sep 17 00:00:00 2001
From: Tobias Bucher <tobiasbucher5991@gmail.com>
Date: Mon, 30 May 2016 09:53:09 +0200
Subject: [PATCH] Prevent the borrow counter from overflowing in `Ref::clone`

Fixes #33880.
---
 src/libcore/cell.rs | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs
index 4929088201d..97a85f6aa43 100644
--- a/src/libcore/cell.rs
+++ b/src/libcore/cell.rs
@@ -618,7 +618,9 @@ impl<'b> Clone for BorrowRef<'b> {
         // Since this Ref exists, we know the borrow flag
         // is not set to WRITING.
         let borrow = self.borrow.get();
-        debug_assert!(borrow != WRITING && borrow != UNUSED);
+        debug_assert!(borrow != UNUSED);
+        // Prevent the borrow counter from overflowing.
+        assert!(borrow != WRITING);
         self.borrow.set(borrow + 1);
         BorrowRef { borrow: self.borrow }
     }