http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51311

--- Comment #2 from Michael Bruck <bruck.michael at googlemail dot com> 
2011-11-28 02:34:16 UTC ---
(In reply to comment #1)
> I don't think this is a bug as in_data can be changed by a different function
> other than bar().

Before I stripped this down to a test case in_data was actually a const coming
from a second function (into which bar was being inlined by gcc).

Apart from that even if modified elsewhere gcc should IMO not assume that that
modification produces an ill-formed in_data that triggers case 0x02.

This revised version with static const behaves the same way:

#include <string.h>

static const char in_data[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00};

void bar(char * dummy)
{
    size_t in_length = sizeof(in_data) / sizeof(in_data[0]);

    size_t pos = 0;

    while (pos < in_length)
    {
    size_t next = pos;

    auto copy = [&](size_t data_len) -> bool
    {
        next = pos + 1 + data_len;

        if (next > in_length)
        return false;

        memcpy(dummy, in_data + pos + 1, data_len);

        return true;
    };

    switch (in_data[pos])
    {
    case 0x01:
        if (!copy(4))
        return;
        break;

    case 0x02:
        if (!copy(16))
        return;
        break;
    }

    pos = next;
    }
}

Reply via email to