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

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

Open
Orbuild opened this issue Apr 13, 2024 · 1 comment

Comments

@Orbuild
Copy link

Orbuild commented Apr 13, 2024

  • When I use the code from the master branch, I ran a simple publish-subscribe program, where the message format is as follows:
module MyTestData
{ 
  struct Msg
  {
    long id;
    @optional char flag;
    string message;
  };
};

  • Before the message is sent, the flag field is tampered with as a character type. At this point, running the program results in an error as follows:
MyTestData_Msg msg;
...
msg.flag = 'A';
=== [MyTestPublisher]  Waiting for a reader to be discovered ...
[MyTestPublisher]  Writing : Message (id: 1)
Segmentation fault (core dumped)
  • When the flag field is decorated with @external, the same error occurs. According to the official documentation, when the flag field is decorated with @optional or @external, and when the flag is set to a string type, the program runs normally. However, when the flag field is set to a character type, the program exits abnormally without checking the type of the flag field or providing any error message. Is this a bug, and should type checking be performed on variables decorated with @optional or @external? It might be more user-friendly to provide relevant error messages when type checking fails. Looking forward to your response!
@eboasson
Copy link
Contributor

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.

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

No branches or pull requests

2 participants