commit 0c985218e9043289f4e72d1492cdb83f07fe600a
parent 488c3c7183788de425a7854aa0008fe795241546
Author: sin <sin@2f30.org>
Date: Sun, 1 Mar 2015 13:12:09 +0000
Add fread() check
Diffstat:
1 file changed, 15 insertions(+), 0 deletions(-)
diff --git a/include/stdio.h b/include/stdio.h
@@ -16,6 +16,19 @@ __fortify_fgets(char *s, int n, FILE *fp)
return fgets(s, n, fp);
}
+static inline __attribute__ ((always_inline))
+size_t
+__fortify_fread(void *dst, size_t n, size_t nmemb, FILE *fp)
+{
+ size_t bos = __builtin_object_size(dst, 0);
+
+ if (n != 0 && (n * nmemb) / n != nmemb)
+ __builtin_trap();
+ if (n * nmemb > bos)
+ __builtin_trap();
+ return fread(dst, n, nmemb, fp);
+}
+
static inline
__attribute__ ((always_inline))
__attribute__ ((__format__ (printf, 2, 0)))
@@ -52,6 +65,8 @@ __fortify_vsnprintf(char *s, size_t n, const char *fmt, __builtin_va_list ap)
#undef fgets
#define fgets(s, n, fp) __fortify_fgets(s, n, fp)
+#undef fread
+#define fread(dst, n, nmemb, fp) __fortify_fread(dst, n, nmemb, fp)
#undef vsprintf
#define vsprintf(s, fmt, ap) __fortify_vsprintf(s, fmt, ap)
#undef vsnprintf