https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71831
Bug ID: 71831 Summary: __builtin_object_size poor results with no optimization Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- The __builtin_object_size intrinsic returns the size of an object pointed to by its first argument. The function relies on optimization to determine the sizes of objects referenced by complex expressions such as arguments of inlined functions. However, when optimization isn't enabled the function returns either zero or -1 even in straightforward cases to indicate that it doesn't know. Enhancing the function to work just a little harder in some of these basic cases would make it much more useful, both to users and also within GCC itself. For example, the function could easily handle all of the expressions in the example below (it only handles the first one). In addition, treating the function as a constant expression in these limited cases (i.e., where the size of the object can unambiguously be determined by folding) would also make it usable in more contexts (both in C and especially in C++ constexpr functions). In C++, this functionality already exists within the front end (a constexpr function must determine whether or not an indirect access to an object via a pointer is within the bounds of the object and reject access that are not), but it is not conveniently exposed to users. $ cat xyz.c && /home/msebor/build/gcc-trunk-svn/gcc/xgcc -B /home/msebor/build/gcc-trunk-svn/gcc -S -Wall -Wextra -Wpedantic -fdump-tree-optimized=/dev/stdout xyz.c | grep -e ^f -e __builtin_object_size char a [7]; int f0 (void) { return __builtin_object_size (a, 0); } int f1 (void) { return __builtin_object_size (a + 3, 0); } int f2 (void) { void *p = a; return __builtin_object_size (p, 0); } f0 () f1 () _2 = __builtin_object_size (_1, 0); f2 () _1 = __builtin_object_size (p_2, 0);