linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] libsocketcan: Initialize stack variables properly
@ 2014-02-07  9:21 Alexander Shiyan
  2014-02-07  9:21 ` [PATCH 2/2] libsocketcan: Remove excess goto's Alexander Shiyan
  2014-02-07  9:40 ` [PATCH 1/2] libsocketcan: Initialize stack variables properly Marc Kleine-Budde
  0 siblings, 2 replies; 4+ messages in thread
From: Alexander Shiyan @ 2014-02-07  9:21 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Alexander Shiyan

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

Signed-off-by: Alexander Shiyan <shc_work@mail.ru>
---
 src/libsocketcan.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/src/libsocketcan.c b/src/libsocketcan.c
index 02714ae..4e7842f 100644
--- a/src/libsocketcan.c
+++ b/src/libsocketcan.c
@@ -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);
 }
 
 /**
-- 
1.8.3.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] libsocketcan: Remove excess goto's.
  2014-02-07  9:21 [PATCH 1/2] libsocketcan: Initialize stack variables properly Alexander Shiyan
@ 2014-02-07  9:21 ` Alexander Shiyan
  2014-02-07  9:42   ` Marc Kleine-Budde
  2014-02-07  9:40 ` [PATCH 1/2] libsocketcan: Initialize stack variables properly Marc Kleine-Budde
  1 sibling, 1 reply; 4+ messages in thread
From: Alexander Shiyan @ 2014-02-07  9:21 UTC (permalink / raw)
  To: linux-can; +Cc: Marc Kleine-Budde, Alexander Shiyan

Signed-off-by: Alexander Shiyan <shc_work@mail.ru>
---
 src/libsocketcan.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/src/libsocketcan.c b/src/libsocketcan.c
index 4e7842f..82d4505 100644
--- a/src/libsocketcan.c
+++ b/src/libsocketcan.c
@@ -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);
 }
 
 /**
-- 
1.8.3.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] libsocketcan: Initialize stack variables properly
  2014-02-07  9:21 [PATCH 1/2] libsocketcan: Initialize stack variables properly Alexander Shiyan
  2014-02-07  9:21 ` [PATCH 2/2] libsocketcan: Remove excess goto's Alexander Shiyan
@ 2014-02-07  9:40 ` Marc Kleine-Budde
  1 sibling, 0 replies; 4+ messages in thread
From: Marc Kleine-Budde @ 2014-02-07  9:40 UTC (permalink / raw)
  To: Alexander Shiyan, linux-can

[-- Attachment #1: Type: text/plain, Size: 1398 bytes --]

On 02/07/2014 10:21 AM, Alexander Shiyan wrote:
> Stack variables may have unpredictable data if it not initialized.
> This patch adds the necessary cleanup stack variables.
> 
> Signed-off-by: Alexander Shiyan <shc_work@mail.ru>
> ---
>  src/libsocketcan.c | 40 ++++++++++++++++++++++------------------
>  1 file changed, 22 insertions(+), 18 deletions(-)
> 
> diff --git a/src/libsocketcan.c b/src/libsocketcan.c
> index 02714ae..4e7842f 100644
> --- a/src/libsocketcan.c
> +++ b/src/libsocketcan.c
> @@ -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;

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

-- 
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   |


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 242 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] libsocketcan: Remove excess goto's.
  2014-02-07  9:21 ` [PATCH 2/2] libsocketcan: Remove excess goto's Alexander Shiyan
@ 2014-02-07  9:42   ` Marc Kleine-Budde
  0 siblings, 0 replies; 4+ messages in thread
From: Marc Kleine-Budde @ 2014-02-07  9:42 UTC (permalink / raw)
  To: Alexander Shiyan, linux-can

[-- Attachment #1: Type: text/plain, Size: 417 bytes --]

On 02/07/2014 10:21 AM, Alexander Shiyan wrote:
> Signed-off-by: Alexander Shiyan <shc_work@mail.ru>

looks good. applied.

Tnx, Marc

-- 
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   |


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 242 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-02-07  9:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-02-07  9:21 [PATCH 1/2] libsocketcan: Initialize stack variables properly Alexander Shiyan
2014-02-07  9:21 ` [PATCH 2/2] libsocketcan: Remove excess goto's Alexander Shiyan
2014-02-07  9:42   ` Marc Kleine-Budde
2014-02-07  9:40 ` [PATCH 1/2] libsocketcan: Initialize stack variables properly Marc Kleine-Budde

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).