add fortified implementations of fread/fwrite
A __size_mul_overflow utility is used to take advantage of the checked
overflow intrinsics in Clang and GCC (>= 5). The fallback for older
compilers is the optimized but less than ideal overflow checking pattern
used in OpenBSD.
Change-Id: Ibb0d4fd9b5acb67983e6a9f46844c2fd444f7e69
diff --git a/tests/fortify_test.cpp b/tests/fortify_test.cpp
index 4faccb4..664e057 100644
--- a/tests/fortify_test.cpp
+++ b/tests/fortify_test.cpp
@@ -647,6 +647,22 @@
close(fd);
}
+TEST_F(DEATHTEST, fread_fortified) {
+ char buf[1];
+ size_t ct = atoi("2"); // prevent optimizations
+ FILE* fp = fopen("/dev/null", "r");
+ ASSERT_FORTIFY(fread(buf, 1, ct, fp));
+ fclose(fp);
+}
+
+TEST_F(DEATHTEST, fwrite_fortified) {
+ char buf[1] = {0};
+ size_t ct = atoi("2"); // prevent optimizations
+ FILE* fp = fopen("/dev/null", "w");
+ ASSERT_FORTIFY(fwrite(buf, 1, ct, fp));
+ fclose(fp);
+}
+
TEST_F(DEATHTEST, readlink_fortified) {
char buf[1];
size_t ct = atoi("2"); // prevent optimizations