Transcoding of enum fields is not portable
The current implementation of transcoding for enum fields is not portable. It can raise undefined behavior depending on some implementation-defined behavior of the C compiler.
In file sopc_types.h, we have the declarations
typedef enum _OpcUa_NodeClass
{
OpcUa_NodeClass_Unspecified = 0,
...
OpcUa_NodeClass_View = 128
} OpcUa_NodeClass;
and
typedef struct _OpcUa_Node
{
...
OpcUa_NodeClass NodeClass;
...
} OpcUa_Node;
The C99 standard (§6.7.2.2) does not provide any guarantee on the actual number of bits used in the representation of the field NodeClass
. It must just be large enough to hold all the values of the OpcUa_NodeClass
enum. It can be signed or unsigned.
But in the functions associated with the OpcUa_Node
in sopc_types.c, we can see several coding patterns that assume that the enum is implemented as a signed 32 bit integer, for instance:
void OpcUa_Node_Initialize(void* pValue)
{
OpcUa_Node* a_pValue = (OpcUa_Node*) pValue;
...
SOPC_Initialize_EnumeratedType((int32_t*) &a_pValue->NodeClass);
...
}
This is undefined behavior if the enum happens to be encoded in something else than an int32_t
.