[cfe-commits] [PATCH] PR12290: introducing overflow builtins

Xi Wang xi.wang at gmail.com
Wed Apr 4 17:18:00 PDT 2012


Attached is a patch that adds arithmetic with overflow builtins to Clang.  Please review.  Thanks.

Arithmetic with overflow builtins are used to perform arithmetic operations with overflow detection.


bool __builtin_add_with_overflow(type *ptr, type a, type b);
bool __builtin_sub_with_overflow(type *ptr, type a, type b);
bool __builtin_mul_with_overflow(type *ptr, type a, type b);

Example of Use:

void *malloc_array(size_t n, size_t size) {
  size_t bytes;
  if (__builtin_mul_with_overflow(&bytes, n, size))
    return NULL;
  return malloc(bytes);


__builtin_op_with_overflow(ptr, a, b) stores the result of a op b in ptr, and returns true if an overflow occurred during the arithmetic operation. Note that type is inferred from *ptr. These builtins help developers write more efficient and correct code by avoiding ad hoc overflow checks.

Query for this feature with __has_builtin(__builtin_op_with_overflow).

- xi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: overflow.patch
Type: application/octet-stream
Size: 11632 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120404/1aedc74c/attachment.obj>

More information about the cfe-commits mailing list