Skip to contentSkip to conversation

libsocketcan: Initialize stack variables properly

PATCH In review Alexander Shiyan 2 participants 4 messages Feb 7, 2014 CAN network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libsocketcan: Initialize stack variables properly

Stack variables may have unpredictable data if it not initialized. This patch adds the necessary cleanup stack variables.

---
 src/libsocketcan.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)
src/libsocketcan.c +22 -18
index 02714ae..4e7842f 100644
@@ -705,6 +705,7 @@ int can_do_stop(const char *name)
  */
 int can_do_restart(const char *name)
 {
+	struct req_info req;
 	int err = -1;
 	int state;
 	__u32 restart_ms;
@@ -731,11 +732,10 @@ int can_do_restart(const char *name)
 		goto err_out;
 	}
 
-	struct req_info req_info = {
-		.restart = 1,
-	};
+	memset(&req, 0, sizeof(req));
+	req.restart = 1;
 
-	err = set_link(name, 0, &req_info);
+	err = set_link(name, 0, &req);
 
 err_out:
 	return err;
@@ -758,14 +758,16 @@ err_out:
  */
 int can_set_restart_ms(const char *name, __u32 restart_ms)
 {
-	struct req_info req_info = {
-		.restart_ms = restart_ms,
-	};
+	struct req_info req;
 
-	if (restart_ms == 0)
-		req_info.disable_autorestart = 1;
+	memset(&req, 0, sizeof(req));
+
+	if (restart_ms)
+		req.restart_ms = restart_ms;
+	else
+		req.disable_autorestart = 1;
 
-	return set_link(name, 0, &req_info);
+	return set_link(name, 0, &req);
 }
 
 /**
@@ -822,11 +824,12 @@ int can_set_restart_ms(const char *name, __u32 restart_ms)
 
 int can_set_ctrlmode(const char *name, struct can_ctrlmode *cm)
 {
-	struct req_info req_info = {
-		.ctrlmode = cm,
-	};
+	struct req_info req;
 
-	return set_link(name, 0, &req_info);
+	memset(&req, 0, sizeof(req));
+	req.ctrlmode = cm;
+
+	return set_link(name, 0, &req);
 }
 
 /**
@@ -869,11 +872,12 @@ int can_set_ctrlmode(const char *name, struct can_ctrlmode *cm)
 
 int can_set_bittiming(const char *name, struct can_bittiming *bt)
 {
-	struct req_info req_info = {
-		.bittiming = bt,
-	};
+	struct req_info req;
+
+	memset(&req, 0, sizeof(req));
+	req.bittiming = bt;
 
-	return set_link(name, 0, &req_info);
+	return set_link(name, 0, &req);
 }
 
 /**
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH 2/2 libsocketcan: Remove excess goto's.

---
 src/libsocketcan.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
src/libsocketcan.c +5 -9
index 4e7842f..82d4505 100644
@@ -706,7 +706,6 @@ int can_do_stop(const char *name)
 int can_do_restart(const char *name)
 {
 	struct req_info req;
-	int err = -1;
 	int state;
 	__u32 restart_ms;
 
@@ -714,31 +713,28 @@ int can_do_restart(const char *name)
 	if ((can_get_state(name, &state)) < 0) {
 		fprintf(stderr, "cannot get bustate, "
 			"something is seriously wrong\n");
-		goto err_out;
+		return -1;
 	} else if (state != CAN_STATE_BUS_OFF) {
 		fprintf(stderr,
 			"Device is not in BUS_OFF," " no use to restart\n");
-		goto err_out;
+		return -1;
 	}
 
 	if ((can_get_restart_ms(name, &restart_ms)) < 0) {
 		fprintf(stderr, "cannot get restart_ms, "
 			"something is seriously wrong\n");
-		goto err_out;
+		return -1;
 	} else if (restart_ms > 0) {
 		fprintf(stderr,
 			"auto restart with %ums interval is turned on,"
 			" no use to restart\n", restart_ms);
-		goto err_out;
+		return -1;
 	}
 
 	memset(&req, 0, sizeof(req));
 	req.restart = 1;
 
-	err = set_link(name, 0, &req);
-
-err_out:
-	return err;
+	return set_link(name, 0, &req);
 }
 
 /**
Signature · 1 line
1.8.3.2
#2 lore raw

Re: [PATCH 2/2] libsocketcan: Remove excess goto's.

Quoted 1 line · Alexander Shiyan

looks good. applied.

Tnx, Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
#3 lore raw
In reply to Alexander Shiyan
Quoted 21 lines · Alexander Shiyan

Stack variables may have unpredictable data if it not initialized. This patch adds the necessary cleanup stack variables.

---
 src/libsocketcan.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)
src/libsocketcan.c +1 -0
index 02714ae..4e7842f 100644
@@ -705,6 +705,7 @@ int can_do_stop(const char *name)
  */
 int can_do_restart(const char *name)
 {
+	struct req_info req;
 	int err = -1;
 	int state;
 	__u32 restart_ms;

src/libsocketcan.c int can_do_restart(const char *name) · line 736

Quoted 8 lines
src/libsocketcan.c +2 -3
@@ -731,11 +732,10 @@ int can_do_restart(const char *name)
 		goto err_out;
 	}
 
-	struct req_info req_info = {
-		.restart = 1,
-	};
+	memset(&req, 0, sizeof(req));
+	req.restart = 1;

This is equivalent, all other members of req_info are set to 0x0, guaranteed by C standard. It was added with C99 and support by gcc for a long time.

If think we can drop the patch.

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |