Skip to content

Commit

Permalink
Merge pull request #3241 from BsAtHome/fix_typepunned-deref
Browse files Browse the repository at this point in the history
Fix type-punned dereference warning
  • Loading branch information
andypugh authored Jan 6, 2025
2 parents c242780 + e8fb461 commit c903ccd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/libnml/nml/stat_msg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int RCS_STAT_MSG_format(NMLTYPE t, void *buf, CMS * cms)
{
cms->update(((RCS_STAT_MSG *) buf)->command_type);
cms->update(((RCS_STAT_MSG *) buf)->echo_serial_number);
cms->update((int&)(((RCS_STAT_MSG *) buf)->status));
cms->update(((RCS_STAT_MSG *) buf)->status_int);
cms->update(((RCS_STAT_MSG *) buf)->state);

switch (t) {
Expand Down
17 changes: 16 additions & 1 deletion src/libnml/nml/stat_msg.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,22 @@ class RCS_STAT_MSG:public NMLmsg {
RCS_STAT_MSG(NMLTYPE t, size_t sz);
NMLTYPE command_type;
int echo_serial_number;
RCS_STATUS status;
// The anonymous union is provided to allow for RCS_STAT_MSG_format() in
// nml/stat_msg.cc to call cms->update() with an integer reference. Member
// 'status' is normally accessed. Casting the 'status' member to int& in
// the cms->update() call will give a 'type-punned pointer dereference'
// warning. We sidestep this problem with the union where we can address
// either field and get the same value.
// The union is necessary because the update() call implementation checks
// the address of its argument to be in a specific memory region, which
// excludes using a temporary. Now, with the union, both members 'status'
// and 'status_int' share the same memory location. The 'RCS_STATUS' enum
// in rcs/rcs.hh explicitly declares the enum's underlying type as int.
// Therefore, both union members are of same underlying type.
union {
RCS_STATUS status;
int status_int;
};
int state;
};

Expand Down
5 changes: 4 additions & 1 deletion src/libnml/rcs/rcs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class RCS_TIMER;
class RCS_CMD_MSG;
class RCS_STAT_MSG;

enum class RCS_STATUS { /* Originally from nml_mod.hh */
// The underlying type specifier of int is meant to stress that it
// is of paramount importance that the underlying type of the
// RCS_STATUS enum is int. See nml/stat_msg.hh for more details.
enum class RCS_STATUS : int { /* Originally from nml_mod.hh */
UNINITIALIZED = -1,
DONE = 1,
EXEC = 2,
Expand Down

0 comments on commit c903ccd

Please sign in to comment.