eclipse-cyclonedds / cyclonedds

Eclipse Cyclone DDS project
https://projects.eclipse.org/projects/iot.cyclonedds
Other
799 stars 349 forks source link

The program exits abnormally when using the @optional or @external annotations #1977

Open Orbuild opened 2 months ago

Orbuild commented 2 months ago
module MyTestData
{ 
  struct Msg
  {
    long id;
    @optional char flag;
    string message;
  };
};
eboasson commented 2 months ago

I'm surprised that even compiles without dire warnings from the C compiler: @optional and @external result in a struct containing a pointer to the data rather than the data itself and you're assigning a character constant to it.

Your example results in the following C type definition:

typedef struct MyTestData_Msg
{
  int32_t id;
  char * flag;
  char * message;
} MyTestData_Msg;

During serialisation, that flag field gets checked for a null pointer (if so, the optional field is not present) and if not gets dereferenced to serialise the contents. If you assign 'A' to flag, it tries to dereference at address 0x41 as it is required to do. A crash is totally reasonable then.

In other words, what you're running is a minor misunderstanding of how @optional is mapped in C. The crash you see is very nearly unavoidable and is not a lack of type checking.