Skip to contentSkip to conversation

e1000_initialize: memset 0x0 nic and hw structure before their use

PATCH New Prabhakar 3 participants 4 messages Nov 11, 2010 Network drivers

lore
Share & tools
lore

Prabhakar

author
#0 lore raw

PATCH 1/1 e1000_initialize: memset 0x0 nic and hw structure before their use

nic and hw structures are allocated via malloc i.e. return memory is not zero initialized. Because of this few structure member like "function pointers" are initialized with garbage values.

It may cause problem. for eg. during eth_initialize, dev->write_hwaddr is used.

---
e1000_initialize: memset 0x0 nic and hw structure before their use
- Space left after sizeof is for the uniformity of this file

 drivers/net/e1000.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
drivers/net/e1000.c +12 -0
index 2825342..6f88012 100644
@@ -5177,7 +5177,19 @@ e1000_initialize(bd_t * bis)
 		}
 
 		nic = (struct eth_device *) malloc(sizeof (*nic));
+		if (nic)
+			memset(nic, 0, sizeof (*nic));
+		else
+			return 0;
+
 		hw = (struct e1000_hw *) malloc(sizeof (*hw));
+		if (hw)
+			memset(hw, 0, sizeof (*hw));
+		else {
+			free(nic);
+			return 0;
+		}
+
 		hw->pdev = devno;
 		nic->priv = hw;
 
Signature · 1 line
1.6.3.1
#1 lore raw

PATCH 1/1 e1000_initialize: memset 0x0 nic and hw structure before their use

Dear Prabhakar,

Quoted 6 lines · In message <[email protected]> you…

nic and hw structures are allocated via malloc i.e. return memory is not zero initialized. Because of this few structure member like "function pointers" are initialized with garbage values.

It may cause problem. for eg. during eth_initialize, dev->write_hwaddr is used.

thanks. I already have a patch series "Add initialized eth_device structure" on my stack. I think this covers this, too.

drivers/net/e1000.c e1000_initialize(bd_t * bis) · line 5183

Quoted 5 lines
drivers/net/e1000.c +4 -0
 		nic = (struct eth_device *) malloc(sizeof (*nic));
+		if (nic)
+			memset(nic, 0, sizeof (*nic));
+		else
+			return 0;

Please don't write code like that. Why would the memset() [and only this, none of the following code whch is in the same logioc branch?] be in the if() branch?

Write instead:

	nic = (struct eth_device *) malloc(sizeof (*nic));
	if (!nic) {
		issue error message
		return error code
	}

	memset();
	...

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Our missions are peaceful -- not for conquest.  When we do battle, it
is only because we have no choice.
	-- Kirk, "The Squire of Gothos", stardate 2124.5
#2 lore raw

PATCH 1/1 e1000_initialize: memset 0x0 nic and hw structure before their use

Quoted 7 lines · Wolfgang Denk

Dear Prabhakar,

Quoted 4 lines · In message <[email protected]> you…

nic and hw structures are allocated via malloc i.e. return memory is not zero initialized. Because of this few structure member like "function pointers" are initialized with garbage values.

Quoted 5 lines
Quoted 2 lines

It may cause problem. for eg. during eth_initialize, dev->write_hwaddr is used.

thanks. I already have a patch series "Add initialized eth_device structure" on my stack. I think this covers this, too.

which patch is that?

- k

#3 lore raw

PATCH 1/1 e1000_initialize: memset 0x0 nic and hw structure before their use

Dear Kumar Gala,

Quoted 5 lines · In message <[email protected]> you wrote:
Quoted 3 lines

thanks. I already have a patch series "Add initialized eth_device structure" on my stack. I think this covers this, too.

which patch is that?

Um... you're right, e1000 is not included there, only e100.

In any case, the fix should be implemented in the same way and style as in that series - for a reference please see
http://article.gmane.org/gmane.comp.boot-loaders.u-boot/86816

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"There's only one kind of woman ..." "Or man, for  that  matter.  You
either believe in yourself or you don't."
	-- Kirk and Harry Mudd, "Mudd's Women", stardate 1330.1