Skip to content

Commit c903ccd

Browse files
authored
Merge pull request #3241 from BsAtHome/fix_typepunned-deref
Fix type-punned dereference warning
2 parents c242780 + e8fb461 commit c903ccd

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/libnml/nml/stat_msg.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ int RCS_STAT_MSG_format(NMLTYPE t, void *buf, CMS * cms)
3535
{
3636
cms->update(((RCS_STAT_MSG *) buf)->command_type);
3737
cms->update(((RCS_STAT_MSG *) buf)->echo_serial_number);
38-
cms->update((int&)(((RCS_STAT_MSG *) buf)->status));
38+
cms->update(((RCS_STAT_MSG *) buf)->status_int);
3939
cms->update(((RCS_STAT_MSG *) buf)->state);
4040

4141
switch (t) {

src/libnml/nml/stat_msg.hh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,22 @@ class RCS_STAT_MSG:public NMLmsg {
2323
RCS_STAT_MSG(NMLTYPE t, size_t sz);
2424
NMLTYPE command_type;
2525
int echo_serial_number;
26-
RCS_STATUS status;
26+
// The anonymous union is provided to allow for RCS_STAT_MSG_format() in
27+
// nml/stat_msg.cc to call cms->update() with an integer reference. Member
28+
// 'status' is normally accessed. Casting the 'status' member to int& in
29+
// the cms->update() call will give a 'type-punned pointer dereference'
30+
// warning. We sidestep this problem with the union where we can address
31+
// either field and get the same value.
32+
// The union is necessary because the update() call implementation checks
33+
// the address of its argument to be in a specific memory region, which
34+
// excludes using a temporary. Now, with the union, both members 'status'
35+
// and 'status_int' share the same memory location. The 'RCS_STATUS' enum
36+
// in rcs/rcs.hh explicitly declares the enum's underlying type as int.
37+
// Therefore, both union members are of same underlying type.
38+
union {
39+
RCS_STATUS status;
40+
int status_int;
41+
};
2742
int state;
2843
};
2944

src/libnml/rcs/rcs.hh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class RCS_TIMER;
2828
class RCS_CMD_MSG;
2929
class RCS_STAT_MSG;
3030

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

0 commit comments

Comments
 (0)