From 0cac2919fdb631f09d5bfb58578fe6477a3f0401 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 2 Aug 2022 18:25:22 +0000 Subject: [PATCH] Don't even try to allocate 2GiB on LP32. std::vector will just throw std::length_error anyway... Bug: http://b/241114825 Test: treehugger Change-Id: I44a9be9a5357c7b3a1c1d1273ef90a023a91e81b --- tests/stdio_test.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp index 5e4e06811..46b0cbee5 100644 --- a/tests/stdio_test.cpp +++ b/tests/stdio_test.cpp @@ -2940,28 +2940,38 @@ TEST(STDIO_TEST, freopen_null_filename_mode) { fclose(fp); } +#if defined(__LP64__) static int64_t GetTotalRamGiB() { struct sysinfo si; sysinfo(&si); return (static_cast(si.totalram) * si.mem_unit) / 1024 / 1024 / 1024; } +#endif TEST(STDIO_TEST, fread_int_overflow) { +#if defined(__LP64__) if (GetTotalRamGiB() <= 4) GTEST_SKIP() << "not enough memory"; const size_t too_big_for_an_int = 0x80000000ULL; std::vector buf(too_big_for_an_int); std::unique_ptr fp{fopen("/dev/zero", "re"), fclose}; ASSERT_EQ(too_big_for_an_int, fread(&buf[0], 1, too_big_for_an_int, fp.get())); +#else + GTEST_SKIP() << "32-bit can't allocate 2GiB"; +#endif } TEST(STDIO_TEST, fwrite_int_overflow) { +#if defined(__LP64__) if (GetTotalRamGiB() <= 4) GTEST_SKIP() << "not enough memory"; const size_t too_big_for_an_int = 0x80000000ULL; std::vector buf(too_big_for_an_int); std::unique_ptr fp{fopen("/dev/null", "we"), fclose}; ASSERT_EQ(too_big_for_an_int, fwrite(&buf[0], 1, too_big_for_an_int, fp.get())); +#else + GTEST_SKIP() << "32-bit can't allocate 2GiB"; +#endif } TEST(STDIO_TEST, snprintf_b) {