Skip to contentSkip to conversation

mv643xx_eth: potential null dereference

PATCH Superseded Dan Carpenter 5 participants 11 messages Jul 23, 2010 – Jul 25, 2010

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mv643xx_eth: potential null dereference

We assume that "pd" can be null on the previous line, and throughout the function so we should check it here as well. This was introduced by 9b2c2ff7a1c0 "mv643xx_eth: use sw csum for big packets"

drivers/net/mv643xx_eth.c +2 -1
index 2fcdb1e..9166f55 100644
@@ -2675,7 +2675,8 @@ static int mv643xx_eth_shared_probe(struct platform_device *pdev)
 	 * Detect hardware parameters.
 	 */
 	msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
-	msp->tx_csum_limit = pd->tx_csum_limit ? pd->tx_csum_limit : 9 * 1024;
+	msp->tx_csum_limit = (pd && pd->tx_csum_limit) ?
+					pd->tx_csum_limit : 9 * 1024;
 	infer_hw_params(msp);
 
 	platform_set_drvdata(pdev, msp);
#1 lore raw
Quoted 11 lines · Dan Carpenter

We assume that "pd" can be null on the previous line, and throughout the function so we should check it here as well. This was introduced by 9b2c2ff7a1c0 "mv643xx_eth: use sw csum for big packets"

drivers/net/mv643xx_eth.c +0 -0
index 2fcdb1e..9166f55 100644

drivers/net/mv643xx_eth.c static int mv643xx_eth_shared_probe(struct platform_device *pdev) · line 2679

Quoted 6 lines
drivers/net/mv643xx_eth.c +2 -1
@@ -2675,7 +2675,8 @@ static int mv643xx_eth_shared_probe(struct platform_device *pdev)
 	 * Detect hardware parameters.
 	 */
 	msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
-	msp->tx_csum_limit = pd->tx_csum_limit ? pd->tx_csum_limit : 9 * 1024;
+	msp->tx_csum_limit = (pd && pd->tx_csum_limit) ?
+					pd->tx_csum_limit : 9 * 1024;

It's odd using two different check styles for the same test on consecutive lines.

How about using the same style:

	msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
					pd->tx_csum_limit : 9 * 1024;
#2 lore raw

PATCH v2 1/1 mv643xx_eth: potential null dereference

We assume that "pd" can be null on the previous line, and throughout the function so we should check it here as well. This was introduced by 9b2c2ff7a1c0 "mv643xx_eth: use sw csum for big packets"

---
v2: style change

drivers/net/mv643xx_eth.c +2 -1
index 73bb8ea..f5e72fe 100644
@@ -2670,7 +2670,8 @@ static int mv643xx_eth_shared_probe(struct platform_device *pdev)
 	 * Detect hardware parameters.
 	 */
 	msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
-	msp->tx_csum_limit = pd->tx_csum_limit ? pd->tx_csum_limit : 9 * 1024;
+	msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
+					pd->tx_csum_limit : 9 * 1024;
 	infer_hw_params(msp);
 
 	platform_set_drvdata(pdev, msp);
#5 lore raw
In reply to Dan Carpenter
Quoted 13 lines · Dan Carpenter

We assume that "pd" can be null on the previous line, and throughout the function so we should check it here as well. This was introduced by 9b2c2ff7a1c0 "mv643xx_eth: use sw csum for big packets"

---
v2: style change

drivers/net/mv643xx_eth.c +0 -0
index 73bb8ea..f5e72fe 100644

drivers/net/mv643xx_eth.c static int mv643xx_eth_shared_probe(struct platform_device *pdev) · line 2677

Quoted 9 lines
drivers/net/mv643xx_eth.c +2 -1
@@ -2670,7 +2670,8 @@ static int mv643xx_eth_shared_probe(struct platform_device *pdev)
 	 * Detect hardware parameters.
 	 */
 	msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
-	msp->tx_csum_limit = pd->tx_csum_limit ? pd->tx_csum_limit : 9 * 1024;
+	msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
+					pd->tx_csum_limit : 9 * 1024;
 	infer_hw_params(msp);
 
 	platform_set_drvdata(pdev, msp);

this is a bit complicated, IMHO ppl have a bigger chance to discover what is going on with this version:

    if (!pd ) {
       msp->t_clk = 133000000;
       msp->tx_csum_limit = 9 * 1024;
     }
     else
      {
	msp->t_clk = pd->t_clk ? pd->t_clk : 133000000 ;
        msp->tx_csum_limit = pd->tx_csum_limit ? pd->tx_csum_limit : 9 * 1024;
       }

re,
wh

#6 lore raw
Quoted 13 lines · walter harms

this is a bit complicated, IMHO ppl have a bigger chance to discover what is going on with this version:

    if (!pd ) {
       msp->t_clk = 133000000;
       msp->tx_csum_limit = 9 * 1024;
     }
     else
      {
	msp->t_clk = pd->t_clk ? pd->t_clk : 133000000 ;
        msp->tx_csum_limit = pd->tx_csum_limit ? pd->tx_csum_limit : 9 * 1024;
       }

But then instead of 2 magic numbers we would have 4. :/

regards,
dan carpenter

#7 lore raw
Quoted 15 lines · Dan Carpenter
Quoted 13 lines · walter harms

this is a bit complicated, IMHO ppl have a bigger chance to discover what is going on with this version:

    if (!pd ) {
       msp->t_clk = 133000000;
       msp->tx_csum_limit = 9 * 1024;
     }
     else
      {
	msp->t_clk = pd->t_clk ? pd->t_clk : 133000000 ;
        msp->tx_csum_limit = pd->tx_csum_limit ? pd->tx_csum_limit : 9 * 1024;
       }
Quoted 2 lines

But then instead of 2 magic numbers we would have 4. :/

Yes it can be shorten,

msp->t_clk = 133000000;
msp->tx_csum_limit = 9 * 1024;

if (pd) {
	if (pd->t_clk) msp->t_clk = pd->t_clk ;
	if (pd->tx_csum_limit) sp->tx_csum_limit = pd->tx_csum_limit;
}

pd->t_clk
i was thinking about that in my first posting but i have the feeling that ppl tend to overlook the "if (pd)" but in this case it is important since the behavier is different.

IMHO it would be better to make sure that pd->t_clk,pd->tx_csum_limit have usefull values than adding a check but this is up to the maintainer.

re,
wh

#8 lore raw
Quoted 2 lines · walter harms

IMHO it would be better to make sure that pd->t_clk,pd->tx_csum_limit have usefull values than adding a check but this is up to the maintainer.

I don't see the point of that at all. We check against zero to see whether the caller bothered to fill in the field at all, but if the caller wants to pass in bogus values, that's up to the caller.

#9 lore raw
Quoted 9 lines · Lennert Buytenhek
Quoted 2 lines · walter harms

IMHO it would be better to make sure that pd->t_clk,pd->tx_csum_limit have usefull values than adding a check but this is up to the maintainer.

I don't see the point of that at all. We check against zero to see whether the caller bothered to fill in the field at all, but if the caller wants to pass in bogus values, that's up to the caller.

at first i have to admit i looked only at the patch. for me the situation looks this way:

You check the values for 0 (and uses default) or take what ever in pd is. The current code is setup like:

1. check if pd is set
2. check if pd->value is non zero and use it

the whole "check X" can be avoided if you could create a pd with all values set to default and just take what comes from the user.

my objection agains this kind of code is that it is not obvious what some one is trying to archive
(pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;

the pd check means: do i have a configuration ? the pd->t_clk != 0 means: should i use then or default ?

This is mixing two very different questions. therefore my idea in the last posting to have a default init if (!pd) and then use the else to make clear that additional checks for pd->value are expected.

this this is the init code readability and simplicity should be king.

hope that helps,
re,
wh

#10 lore raw
Quoted 10 lines · walter harms
Quoted 6 lines
Quoted 2 lines

IMHO it would be better to make sure that pd->t_clk,pd->tx_csum_limit have usefull values than adding a check but this is up to the maintainer.

I don't see the point of that at all. We check against zero to see whether the caller bothered to fill in the field at all, but if the caller wants to pass in bogus values, that's up to the caller.

at first i have to admit i looked only at the patch. for me the situation looks this way:

Quoted 8 lines

You check the values for 0 (and uses default) or take what ever in pd is. The current code is setup like:

1. check if pd is set
2. check if pd->value is non zero and use it

the whole "check X" can be avoided if you could create a pd with all values set to default and just take what comes from the user.

Why would you want to avoid "check X"?

Quoted 5 lines

my objection agains this kind of code is that it is not obvious what some one is trying to archive
(pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;

the pd check means: do i have a configuration ?

Yep.

Quoted 1 line

the pd->t_clk != 0 means: should i use then or default ?

Yep.

Quoted 1 line

This is mixing two very different questions.

And?

You're arguing against the use of 'if (a && b)' in general?

Quoted 3 lines

therefore my idea in the last posting to have a default init if (!pd) and then use the else to make clear that additional checks for pd->value are expected.

Again, I don't see the point of this, and I think we're just splitting hairs here and wasting everyone's time.

Quoted 1 line

this this is the init code readability and simplicity should be king.

'if (foo != NULL && foo->value <op> <value>)' is a fairly common C construct, in my opinion, and I don't see how expanding it into a multi-line nested if construct will make it either more readable or simpler.

Quoted 1 line

hope that helps,

Not really.

thanks,
Lennert