Bug creation and email sending has been disabled, file new bugs at gcc.gnu.org/bugzilla
Bug 211 - Error: iteration 2305843009213693951 invokes undefined behavior
Summary: Error: iteration 2305843009213693951 invokes undefined behavior
Status: RESOLVED INVALID
Alias: None
Product: GDC
Classification: Unclassified
Component: gdc (show other bugs)
Version: development
Hardware: All All
: --- normal
Assignee: Iain Buclaw
URL:
Depends on:
Blocks:
 
Reported: 2016-02-07 20:52 CET by Iain Buclaw
Modified: 2016-09-16 21:04 CEST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Iain Buclaw 2016-02-07 20:52:09 CET
---
struct Array
{
    void setlength(size_t nlength)
    {
        foreach (ref val; ptr[nlength .. length])
            val = 0;
    }

    void insertBack()
    {
        setlength(length + 1);
    }

    size_t* ptr;
    size_t length;
}
---

This causes an warning/error to be emitted under optimizations. (-O2 -Werror)

---
array.d: In function ‘insertBack’:
array.d:5:9: error: iteration 2305843009213693951 invokes undefined behavior [-Werror=aggressive-loop-optimizations]
         foreach (ref val; ptr[nlength .. length])
         ^

array.d:5:9: note: within this loop
         foreach (ref val; ptr[nlength .. length])
         ^
---

This is the codegen:
---
while (1)
{
    ulong & val;

    if (!(__aggr40.length > __key41)) break;
    val = __aggr40.ptr + __key41 * 8;
    *val = 0;
    __key41 = __key41 + 1;
}
---

I think this could be vastly improved by initializing the reference directly, rather than via a modify expression.
Comment 1 Iain Buclaw 2016-05-22 09:21:36 CEST
Unfortunately, this is a gcc bug.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71224
Comment 2 Iain Buclaw 2016-09-16 21:04:32 CEST
Even more unfortunately, the bug was me.