Haswell
Haswell

Reputation: 1652

Multiple enum members same name

I was browsing through some netfilter code.

I am not aware of any C rules which specify enum members with repeated names.

enum    ctattr_type { 
  CTA_UNSPEC, 
  CTA_TUPLE_ORIG, 
  CTA_TUPLE_REPLY, 
  CTA_STATUS, 
  CTA_PROTOINFO, 
  CTA_HELP, 
  CTA_NAT_SRC, 
  CTA_TIMEOUT, 
  CTA_MARK, 
  CTA_COUNTERS_ORIG, 
  CTA_COUNTERS_REPLY, 
  CTA_USE, 
  CTA_ID, 
  CTA_NAT_DST, 
  CTA_TUPLE_MASTER, 
  CTA_NAT_SEQ_ADJ_ORIG, 
  CTA_NAT_SEQ_ADJ_REPLY, 
  CTA_SECMARK, 
  CTA_ZONE, 
  CTA_SECCTX, 
  CTA_TIMESTAMP, 
  CTA_MARK_MASK, 
  CTA_LABELS, 
  CTA_LABELS_MASK, 
  __CTA_MAX, 
  CTA_UNSPEC, 
  CTA_TUPLE_ORIG, 
  CTA_TUPLE_REPLY, 
  CTA_STATUS, 
  CTA_PROTOINFO, 
  CTA_HELP, 
  CTA_NAT_SRC, 
  CTA_TIMEOUT, 
  CTA_MARK, 
  CTA_COUNTERS_ORIG, 
  CTA_COUNTERS_REPLY, 
  CTA_USE, 
  CTA_ID, 
  CTA_NAT_DST, 
  CTA_TUPLE_MASTER, 
  CTA_NAT_SEQ_ADJ_ORIG, 
  CTA_NAT_SEQ_ADJ_REPLY, 
  CTA_SECMARK, 
  CTA_ZONE, 
  CTA_SECCTX, 
  CTA_TIMESTAMP, 
  CTA_MARK_MASK, 
  CTA_LABELS, 
  CTA_LABELS_MASK, 
  __CTA_MAX, 
  CTA_UNSPEC, 
  CTA_TUPLE_ORIG, 
  CTA_TUPLE_REPLY, 
  CTA_STATUS, 
  CTA_PROTOINFO, 
  CTA_HELP, 
  CTA_NAT_SRC, 
  CTA_TIMEOUT, 
  CTA_MARK, 
  CTA_COUNTERS_ORIG, 
  CTA_COUNTERS_REPLY, 
  CTA_USE, 
  CTA_ID, 
  CTA_NAT_DST, 
  __CTA_MAX 
}

Here __CTA_MAX appears multiple times. Infact many other attribute enums get repeated defined.

When I printf the __CTA_MAX value it prints the first occurence enum value. 23

So what is the use of the other __CTA_MAX values. What is the use of other attributes repeated.

Upvotes: 2

Views: 914

Answers (1)

Martin R
Martin R

Reputation: 539795

From the C11 standard (taken from http://port70.net/~nsz/c/c11/n1570.html#6.7.2.2):

6.7.2.2 Enumeration specifiers
...
Footnotes:
127) Thus, the identifiers of enumeration constants declared in the same scope shall all be distinct from each other and from other identifiers declared in ordinary declarators.

So using the same enumeration constant repeatedly in one enumeration (or even using the same enumeration constant in two different enumerations in the same scope) is not valid in C.

What you see is most probably an artefact created by Doxygen, and does not reflect the actual definition in the source code.

The definition in linux_nfnetlink_conntrack.h from http://git.netfilter.org is just

enum ctattr_type {
    CTA_UNSPEC,
    CTA_TUPLE_ORIG,
    CTA_TUPLE_REPLY,
    CTA_STATUS,
    CTA_PROTOINFO,
    CTA_HELP,
    CTA_NAT_SRC,
#define CTA_NAT CTA_NAT_SRC /* backwards compatibility */
    CTA_TIMEOUT,
    CTA_MARK,
    CTA_COUNTERS_ORIG,
    CTA_COUNTERS_REPLY,
    CTA_USE,
    CTA_ID,
    CTA_NAT_DST,
    CTA_TUPLE_MASTER,
    CTA_SEQ_ADJ_ORIG,
    CTA_NAT_SEQ_ADJ_ORIG    = CTA_SEQ_ADJ_ORIG,
    CTA_SEQ_ADJ_REPLY,
    CTA_NAT_SEQ_ADJ_REPLY   = CTA_SEQ_ADJ_REPLY,
    CTA_SECMARK,        /* obsolete */
    CTA_ZONE,
    CTA_SECCTX,
    CTA_TIMESTAMP,
    CTA_MARK_MASK,
    CTA_LABELS,
    CTA_LABELS_MASK,
    __CTA_MAX
};
#define CTA_MAX (__CTA_MAX - 1)

Upvotes: 5

Related Questions