void fn0 (int n) { p = __builtin_alloca_with_align (n, 6); /* { dg-error "39:must be a constant integer" } */ r += __builtin_isfinite (0); /* { dg-error "28:non-floating-point argument in call" } */ r += __builtin_isinf (0); /* { dg-error "25:non-floating-point argument in call" } */ r += __builtin_isinf_sign (0); /* { dg-error "30:non-floating-point argument in call" } */ r += __builtin_isnan (0); /* { dg-error "25:non-floating-point argument in call" } */ r += __builtin_isnormal (0); /* { dg-error "28:non-floating-point argument in call" } */ r += __builtin_signbit (0); /* { dg-error "27:non-floating-point argument in call" } */ r += __builtin_isgreater (0, 0); /* { dg-error "8:non-floating-point arguments in call to function" } */ r += __builtin_isgreaterequal (0, 0); /* { dg-error "8:non-floating-point arguments in call to function" } */ r += __builtin_isless (0, 0); /* { dg-error "8:non-floating-point arguments in call to function" } */ r += __builtin_islessequal (0, 0); /* { dg-error "8:non-floating-point arguments in call to function" } */ r += __builtin_islessgreater (0, 0); /* { dg-error "8:non-floating-point arguments in call to function" } */ r += __builtin_isunordered (0, 0); /* { dg-error "8:non-floating-point arguments in call to function" } */ r += __builtin_fpclassify (1, 2, n, 4, 5, n); /* { dg-error "36:non-const integer argument 3 in call" } */ r += __builtin_fpclassify (1, 2, 3, 4, 5, 6); /* { dg-error "45:non-floating-point argument in call" } */ d = __builtin_assume_aligned (p, n, p); /* { dg-error "39:non-integer argument 3 in call" } */ b = __builtin_add_overflow (n, *d, &r); /* { dg-error "34:argument 2 in call to function" } */ b = __builtin_add_overflow (n, 5, d); /* { dg-error "37:argument 3 in call" } */ b = __builtin_sub_overflow (n, *d, &r); /* { dg-error "34:argument 2 in call to function" } */ b = __builtin_sub_overflow (n, 5, d); /* { dg-error "37:argument 3 in call" } */ b = __builtin_mul_overflow (n, *d, &r); /* { dg-error "34:argument 2 in call to function" } */ b = __builtin_mul_overflow (n, 5, d); /* { dg-error "37:argument 3 in call" } */ }
int f4 (float *fp, double *dp, _Complex int *cp, enum E *ep, bool *bp, long long int *llp) { int x = __builtin_add_overflow (1, 2, fp); /* { dg-error "argument 3 in call to function\[^\n\r]*does not have pointer to integer type" } */ x += __builtin_sub_overflow (1, 2, dp); /* { dg-error "argument 3 in call to function\[^\n\r]*does not have pointer to integer type" } */ x += __builtin_mul_overflow (1, 2, cp); /* { dg-error "argument 3 in call to function\[^\n\r]*does not have pointer to integer type" } */ x += __builtin_add_overflow (1, 2, ep); /* { dg-error "argument 3 in call to function\[^\n\r]*does not have pointer to integer type" } */ x += __builtin_sub_overflow (1, 2, bp); /* { dg-error "argument 3 in call to function\[^\n\r]*does not have pointer to integer type" } */ x += __builtin_mul_overflow (1, 2, llp); return x; }
Fraction Fraction::operator *= (const Fraction& rhs) { if (__builtin_mul_overflow(numerator, rhs.numerator, &numerator)) goto err; if (__builtin_mul_overflow(DenominatorAsInt(), rhs.DenominatorAsInt(), &denominator)) goto err; Reduce(); return *this; err: valid = false; return *this; }
int f3 (float fa, int a, _Complex long int ca, double fb, void *pb, int b, enum E eb, bool bb, int *c) { int x = __builtin_add_overflow (fa, b, c); /* { dg-error "argument 1 in call to function\[^\n\r]*does not have integral type" } */ x += __builtin_sub_overflow (ca, b, c); /* { dg-error "argument 1 in call to function\[^\n\r]*does not have integral type" } */ x += __builtin_mul_overflow (a, fb, c); /* { dg-error "argument 2 in call to function\[^\n\r]*does not have integral type" } */ x += __builtin_add_overflow (a, pb, c); /* { dg-error "argument 2 in call to function\[^\n\r]*does not have integral type" } */ x += __builtin_sub_overflow (a, eb, c); x += __builtin_mul_overflow (a, bb, c); return x; }
u64 parseu64(string data, string *remain) { if (remain == NULL) { if (*data == 0) { panic_static("parsenum got no number"); } if (*data == '-') { panic_static("parsenum got a negative number"); } string str; u64 out = parseu64(data, &str); if (str == NULL) { panic_static("parsenum found a too-large number"); } else if (*str != '\0') { if (str == data) { panic_static("parsenum did not find a number"); } else if (*str >= '0' && *str <= '9') { panic_static("parsenum found a too-long number"); } else { panic_static("parsenum found garbage after a number"); } } return out; } u64 out = 0; while (*data >= '0' && *data <= '9') { if (__builtin_mul_overflow(out, 10, &out) || __builtin_add_overflow(out, *data - '0', &out)) { *remain = NULL; return out; } data++; } *remain = data; return out; }
int f1 (void) { int x = __builtin_add_overflow (); /* { dg-error "not enough arguments to function" } */ x += __builtin_sub_overflow (); /* { dg-error "not enough arguments to function" } */ x += __builtin_mul_overflow (); /* { dg-error "not enough arguments to function" } */ return x; }
int f2 (int a, int b, int *c, int d) { int x = __builtin_add_overflow (a, b, c, d); /* { dg-error "too many arguments to function" } */ x += __builtin_sub_overflow (a, b, c, d, d, d); /* { dg-error "too many arguments to function" } */ x += __builtin_mul_overflow (a, b, c, d); /* { dg-error "too many arguments to function" } */ return x; }
void foo (unsigned int x) { signed int y = ((-__INT_MAX__ - 1) / 2); signed int r; if (__builtin_mul_overflow (x, y, &r)) bar (); }
/* MUL_OVERFLOW should be folded into unsigned multiplication, because it always overflows. */ __attribute__((noinline, noclone)) signed char fn5 (long int x, long int y, int *ovf) { signed char res; x = (x & 63) + (__SCHAR_MAX__ / 4); y = (y & 3) + 5; *ovf = __builtin_mul_overflow (x, y, &res); return res; }
Fraction Fraction::operator += (const Fraction& rhs) { const int lcm = LCM(DenominatorAsInt(), rhs.DenominatorAsInt()); int A = 0, B = 0; if (__builtin_mul_overflow(numerator / DenominatorAsInt(), lcm, &A)) goto err; if (__builtin_mul_overflow(rhs.numerator / rhs.DenominatorAsInt(), lcm, &B)) goto err; if (__builtin_add_overflow(A, B, &numerator)) goto err; denominator = lcm; Reduce(); return *this; err: valid = false; return *this; }
/* MUL_OVERFLOW should be folded into unsigned multiplication, because it never overflows. */ __attribute__((noinline, noclone)) long int fn4 (long int x, long int y, int *ovf) { long int res; x &= 65535; y = (y & 65535) - 32768; *ovf = __builtin_mul_overflow (x, y, &res); return res; }
/* MUL_OVERFLOW should be folded into unsigned multiplication, because ovf is never used. */ __attribute__((noinline, noclone)) int fn2 (char x, long int y) { short int res; int ovf = __builtin_mul_overflow (x, y, &res); int res2 = res; int res3 = res2 - 2; (void) ovf; return res; }
bool Overflow<Unsigned>::operator*=(Unsigned other) { #ifdef __GNUC__ return __builtin_mul_overflow(_value, other, &_value); #else Unsigned cache = _value; _value *= other; return other && _value / other != cache; #endif }
void * __libc_reallocarray (void *optr, size_t nmemb, size_t elem_size) { size_t bytes; if (__builtin_mul_overflow (nmemb, elem_size, &bytes)) { __set_errno (ENOMEM); return 0; } return realloc (optr, bytes); }
Fraction Fraction::operator -= (const Fraction& rhs) { const int lcm = LCM(DenominatorAsInt(), rhs.DenominatorAsInt()); int A = 0, B = 0; if (__builtin_mul_overflow(numerator / DenominatorAsInt(), lcm, &A)) goto err; if (__builtin_mul_overflow(rhs.numerator / rhs.DenominatorAsInt(), lcm, &B)) goto err; if (__builtin_sub_overflow(A, B, &numerator)) goto err; denominator = lcm; Reduce(); // std::cout << "result: " << *this << std::endl; return *this; err: valid = false; return *this; }
void* debug_calloc(size_t nmemb, size_t bytes) { if (DebugCallsDisabled()) { return g_dispatch->calloc(nmemb, bytes); } ScopedDisableDebugCalls disable; size_t size; if (__builtin_mul_overflow(nmemb, bytes, &size)) { // Overflow errno = ENOMEM; return nullptr; } if (size == 0) { size = 1; } size_t real_size; if (__builtin_add_overflow(size, g_debug->extra_bytes(), &real_size)) { // Overflow. errno = ENOMEM; return nullptr; } if (g_debug->need_header()) { // The above check will guarantee the multiply will not overflow. if (size > Header::max_size()) { errno = ENOMEM; return nullptr; } // Need to guarantee the alignment of the header. Header* header = reinterpret_cast<Header*>( g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size)); if (header == nullptr) { return nullptr; } memset(header, 0, g_dispatch->malloc_usable_size(header)); return InitHeader(header, header, size); } else { return g_dispatch->calloc(1, real_size); } }
int main(int argc, const char **argv) { signed int res; if (__builtin_mul_overflow((signed int)0x0, (signed int)0x0, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0x0, (signed int)0x7FFFFFFF, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0x0, (signed int)0x80000000, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0x1, (signed int)0x7FFFFFFF, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0x1, (signed int)0x80000000, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0x2, (signed int)0x3FFFFFFF, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0x2, (signed int)0xC0000000, &res)) { return -1; } if (!__builtin_mul_overflow((signed int)0x2, (signed int)0x7FFFFFFF, &res)) { return -1; } if (!__builtin_mul_overflow((signed int)0x2, (signed int)0x80000000, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0x0FFFFFFF, (signed int)0x8, &res)) { return -1; } if (!__builtin_mul_overflow((signed int)0x10000000, (signed int)0x8, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0x7FFFFFFF, (signed int)0x0, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0x7FFFFFFF, (signed int)0x1, &res)) { return -1; } if (!__builtin_mul_overflow((signed int)0x7FFFFFFF, (signed int)0x2, &res)) { return -1; } if (!__builtin_mul_overflow((signed int)0x7FFFFFFF, (signed int)0x7FFFFFFF, &res)) { return -1; } if (!__builtin_mul_overflow((signed int)0x7FFFFFFF, (signed int)0x80000000, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0x80000000, (signed int)0x0, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0x80000000, (signed int)0x1, &res)) { return -1; } if (!__builtin_mul_overflow((signed int)0x80000000, (signed int)0x2, &res)) { return -1; } if (!__builtin_mul_overflow((signed int)0x80000000, (signed int)0x7FFFFFFF, &res)) { return -1; } if (!__builtin_mul_overflow((signed int)0x80000000, (signed int)0x80000000, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0xFFFFFFFF, (signed int)0x0, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0xFFFFFFFF, (signed int)0x1, &res)) { return -1; } if (__builtin_mul_overflow((signed int)0xFFFFFFFF, (signed int)0xFFFFFFFF, &res)) { return -1; } return 0; }
constexpr Result<RET> mul(LHS &&lhs, RHS &&rhs) { RET sum{}; return {__builtin_mul_overflow(lhs, rhs, &sum), sum}; }
int umult16(uint16 a,uint16 b,uint16* c) { return !__builtin_mul_overflow(a,b,c); }
// Determine video dimensions from the sequence parameterset. void FindAVCDimensions( const sp<ABuffer> &seqParamSet, int32_t *width, int32_t *height, int32_t *sarWidth, int32_t *sarHeight) { ABitReader br(seqParamSet->data() + 1, seqParamSet->size() - 1); unsigned profile_idc = br.getBits(8); br.skipBits(16); parseUE(&br); // seq_parameter_set_id unsigned chroma_format_idc = 1; // 4:2:0 chroma format if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || profile_idc == 244 || profile_idc == 44 || profile_idc == 83 || profile_idc == 86) { chroma_format_idc = parseUE(&br); if (chroma_format_idc == 3) { br.skipBits(1); // residual_colour_transform_flag } parseUE(&br); // bit_depth_luma_minus8 parseUE(&br); // bit_depth_chroma_minus8 br.skipBits(1); // qpprime_y_zero_transform_bypass_flag if (br.getBits(1)) { // seq_scaling_matrix_present_flag for (size_t i = 0; i < 8; ++i) { if (br.getBits(1)) { // seq_scaling_list_present_flag[i] // WARNING: the code below has not ever been exercised... // need a real-world example. if (i < 6) { // ScalingList4x4[i],16,... skipScalingList(&br, 16); } else { // ScalingList8x8[i-6],64,... skipScalingList(&br, 64); } } } } } parseUE(&br); // log2_max_frame_num_minus4 unsigned pic_order_cnt_type = parseUE(&br); if (pic_order_cnt_type == 0) { parseUE(&br); // log2_max_pic_order_cnt_lsb_minus4 } else if (pic_order_cnt_type == 1) { // offset_for_non_ref_pic, offset_for_top_to_bottom_field and // offset_for_ref_frame are technically se(v), but since we are // just skipping over them the midpoint does not matter. br.getBits(1); // delta_pic_order_always_zero_flag parseUE(&br); // offset_for_non_ref_pic parseUE(&br); // offset_for_top_to_bottom_field unsigned num_ref_frames_in_pic_order_cnt_cycle = parseUE(&br); for (unsigned i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; ++i) { parseUE(&br); // offset_for_ref_frame } } parseUE(&br); // num_ref_frames br.getBits(1); // gaps_in_frame_num_value_allowed_flag unsigned pic_width_in_mbs_minus1 = parseUE(&br); unsigned pic_height_in_map_units_minus1 = parseUE(&br); unsigned frame_mbs_only_flag = br.getBits(1); *width = pic_width_in_mbs_minus1 * 16 + 16; *height = (2 - frame_mbs_only_flag) * (pic_height_in_map_units_minus1 * 16 + 16); if (!frame_mbs_only_flag) { br.getBits(1); // mb_adaptive_frame_field_flag } br.getBits(1); // direct_8x8_inference_flag if (br.getBits(1)) { // frame_cropping_flag unsigned frame_crop_left_offset = parseUE(&br); unsigned frame_crop_right_offset = parseUE(&br); unsigned frame_crop_top_offset = parseUE(&br); unsigned frame_crop_bottom_offset = parseUE(&br); unsigned cropUnitX, cropUnitY; if (chroma_format_idc == 0 /* monochrome */) { cropUnitX = 1; cropUnitY = 2 - frame_mbs_only_flag; } else { unsigned subWidthC = (chroma_format_idc == 3) ? 1 : 2; unsigned subHeightC = (chroma_format_idc == 1) ? 2 : 1; cropUnitX = subWidthC; cropUnitY = subHeightC * (2 - frame_mbs_only_flag); } ALOGV("frame_crop = (%u, %u, %u, %u), cropUnitX = %u, cropUnitY = %u", frame_crop_left_offset, frame_crop_right_offset, frame_crop_top_offset, frame_crop_bottom_offset, cropUnitX, cropUnitY); // *width -= (frame_crop_left_offset + frame_crop_right_offset) * cropUnitX; if(__builtin_add_overflow(frame_crop_left_offset, frame_crop_right_offset, &frame_crop_left_offset) || __builtin_mul_overflow(frame_crop_left_offset, cropUnitX, &frame_crop_left_offset) || __builtin_sub_overflow(*width, frame_crop_left_offset, width) || *width < 0) { *width = 0; } //*height -= (frame_crop_top_offset + frame_crop_bottom_offset) * cropUnitY; if(__builtin_add_overflow(frame_crop_top_offset, frame_crop_bottom_offset, &frame_crop_top_offset) || __builtin_mul_overflow(frame_crop_top_offset, cropUnitY, &frame_crop_top_offset) || __builtin_sub_overflow(*height, frame_crop_top_offset, height) || *height < 0) { *height = 0; } } if (sarWidth != NULL) { *sarWidth = 0; } if (sarHeight != NULL) { *sarHeight = 0; } if (br.getBits(1)) { // vui_parameters_present_flag unsigned sar_width = 0, sar_height = 0; if (br.getBits(1)) { // aspect_ratio_info_present_flag unsigned aspect_ratio_idc = br.getBits(8); if (aspect_ratio_idc == 255 /* extendedSAR */) { sar_width = br.getBits(16); sar_height = br.getBits(16); } else { static const struct { unsigned width, height; } kFixedSARs[] = { { 0, 0 }, // Invalid { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 }, { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 }, }; if (aspect_ratio_idc > 0 && aspect_ratio_idc < NELEM(kFixedSARs)) { sar_width = kFixedSARs[aspect_ratio_idc].width; sar_height = kFixedSARs[aspect_ratio_idc].height; } } } ALOGV("sample aspect ratio = %u : %u", sar_width, sar_height); if (sarWidth != NULL) { *sarWidth = sar_width; } if (sarHeight != NULL) { *sarHeight = sar_height; } } }
int imult16(int16 a, int16 b, int16* c) { return !__builtin_mul_overflow(a, b, c); }