keyspan_pda.c use of keyspan_pda_get_modem_info
Subsystems (2)
with gcc 4.3.0 i see these warnings:
drivers/usb/serial/keyspan_pda.c: In function 'keyspan_pda_tiocmget':drivers/usb/serial/keyspan_pda.c:444: warning: 'status' may be used uninitialized in this function drivers/usb/serial/keyspan_pda.c: In function 'keyspan_pda_tiocmset':drivers/usb/serial/keyspan_pda.c:465: warning: 'status' may be used uninitialized in this function
In these two call sites the callers bail out if keyspan_pda_get_modem_info return value is < 0
e.g.
static int keyspan_pda_tiocmget(struct usb_serial_port *port, struct file *file) {
...
unsigned char status; ... rc = keyspan_pda_get_modem_info(serial, &status); if (rc < 0) return rc; value = ((status & (1<<7)) ? TIOCM_DTR : 0) | ... return value;
However, keyspan_pda_get_modem_info sets status only for rc > 0 so it may indeed be used uninitialized in case keyspan_pda_get_modem_info returns 0.
static int keyspan_pda_get_modem_info(struct usb_serial *serial,
unsigned char *value)
{
int rc;
unsigned char data;
rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
3, /* get pins */
USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
0, 0, &data, 1, 2000);
if (rc > 0)
*value = data;
return rc;
}In the usb_control_msg/usb_internal_control_msg/usb_start_wait_urb path, if usb_submit_urb
That said, I'm not sure if that can happen at all but regardless, it seems like a good idea to handle this case.
Benny
Quoted 2 lines
In these two call sites the callers bail out if
keyspan_pda_get_modem_inforeturn value is < 0
I don't think that is what the compiler is warning about.
We error on rc < 0
We use the returned status on rc >= 0
We set the returned status on rc > 0
So the rc = 0 case is broken and gcc seems to be correct about that
Quoted 11 lines · Alan Cox
Quoted 2 lines
In these two call sites the callers bail out if
keyspan_pda_get_modem_inforeturn value is < 0I don't think that is what the compiler is warning about.
We error on rc < 0
We use the returned status on rc >= 0We set the returned status on rc > 0
So the rc = 0 case is broken and gcc seems to be correct about that
Yes, that is correct, fortunatly that function can never return rc = 0, so this will not happen in real life.
But there is really no way the compiler can ever figure that out, so I don't blame it for complaining.
If you want to make a simple patch to make the compiler happy and be quiet about the warning, I'll take it.
thanks,
greg k-h
Quoted 12 lines · Greg KH
Quoted 10 lines · Alan Cox
Quoted 2 lines
In these two call sites the callers bail out if
keyspan_pda_get_modem_inforeturn value is < 0I don't think that is what the compiler is warning about.
We error on rc < 0
We use the returned status on rc >= 0We set the returned status on rc > 0
So the rc = 0 case is broken and gcc seems to be correct about that
Quoted 8 lines
Yes, that is correct, fortunatly that function can never return rc = 0, so this will not happen in real life.
But there is really no way the compiler can ever figure that out, so I don't blame it for complaining.
If you want to make a simple patch to make the compiler happy and be quiet about the warning, I'll take it.
Cool. I'll send a patch initializing status to 0. Now, what about the rc == 0 case?
Does it warrant a BUG_ON() if it should ever happen?
Benny
Quoted 4 lines
thanks,
greg k-h
PATCH 1/1 usb: fix uninitialized variables in keyspan_pda
The compiler (gcc 4.3.0) warns that status might be used uninitialized in keyspan_pda_tiocmget and keyspan_pda_tiocmset.
It is technically correct and therefore this patch initializes status to 0 in both cases.
Note that keyspan_pda_get_modem_info sets *value only when usb_control_msg returns rc > 0, otherwise it must
return an error rc < 0, and it must never return rc == 0, hence a WARN_ON(rc == 0) was added in case usb_control_msg ever return 0.
Both its callers bail out for rc < 0 and use the value of "status" in the rc >= 0 case. If the WARN_ON is ever seen, status will be set to the initial value of 0.
--- drivers/usb/serial/keyspan_pda.c | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-)
drivers/usb/serial/keyspan_pda.c +3 -2
@@ -420,6 +420,7 @@ static int keyspan_pda_get_modem_info(struct usb_serial *serial,3 ,/* get pins */ USB_TYPE_VENDOR |USB_RECIP_INTERFACE |USB_DIR_IN , 0, 0, &data, 1, 2000); +WARN_ON (rc ==0 );if (rc >0 ) *value = data;return rc;
@@ -441,7 +442,7 @@ static int keyspan_pda_tiocmget(struct usb_serial_port *port, struct file *file) {struct usb_serial *serial = port->serial;int rc; -unsigned char status; +unsigned char status =0 ;int value; rc =keyspan_pda_get_modem_info (serial, &status);
@@ -462,7 +463,7 @@ static int keyspan_pda_tiocmset(struct usb_serial_port *port, struct file *file, {struct usb_serial *serial = port->serial;int rc; -unsigned char status; +unsigned char status =0 ; rc =keyspan_pda_get_modem_info (serial, &status);if (rc <0 )
Signature · 1 line
1.5.6.GIT
Re: [PATCH] usb: fix uninitialized variables in keyspan_pda
Quoted 10 lines · On Fri, 27 Jun 2008 10:58:54 +0300 Benny Halevy <[email protected]> wrote:
The compiler (gcc 4.3.0) warns that status might be used uninitialized in
keyspan_pda_tiocmgetandkeyspan_pda_tiocmset.It is technically correct and therefore this patch initializes status to 0 in both cases.
Note that
keyspan_pda_get_modem_infosets *value only whenusb_control_msgreturns rc > 0, otherwise it must
return an error rc < 0, and it must never return rc == 0, hence aWARN_ON(rc == 0)was added in caseusb_control_msgever return 0.
That seems like overkill - and by forcing status to 0 you will hide future gcc error catches. Far better to change the two callers to test for > 0 not >= 0 surely ?
Alan
Re: [PATCH] usb: fix uninitialized variables in keyspan_pda
Quoted 9 lines · Alan Cox
Quoted 6 lines · On Fri, 27 Jun 2008 10:58:54 +0300 Benny Halevy <[email protected]> wrote:
The compiler (gcc 4.3.0) warns that status might be used uninitialized in
keyspan_pda_tiocmgetandkeyspan_pda_tiocmset.It is technically correct and therefore this patch initializes status to 0 in both cases.
Quoted 8 lines
Quoted 4 lines
Note that
keyspan_pda_get_modem_infosets *value only whenusb_control_msgreturns rc > 0, otherwise it must
return an error rc < 0, and it must never return rc == 0, hence aWARN_ON(rc == 0)was added in caseusb_control_msgever return 0.That seems like overkill - and by forcing status to 0 you will hide future gcc error catches. Far better to change the two callers to test for > 0 not >= 0 surely ?
Well, since usb_control_msg is not supposed to ever return 0 I'm really not sure what to do if it does. I'll gladly defer that decision to Greg...
The following patch makes gcc happy as you said and if it's correct I, too, like it much better.
Benny
drivers/usb/serial/keyspan_pda.c +1 -1
@@ -420,7 +420,7 @@ static int keyspan_pda_get_modem_info(struct usb_serial *serial,3 ,/* get pins */ USB_TYPE_VENDOR |USB_RECIP_INTERFACE |USB_DIR_IN , 0, 0, &data, 1, 2000); -if (rc >0 ) +if (rc >=0 ) *value = data;return rc; }
Quoted 2 lines
Alan
Re: [PATCH] usb: fix uninitialized variables in keyspan_pda
Quoted 15 lines · Benny Halevy
Quoted 14 lines · Alan Cox
Quoted 10 lines · On Fri, 27 Jun 2008 10:58:54 +0300 Benny Halevy <[email protected]> wrote:
The compiler (gcc 4.3.0) warns that status might be used uninitialized in
keyspan_pda_tiocmgetandkeyspan_pda_tiocmset.It is technically correct and therefore this patch initializes status to 0 in both cases.
Note that
keyspan_pda_get_modem_infosets *value only whenusb_control_msgreturns rc > 0, otherwise it must
return an error rc < 0, and it must never return rc == 0, hence aWARN_ON(rc == 0)was added in caseusb_control_msgever return 0.
Quoted 7 lines
Quoted 3 lines
That seems like overkill - and by forcing status to 0 you will hide future gcc error catches. Far better to change the two callers to test for > 0 not >= 0 surely ?
Well, since
usb_control_msgis not supposed to ever return 0 I'm really not sure what to do if it does. I'll gladly defer that decision to Greg...
It's not supposed to return 0 unless you pass in 0 as the length to be transferred (not likely, but possible.) So I'm referring to the case in the keyspan driver where the return value can not be zero, sorry for any confusion.
Quoted 2 lines
The following patch makes gcc happy as you said and if it's correct I, too, like it much better.
I like it better too, make it more obvious.
thanks,
greg k-h