Subject: Re: [PATCH net 2/2] ice: Add check for kzalloc

From: Jakub Kicinski <[email-masked]>
Date: Tue, 10 Jan 2023 18:26:07 -0800

On Mon,  9 Jan 2023 14:53:58 -0800 Tony Nguyen wrote:
> @@ -470,21 +473,23 @@ static struct tty_driver *ice_gnss_create_tty_driver(struct ice_pf *pf)
>  	err = tty_register_driver(tty_driver);
>  	if (err) {
>  		dev_err(dev, "Failed to register TTY driver err=%d\n", err);
> -
> -		for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) {
> -			tty_port_destroy(pf->gnss_tty_port[i]);
> -			kfree(pf->gnss_tty_port[i]);
> -		}
> -		kfree(ttydrv_name);
> -		tty_driver_kref_put(pf->ice_gnss_tty_driver);
> -
> -		return NULL;
> +		goto err_out;

FTR I think that depending on i == ICE_GNSS_TTY_MINOR_DEVICES
here is fragile. I can merge as is, since the code is technically
correct, but what you should have done is crate a new label, say
err_unreg_all_ports, do:

	goto err_unreg_all_ports;

>  	}
>  
>  	for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++)
>  		dev_info(dev, "%s%d registered\n", ttydrv_name, i);
>  
>  	return tty_driver;
> +

And here add:

err_unreg_all_ports:
	i = ICE_GNSS_TTY_MINOR_DEVICES;
> +err_out:
> +	while (i--) {
> +		tty_port_destroy(pf->gnss_tty_port[i]);
> +		kfree(pf->gnss_tty_port[i]);
> +	}
> +	kfree(ttydrv_name);
> +	tty_driver_kref_put(pf->ice_gnss_tty_driver);
> +
> +	return NULL;