Skip to content

Commit 823e394

Browse files
committed
Adjust tests for isize::MAX allocation always being checked
1 parent f68f1cb commit 823e394

File tree

3 files changed

+142
-311
lines changed

3 files changed

+142
-311
lines changed

alloc/tests/string.rs

+38-88
Original file line numberDiff line numberDiff line change
@@ -693,12 +693,6 @@ fn test_try_reserve() {
693693
const MAX_CAP: usize = isize::MAX as usize;
694694
const MAX_USIZE: usize = usize::MAX;
695695

696-
// On 16/32-bit, we check that allocations don't exceed isize::MAX,
697-
// on 64-bit, we assume the OS will give an OOM for such a ridiculous size.
698-
// Any platform that succeeds for these requests is technically broken with
699-
// ptr::offset because LLVM is the worst.
700-
let guards_against_isize = usize::BITS < 64;
701-
702696
{
703697
// Note: basic stuff is checked by test_reserve
704698
let mut empty_string: String = String::new();
@@ -712,35 +706,19 @@ fn test_try_reserve() {
712706
panic!("isize::MAX shouldn't trigger an overflow!");
713707
}
714708

715-
if guards_against_isize {
716-
// Check isize::MAX + 1 does count as overflow
717-
assert_matches!(
718-
empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
719-
Err(CapacityOverflow),
720-
"isize::MAX + 1 should trigger an overflow!"
721-
);
722-
723-
// Check usize::MAX does count as overflow
724-
assert_matches!(
725-
empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
726-
Err(CapacityOverflow),
727-
"usize::MAX should trigger an overflow!"
728-
);
729-
} else {
730-
// Check isize::MAX + 1 is an OOM
731-
assert_matches!(
732-
empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
733-
Err(AllocError { .. }),
734-
"isize::MAX + 1 should trigger an OOM!"
735-
);
736-
737-
// Check usize::MAX is an OOM
738-
assert_matches!(
739-
empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
740-
Err(AllocError { .. }),
741-
"usize::MAX should trigger an OOM!"
742-
);
743-
}
709+
// Check isize::MAX + 1 does count as overflow
710+
assert_matches!(
711+
empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
712+
Err(CapacityOverflow),
713+
"isize::MAX + 1 should trigger an overflow!"
714+
);
715+
716+
// Check usize::MAX does count as overflow
717+
assert_matches!(
718+
empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
719+
Err(CapacityOverflow),
720+
"usize::MAX should trigger an overflow!"
721+
);
744722
}
745723

746724
{
@@ -753,19 +731,13 @@ fn test_try_reserve() {
753731
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10).map_err(|e| e.kind()) {
754732
panic!("isize::MAX shouldn't trigger an overflow!");
755733
}
756-
if guards_against_isize {
757-
assert_matches!(
758-
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
759-
Err(CapacityOverflow),
760-
"isize::MAX + 1 should trigger an overflow!"
761-
);
762-
} else {
763-
assert_matches!(
764-
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
765-
Err(AllocError { .. }),
766-
"isize::MAX + 1 should trigger an OOM!"
767-
);
768-
}
734+
735+
assert_matches!(
736+
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
737+
Err(CapacityOverflow),
738+
"isize::MAX + 1 should trigger an overflow!"
739+
);
740+
769741
// Should always overflow in the add-to-len
770742
assert_matches!(
771743
ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
@@ -785,8 +757,6 @@ fn test_try_reserve_exact() {
785757
const MAX_CAP: usize = isize::MAX as usize;
786758
const MAX_USIZE: usize = usize::MAX;
787759

788-
let guards_against_isize = usize::BITS < 64;
789-
790760
{
791761
let mut empty_string: String = String::new();
792762

@@ -799,31 +769,17 @@ fn test_try_reserve_exact() {
799769
panic!("isize::MAX shouldn't trigger an overflow!");
800770
}
801771

802-
if guards_against_isize {
803-
assert_matches!(
804-
empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
805-
Err(CapacityOverflow),
806-
"isize::MAX + 1 should trigger an overflow!"
807-
);
808-
809-
assert_matches!(
810-
empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
811-
Err(CapacityOverflow),
812-
"usize::MAX should trigger an overflow!"
813-
);
814-
} else {
815-
assert_matches!(
816-
empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
817-
Err(AllocError { .. }),
818-
"isize::MAX + 1 should trigger an OOM!"
819-
);
820-
821-
assert_matches!(
822-
empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
823-
Err(AllocError { .. }),
824-
"usize::MAX should trigger an OOM!"
825-
);
826-
}
772+
assert_matches!(
773+
empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
774+
Err(CapacityOverflow),
775+
"isize::MAX + 1 should trigger an overflow!"
776+
);
777+
778+
assert_matches!(
779+
empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
780+
Err(CapacityOverflow),
781+
"usize::MAX should trigger an overflow!"
782+
);
827783
}
828784

829785
{
@@ -839,19 +795,13 @@ fn test_try_reserve_exact() {
839795
{
840796
panic!("isize::MAX shouldn't trigger an overflow!");
841797
}
842-
if guards_against_isize {
843-
assert_matches!(
844-
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
845-
Err(CapacityOverflow),
846-
"isize::MAX + 1 should trigger an overflow!"
847-
);
848-
} else {
849-
assert_matches!(
850-
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
851-
Err(AllocError { .. }),
852-
"isize::MAX + 1 should trigger an OOM!"
853-
);
854-
}
798+
799+
assert_matches!(
800+
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
801+
Err(CapacityOverflow),
802+
"isize::MAX + 1 should trigger an overflow!"
803+
);
804+
855805
assert_matches!(
856806
ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
857807
Err(CapacityOverflow),

alloc/tests/vec.rs

+52-114
Original file line numberDiff line numberDiff line change
@@ -1489,12 +1489,6 @@ fn test_try_reserve() {
14891489
const MAX_CAP: usize = isize::MAX as usize;
14901490
const MAX_USIZE: usize = usize::MAX;
14911491

1492-
// On 16/32-bit, we check that allocations don't exceed isize::MAX,
1493-
// on 64-bit, we assume the OS will give an OOM for such a ridiculous size.
1494-
// Any platform that succeeds for these requests is technically broken with
1495-
// ptr::offset because LLVM is the worst.
1496-
let guards_against_isize = usize::BITS < 64;
1497-
14981492
{
14991493
// Note: basic stuff is checked by test_reserve
15001494
let mut empty_bytes: Vec<u8> = Vec::new();
@@ -1508,35 +1502,19 @@ fn test_try_reserve() {
15081502
panic!("isize::MAX shouldn't trigger an overflow!");
15091503
}
15101504

1511-
if guards_against_isize {
1512-
// Check isize::MAX + 1 does count as overflow
1513-
assert_matches!(
1514-
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
1515-
Err(CapacityOverflow),
1516-
"isize::MAX + 1 should trigger an overflow!"
1517-
);
1518-
1519-
// Check usize::MAX does count as overflow
1520-
assert_matches!(
1521-
empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
1522-
Err(CapacityOverflow),
1523-
"usize::MAX should trigger an overflow!"
1524-
);
1525-
} else {
1526-
// Check isize::MAX + 1 is an OOM
1527-
assert_matches!(
1528-
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
1529-
Err(AllocError { .. }),
1530-
"isize::MAX + 1 should trigger an OOM!"
1531-
);
1532-
1533-
// Check usize::MAX is an OOM
1534-
assert_matches!(
1535-
empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
1536-
Err(AllocError { .. }),
1537-
"usize::MAX should trigger an OOM!"
1538-
);
1539-
}
1505+
// Check isize::MAX + 1 does count as overflow
1506+
assert_matches!(
1507+
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
1508+
Err(CapacityOverflow),
1509+
"isize::MAX + 1 should trigger an overflow!"
1510+
);
1511+
1512+
// Check usize::MAX does count as overflow
1513+
assert_matches!(
1514+
empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
1515+
Err(CapacityOverflow),
1516+
"usize::MAX should trigger an overflow!"
1517+
);
15401518
}
15411519

15421520
{
@@ -1549,19 +1527,13 @@ fn test_try_reserve() {
15491527
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10).map_err(|e| e.kind()) {
15501528
panic!("isize::MAX shouldn't trigger an overflow!");
15511529
}
1552-
if guards_against_isize {
1553-
assert_matches!(
1554-
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
1555-
Err(CapacityOverflow),
1556-
"isize::MAX + 1 should trigger an overflow!"
1557-
);
1558-
} else {
1559-
assert_matches!(
1560-
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
1561-
Err(AllocError { .. }),
1562-
"isize::MAX + 1 should trigger an OOM!"
1563-
);
1564-
}
1530+
1531+
assert_matches!(
1532+
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
1533+
Err(CapacityOverflow),
1534+
"isize::MAX + 1 should trigger an overflow!"
1535+
);
1536+
15651537
// Should always overflow in the add-to-len
15661538
assert_matches!(
15671539
ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
@@ -1582,19 +1554,13 @@ fn test_try_reserve() {
15821554
{
15831555
panic!("isize::MAX shouldn't trigger an overflow!");
15841556
}
1585-
if guards_against_isize {
1586-
assert_matches!(
1587-
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1588-
Err(CapacityOverflow),
1589-
"isize::MAX + 1 should trigger an overflow!"
1590-
);
1591-
} else {
1592-
assert_matches!(
1593-
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1594-
Err(AllocError { .. }),
1595-
"isize::MAX + 1 should trigger an OOM!"
1596-
);
1597-
}
1557+
1558+
assert_matches!(
1559+
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1560+
Err(CapacityOverflow),
1561+
"isize::MAX + 1 should trigger an overflow!"
1562+
);
1563+
15981564
// Should fail in the mul-by-size
15991565
assert_matches!(
16001566
ten_u32s.try_reserve(MAX_USIZE - 20).map_err(|e| e.kind()),
@@ -1614,8 +1580,6 @@ fn test_try_reserve_exact() {
16141580
const MAX_CAP: usize = isize::MAX as usize;
16151581
const MAX_USIZE: usize = usize::MAX;
16161582

1617-
let guards_against_isize = size_of::<usize>() < 8;
1618-
16191583
{
16201584
let mut empty_bytes: Vec<u8> = Vec::new();
16211585

@@ -1628,31 +1592,17 @@ fn test_try_reserve_exact() {
16281592
panic!("isize::MAX shouldn't trigger an overflow!");
16291593
}
16301594

1631-
if guards_against_isize {
1632-
assert_matches!(
1633-
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
1634-
Err(CapacityOverflow),
1635-
"isize::MAX + 1 should trigger an overflow!"
1636-
);
1637-
1638-
assert_matches!(
1639-
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
1640-
Err(CapacityOverflow),
1641-
"usize::MAX should trigger an overflow!"
1642-
);
1643-
} else {
1644-
assert_matches!(
1645-
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
1646-
Err(AllocError { .. }),
1647-
"isize::MAX + 1 should trigger an OOM!"
1648-
);
1649-
1650-
assert_matches!(
1651-
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
1652-
Err(AllocError { .. }),
1653-
"usize::MAX should trigger an OOM!"
1654-
);
1655-
}
1595+
assert_matches!(
1596+
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
1597+
Err(CapacityOverflow),
1598+
"isize::MAX + 1 should trigger an overflow!"
1599+
);
1600+
1601+
assert_matches!(
1602+
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
1603+
Err(CapacityOverflow),
1604+
"usize::MAX should trigger an overflow!"
1605+
);
16561606
}
16571607

16581608
{
@@ -1668,19 +1618,13 @@ fn test_try_reserve_exact() {
16681618
{
16691619
panic!("isize::MAX shouldn't trigger an overflow!");
16701620
}
1671-
if guards_against_isize {
1672-
assert_matches!(
1673-
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
1674-
Err(CapacityOverflow),
1675-
"isize::MAX + 1 should trigger an overflow!"
1676-
);
1677-
} else {
1678-
assert_matches!(
1679-
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
1680-
Err(AllocError { .. }),
1681-
"isize::MAX + 1 should trigger an OOM!"
1682-
);
1683-
}
1621+
1622+
assert_matches!(
1623+
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
1624+
Err(CapacityOverflow),
1625+
"isize::MAX + 1 should trigger an overflow!"
1626+
);
1627+
16841628
assert_matches!(
16851629
ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
16861630
Err(CapacityOverflow),
@@ -1701,19 +1645,13 @@ fn test_try_reserve_exact() {
17011645
{
17021646
panic!("isize::MAX shouldn't trigger an overflow!");
17031647
}
1704-
if guards_against_isize {
1705-
assert_matches!(
1706-
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1707-
Err(CapacityOverflow),
1708-
"isize::MAX + 1 should trigger an overflow!"
1709-
);
1710-
} else {
1711-
assert_matches!(
1712-
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1713-
Err(AllocError { .. }),
1714-
"isize::MAX + 1 should trigger an OOM!"
1715-
);
1716-
}
1648+
1649+
assert_matches!(
1650+
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1651+
Err(CapacityOverflow),
1652+
"isize::MAX + 1 should trigger an overflow!"
1653+
);
1654+
17171655
assert_matches!(
17181656
ten_u32s.try_reserve_exact(MAX_USIZE - 20).map_err(|e| e.kind()),
17191657
Err(CapacityOverflow),

0 commit comments

Comments
 (0)