Skip to content

Commit a95f116

Browse files
committed
graphite2: update gralloc in Main.h
- With newer compiler / libraries, there is error G47DF2280: argument 1 value ‘18446744073709551560’ exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=] - Copied in the newer implementation from graphite.git. Change-Id: I904fff90c7cce9331aeb19c4854b47541c498df0
1 parent 11884e4 commit a95f116

File tree

1 file changed

+48
-2
lines changed
  • Lib/src/graphite2/src/inc

1 file changed

+48
-2
lines changed

Lib/src/graphite2/src/inc/Main.h

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,61 @@ class telemetry::category
7777
struct telemetry {};
7878
#endif
7979

80+
// Checked multiplaction to catch overflow or underflow when allocating memory
81+
#if defined(__has_builtin)
82+
#if __has_builtin(__builtin_mul_overflow)
83+
#define HAVE_BUILTIN_OVERFLOW
84+
#endif
85+
#elif defined(__GNUC__) && (__GNUC__ >= 5) && !defined(__INTEL_COMPILER)
86+
#define HAVE_BUILTIN_OVERFLOW
87+
#endif
88+
#if defined(__has_include)
89+
#if __has_include(<intsafe.h>) && !defined(__CYGWIN__)
90+
#define HAVE_INTSAFE_H
91+
#endif
92+
#elif defined(_WIN32)
93+
#define HAVE_INTSAFE_H
94+
#endif
95+
96+
// Need to import intsafe into the top level namespace
97+
#if defined(HAVE_INTSAFE_H)
98+
} // namespace graphite2
99+
100+
#include <intsafe.h>
101+
102+
namespace graphite2 {
103+
#endif
104+
105+
#if defined(HAVE_BUILTIN_OVERFLOW)
106+
inline
107+
bool checked_mul(const size_t a, const size_t b, size_t & t) {
108+
return __builtin_mul_overflow(a, b, &t);
109+
}
110+
#elif defined(HAVE_INTSAFE_H)
111+
inline
112+
bool checked_mul(const size_t a, const size_t b, size_t & t) {
113+
return SizeTMult(a, b, &t) == INTSAFE_E_ARITHMETIC_OVERFLOW;
114+
}
115+
#else
116+
inline
117+
bool checked_mul(const size_t a, const size_t b, size_t & t) {
118+
t = a*b;
119+
return (((a | b) & (~size_t(0) << (sizeof(size_t) << 2))) && (t / a != b));
120+
}
121+
#endif
122+
80123
// typesafe wrapper around malloc for simple types
81124
// use free(pointer) to deallocate
82125

83126
template <typename T> T * gralloc(size_t n)
84127
{
128+
size_t total;
129+
if (checked_mul(n, sizeof(T), total))
130+
return 0;
85131
#ifdef GRAPHITE2_TELEMETRY
86-
telemetry::count_bytes(sizeof(T) * n);
132+
telemetry::count_bytes(total);
87133
#endif
88-
return static_cast<T*>(malloc(sizeof(T) * n));
134+
return static_cast<T*>(malloc(total));
89135
}
90136

91137
template <typename T> T * grzeroalloc(size_t n)

0 commit comments

Comments
 (0)