Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Designated initializers for C structs broken in C translation #693

Open
bahnwaerter opened this issue Nov 14, 2024 · 2 comments
Open

Comments

@bahnwaerter
Copy link
Member

Designated initializers for C structs according to C99 are broken in Ultimate in version 0.3.0-dev-f28124e. The C translation plugin does not seem to correctly handle the lookup of C struct field names from specified initializers, thus violating the assertion in the CTranslationUtil in line 253.

The following reduced program from a large embedded program can be used to reproduce the issue:

typedef unsigned char __uint8_t;
typedef short unsigned int __uint16_t;
typedef long unsigned int __uint32_t;
         
typedef __uint8_t uint8_t;
typedef __uint16_t uint16_t;
typedef __uint32_t uint32_t;

typedef struct {
    uint8_t Channel;
    uint8_t Direction;
    uint16_t Value;
    uint8_t Gain;
} SB17_SENSOR_DATA_t;

typedef struct {
    uint32_t Timestamp;
    SB17_SENSOR_DATA_t Reading;
} SB17_SENSOR_READING_t;

typedef struct {
    /* ... */
    uint32_t default_value;
} cyhal_timer_t;

uint32_t cyhal_timer_read(const cyhal_timer_t *obj);

static SB17_SENSOR_DATA_t _sb17_read_single_sensor_value();

static cyhal_timer_t timestamp_timer;

int main()
{
    /* ... */
    SB17_SENSOR_DATA_t data = _sb17_read_single_sensor_value();
    /* ... */
    SB17_SENSOR_READING_t reading = { .Timestamp = cyhal_timer_read(&timestamp_timer), .Reading = data };
    /* ... */
    return 0;
}
@schuessf
Copy link
Contributor

schuessf commented Dec 2, 2024

Thanks for your report! In principle designated initializer seem to work, the problem only seems to occur in the combination with nested struct. But I will have a look at this problem.

@schuessf
Copy link
Contributor

schuessf commented Dec 3, 2024

I tried to debug it and I minimized this example:

typedef struct { int x; } DATA;
typedef struct { DATA d; } RESULT;

int main() {
  DATA data;
  data.x = 42;
  RESULT result = { .d = data };
  //@ assert result.d.x == 42;
}

It looks like the problem is somewhere in InitializationHandler::constructIndexToInitInfo: There is some queue for children-initializer-results and I have the impression that something goes wrong with peekFirst (that keeps the first element in the queue) and pollFirst (that removes the first element), but I didn't succeed to fix it.

Also: We also crash with RESULT result = { data }; as an initialization, but which a different error message (conversion from CStruct not implemented.), even though the underlying problem seems to be the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants