Skip to content

Narrowing conversion warning for CFE_ResourceId_FromInteger #2664

@pcooksey

Description

@pcooksey

Describe the bug
When compiling with -Werror and OMIT_DEPRECATED=ON with C++ code, the code does not compile because there is a narrowing warning in the cfe_resourceid.h file. Turning off the warning globally is not preferable.

Problem code link

Compiler error output for gcc:

cFS/cfe/modules/core_api/fsw/inc/cfe_resourceid.h: In function 'CFE_ResourceId_t CFE_ResourceId_FromInteger(long unsigned int)':
cFS/cfe/modules/core_api/fsw/inc/cfe_resourceid.h:123:50: error: narrowing conversion of 'Value' from 'long unsigned int' to 'uint32' {aka 'unsigned int'} [-Werror=narrowing]
  123 |     return (CFE_ResourceId_t)CFE_RESOURCEID_WRAP(Value);
      |                                                  ^~~~~
cFS/cfe/modules/resourceid/option_inc/cfe_resourceid_strict.h:87:9: note: in definition of macro 'CFE_RESOURCEID_WRAP'
   87 |         x                      \

To Reproduce
I made an easy to see example on compiler explorer.
Otherwise, you'll need to

  1. -Werror and OMIT_DEPRECATED=ON
  2. Compile a C++ file with gcc including "cfe.h"
  3. Should see the warning being treated as an error

Expected behavior
No warning.

Code snips
This is the simplified code I put in compiler explorer.

#include <cstdint>
typedef struct
{
    uint32_t id;
} CFE_ResourceId_t;

#define CFE_RESOURCEID_WRAP(x) \
    {                          \
        x                      \
    }

static inline CFE_ResourceId_t CFE_ResourceId_FromInteger(unsigned long Value)
{
    return (CFE_ResourceId_t)CFE_RESOURCEID_WRAP(Value);
}

int main() {}

Potential Fix
One option is to insert an explicit cast to silence this issue (Works in C & Works in C++). Not the best given this now has to assume uint32 but maybe the simple and strict header files can provide the type used within CFE_ResourceId_t.

static inline CFE_ResourceId_t CFE_ResourceId_FromInteger(unsigned long Value)
{
    return (CFE_ResourceId_t)CFE_RESOURCEID_WRAP((uint32)Value);
}

System observed on
This code is in develop of cFE

Reporter Info
Philip Cooksey @ NASA/ARC

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions