diff mbox

[U-Boot,1/1] usbtty: avoid potential NULL pointer dereference

Message ID 20170415130546.25341-1-xypron.glpk@gmx.de
State Accepted
Commit 73be5b3fa73e329ad0d9e12db427cd665e1a483c
Delegated to: Tom Rini
Headers show

Commit Message

Heinrich Schuchardt April 15, 2017, 1:05 p.m. UTC
If current_urb is NULL it should not be dereferenced.

The problem was indicated by cppcheck.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 drivers/serial/usbtty.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

Comments

Tom Rini April 15, 2017, 4:12 p.m. UTC | #1
On Sat, Apr 15, 2017 at 03:05:46PM +0200, Heinrich Schuchardt wrote:

> If current_urb is NULL it should not be dereferenced.
> 
> The problem was indicated by cppcheck.
> 
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

Reviewed-by: Tom Rini <trini@konsulko.com>
Marek Vasut April 16, 2017, 4:11 p.m. UTC | #2
On 04/15/2017 03:05 PM, Heinrich Schuchardt wrote:
> If current_urb is NULL it should not be dereferenced.
> 
> The problem was indicated by cppcheck.
> 
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

Applied, thanks.

> ---
>  drivers/serial/usbtty.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/serial/usbtty.c b/drivers/serial/usbtty.c
> index 2e19813643..29799dce93 100644
> --- a/drivers/serial/usbtty.c
> +++ b/drivers/serial/usbtty.c
> @@ -850,6 +850,13 @@ static int write_buffer (circbuf_t * buf)
>  	struct urb *current_urb = NULL;
>  
>  	current_urb = next_urb (device_instance, endpoint);
> +
> +	if (!current_urb) {
> +		TTYERR ("current_urb is NULL, buf->size %d\n",
> +		buf->size);
> +		return 0;
> +	}
> +
>  	/* TX data still exists - send it now
>  	 */
>  	if(endpoint->sent < current_urb->actual_length){
> @@ -871,12 +878,6 @@ static int write_buffer (circbuf_t * buf)
>  		 */
>  		while (buf->size > 0) {
>  
> -			if (!current_urb) {
> -				TTYERR ("current_urb is NULL, buf->size %d\n",
> -					buf->size);
> -				return total;
> -			}
> -
>  			dest = (char*)current_urb->buffer +
>  				current_urb->actual_length;
>  
>
Tom Rini April 19, 2017, 1:03 p.m. UTC | #3
On Sat, Apr 15, 2017 at 03:05:46PM +0200, xypron.glpk@gmx.de wrote:

> If current_urb is NULL it should not be dereferenced.
> 
> The problem was indicated by cppcheck.
> 
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> Reviewed-by: Tom Rini <trini@konsulko.com>

Applied to u-boot/master, thanks!
diff mbox

Patch

diff --git a/drivers/serial/usbtty.c b/drivers/serial/usbtty.c
index 2e19813643..29799dce93 100644
--- a/drivers/serial/usbtty.c
+++ b/drivers/serial/usbtty.c
@@ -850,6 +850,13 @@  static int write_buffer (circbuf_t * buf)
 	struct urb *current_urb = NULL;
 
 	current_urb = next_urb (device_instance, endpoint);
+
+	if (!current_urb) {
+		TTYERR ("current_urb is NULL, buf->size %d\n",
+		buf->size);
+		return 0;
+	}
+
 	/* TX data still exists - send it now
 	 */
 	if(endpoint->sent < current_urb->actual_length){
@@ -871,12 +878,6 @@  static int write_buffer (circbuf_t * buf)
 		 */
 		while (buf->size > 0) {
 
-			if (!current_urb) {
-				TTYERR ("current_urb is NULL, buf->size %d\n",
-					buf->size);
-				return total;
-			}
-
 			dest = (char*)current_urb->buffer +
 				current_urb->actual_length;