Skip to contentSkip to conversation

netfilter: xtables: add const qualifiers

PATCH Alexey Dobriyan 3 participants 5 messages Feb 11, 2010 netfilter

lore
Share & tools
lore
#0 lore raw

Review

Quoted 5 lines
(quoted diff) +3 -2
-static inline int ebt_basic_match(struct ebt_entry *e, struct ethhdr *h,
-   const struct net_device *in, const struct net_device *out)
+static inline int
+ebt_basic_match(const struct ebt_entry *e, const struct ethhdr *h,
+                const struct net_device *in, const struct net_device *out)

These const modifiers are pointless because compilers are smart enough to notice non-modifiability and generate the very same code in both cases.

Nowadays, half of functions declarations in generic xtables/iptables/ip6tables/arptables
code are littered with const which makes them pretty unpleasant to read.

Review

Quoted 2 lines
(quoted diff) +1 -1
-	struct ebt_entry *e = (struct ebt_entry *)chain->data;
+	const struct ebt_entry *e = (struct ebt_entry *)chain->data;

And such things are wrong (not second const).

Please, at least, keep "struct net *" out of this.

#1 lore raw
Quoted 9 lines · Alexey Dobriyan

Review

Quoted 5 lines
(quoted diff) +3 -2
-static inline int ebt_basic_match(struct ebt_entry *e, struct ethhdr *h,
-   const struct net_device *in, const struct net_device *out)
+static inline int
+ebt_basic_match(const struct ebt_entry *e, const struct ethhdr *h,
+                const struct net_device *in, const struct net_device *out)

These const modifiers are pointless because compilers are smart enough to notice non-modifiability and generate the very same code in both cases.

Quoted 3 lines

Nowadays, half of functions declarations in generic xtables/iptables/ip6tables/arptables
code are littered with const which makes them pretty unpleasant to read.

Well, the benefit is that the compiler can catch accidental modification of data that is supposed to be constant.

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +1 -1
-	struct ebt_entry *e = (struct ebt_entry *)chain->data;
+	const struct ebt_entry *e = (struct ebt_entry *)chain->data;

And such things are wrong (not second const).

What's wrong about this?

#2 lore raw
Quoted 10 lines · Patrick McHardy
Quoted 9 lines · Alexey Dobriyan

Review

Quoted 5 lines
(quoted diff) +3 -2
-static inline int ebt_basic_match(struct ebt_entry *e, struct ethhdr *h,
-   const struct net_device *in, const struct net_device *out)
+static inline int
+ebt_basic_match(const struct ebt_entry *e, const struct ethhdr *h,
+                const struct net_device *in, const struct net_device *out)

These const modifiers are pointless because compilers are smart enough to notice non-modifiability and generate the very same code in both cases.

Quoted 6 lines
Quoted 3 lines

Nowadays, half of functions declarations in generic xtables/iptables/ip6tables/arptables
code are littered with const which makes them pretty unpleasant to read.

Well, the benefit is that the compiler can catch accidental modification of data that is supposed to be constant.

Did it ever? Realistically?

From what I see, const is used with string functions
(to make string literals go to rodata) and with structures full of hooks (to make them go to rodata).

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

-    struct ebt_entry *e = (struct ebt_entry *)chain->data;
+    const struct ebt_entry *e = (struct ebt_entry *)chain->data;

And such things are wrong (not second const).

What's wrong about this?

Should it be (const struct ebt_entry *)?
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 10 lines · Alexey Dobriyan
Quoted 9 lines · Patrick McHardy
Quoted 8 lines · Alexey Dobriyan

Review

Quoted 5 lines
(quoted diff) +3 -2
-static inline int ebt_basic_match(struct ebt_entry *e, struct ethhdr *h,
-   const struct net_device *in, const struct net_device *out)
+static inline int
+ebt_basic_match(const struct ebt_entry *e, const struct ethhdr *h,
+                const struct net_device *in, const struct net_device *out)

These const modifiers are pointless because compilers are smart enough to notice non-modifiability and generate the very same code in both cases.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Nowadays, half of functions declarations in generic xtables/iptables/ip6tables/arptables
code are littered with const which makes them pretty unpleasant to read.

Well, the benefit is that the compiler can catch accidental modification of data that is supposed to be constant.

Did it ever? Realistically?

Yes, occasionally it has caught typos or other variable mixups for me. It also documents that a variable is only supposed to be read.

Quoted 3 lines
Quoted 1 line

From what I see, const is used with string functions

(to make string literals go to rodata) and with structures full of hooks (to make them go to rodata).

Yes, that's also a reason.

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-    struct ebt_entry *e = (struct ebt_entry *)chain->data;
+    const struct ebt_entry *e = (struct ebt_entry *)chain->data;

And such things are wrong (not second const).

What's wrong about this?

Should it be (const struct ebt_entry *)?

That's not necessary. Its just declaring the the memory should not be modified through e. The const qualification happens automatically.

#4 lore raw
Quoted 11 lines · Patrick McHardy
Quoted 9 lines · Alexey Dobriyan
Quoted 8 lines · Patrick McHardy
Quoted 7 lines · Alexey Dobriyan

Review

Quoted 5 lines
(quoted diff) +3 -2
-static inline int ebt_basic_match(struct ebt_entry *e, struct ethhdr *h,
-   const struct net_device *in, const struct net_device *out)
+static inline int
+ebt_basic_match(const struct ebt_entry *e, const struct ethhdr *h,
+                const struct net_device *in, const struct net_device *out)

These const modifiers are pointless because compilers are smart enough to notice non-modifiability and generate the very same code in both cases.

Quoted 2 lines

Yes, occasionally it has caught typos or other variable mixups for me. It also documents that a variable is only supposed to be read.

In this case it allows parameter and local variable reduction because I have proven, by means of const, that they would remain the same over the entire function. (Scheduled commit 3c58069510:
http://dev.medozas.de/gitweb.cgi?p=linux;a=commit;h=3c58069510a498110240ceaff8c2a10c0d9b4460
- this will be in the next pull group.)

Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-    struct ebt_entry *e = (struct ebt_entry *)chain->data;
+    const struct ebt_entry *e = (struct ebt_entry *)chain->data;

And such things are wrong (not second const).

What's wrong about this?

Should it be (const struct ebt_entry *)?

That's not necessary. Its just declaring the the memory should not be modified through e. The const qualification happens automatically.

Conversion from T* to const T* is permitted and implicit across =.