int fn1 (void) { if (__builtin_constant_p ()) /* { dg-error "7:not enough" } */ return 0; if (__builtin_constant_p (1, 2)) /* { dg-error "7:too many" } */ return 1; if (__builtin_isfinite ()) /* { dg-error "7:not enough" } */ return 3; if (__builtin_isfinite (1, 2)) /* { dg-error "7:too many" } */ return 4; if (__builtin_isless (0)) /* { dg-error "7:not enough" } */ return 5; if (__builtin_isless (1, 2, 3)) /* { dg-error "7:too many" } */ return 6; if (__builtin_fpclassify (1, 2, 3, 4, 5)) /* { dg-error "7:not enough" } */ return 7; if (__builtin_fpclassify (1, 2, 3, 4, 5, r, 6)) /* { dg-error "7:too many" } */ return 8; if (__builtin_assume_aligned (p)) /* { dg-error "7:too few" } */ return 9; if (__builtin_assume_aligned (p, r, p, p)) /* { dg-error "7:too many" } */ return 10; if (__builtin_add_overflow ()) /* { dg-error "7:not enough" } */ return 11; if (__builtin_add_overflow (1, 2, 3, &r)) /* { dg-error "7:too many" } */ return 12; return -1; }
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 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; }
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; }
/* ADD_OVERFLOW should be folded into unsigned addition, because it never overflows. */ __attribute__((noinline, noclone)) int fn3 (char x, unsigned short y, int *ovf) { int res; *ovf = __builtin_add_overflow (x, y, &res); return res; }
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 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; }
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; }
bool Overflow<Unsigned>::operator+=(Unsigned other) { #ifdef __GNUC__ return __builtin_add_overflow(_value, other, &_value); #else _value += other; return _value < other; #endif }
static void test_basics() { // typedefs are allowed typedef int td_int; typedef td_int * td_int_ptr; td_int x; td_int_ptr px = &x; __builtin_add_overflow(1, 1, px); }
/* ADD_OVERFLOW should be folded into unsigned additrion, because it always overflows. */ __attribute__((noinline, noclone)) unsigned char fn7 (unsigned char x, unsigned char y, int *ovf) { unsigned char res; x = (x & 15) + ((unsigned char) ~0 - 15); y = (y & 3) + 16; *ovf = __builtin_add_overflow (x, y, &res); return res; }
static inline bool add_check_overflow(size_t v1, size_t v2, size_t *r) { #if ((defined(__GNUC__) && (__GNUC__ >= 5)) && !defined(__INTEL_COMPILER)) || __has_builtin(__builtin_add_overflow) return __builtin_add_overflow(v1, v2, r); #else // unsigned additions are well-defined *r = v1 + v2; return v1 > v1 + v2; #endif }
int8_t add_int8(pint8_t src, pint8_t dest) { int8_t src_val = *src; int8_t dest_val = *dest; int8_t res_val; bool overflow = !__builtin_add_overflow(src_val, dest_val, &res_val); set_rflags_arth(false, false, false, res_val, overflow); return res_val; }
static bool zadd_overflow(size_t a, size_t b, size_t *result) { #ifdef __GNUC__ return __builtin_add_overflow(a, b, result); #else if (b > SIZE_MAX - a) return true; *result = a + b; return false; #endif }
off_t FileDescription::lseek(off_t offset, int whence) { if (whence == SEEK_CUR) { if (__builtin_add_overflow(offset, this->offset, &offset)) { errno = EOVERFLOW; return -1; } } off_t result = vnode->lseek(offset, whence); if (result < 0) return -1; this->offset = result; return result; }
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; }
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); } }
size_t blockdevice_preadall(const struct blockdevice* bdev, void* buffer, size_t count, off_t off) { if ( off < 0 ) return errno = EINVAL, 0; while ( bdev->p ) { if ( bdev->p->length < off ) return 0; off_t available = bdev->p->length - off; if ( (uintmax_t) available < (uintmax_t) count ) count = (size_t) available; if ( __builtin_add_overflow(bdev->p->start, off, &off) ) return 0; bdev = bdev->p->parent_bdev; } assert(bdev->hd); return preadall(bdev->hd->fd, buffer, count, off); }
// 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; } } }
constexpr Result<RET> add(LHS &&lhs, RHS &&rhs) { RET sum{}; return {__builtin_add_overflow(lhs, rhs, &sum), sum}; }
bool my_uadd_overflow (unsigned int a, unsigned int b, unsigned int *res) { return __builtin_add_overflow (a, b, res); }
bool my_add_overflow (int a, int b, int *res) { return __builtin_add_overflow (a, b, res); }
void zisolate() { bool r; bool c = __builtin_add_overflow((bool)1, (int8_t)-2, &r); //printf("Result: %d, carry: %d\n", r, c); }
U upseu (U x, S y, int *ovf) { U res; *ovf = __builtin_add_overflow (x, y, &res); return res; }