drivers/usb: refactor min(), max() with min_t(), max_t()
Subsystems (7)
PATCH 1/1 drivers/usb: refactor min(), max() with min_t(), max_t()
Scanned the current drivers/usb code with max\(.*\( and min\(.*\( regexp queries to find casting inside of min() and max() which may lead to subtle bugs or even security vulnerabilities, especially if negative values are involved.
Let's refactor to min_t() and max_t() specifying the data type to ensure it's applicable for the both compareable arguments. It should address potential type promotion issues and improves type safety.
--- drivers/usb/core/config.c | 2 +- drivers/usb/gadget/composite.c | 12 ++++++------ drivers/usb/gadget/configfs.c | 2 +- drivers/usb/gadget/function/f_fs.c | 6 +++--- drivers/usb/gadget/function/f_mass_storage.c | 8 ++++---- drivers/usb/gadget/function/uvc_video.c | 4 ++-- drivers/usb/gadget/legacy/raw_gadget.c | 4 ++-- drivers/usb/gadget/udc/omap_udc.c | 4 ++-- drivers/usb/gadget/usbstring.c | 2 +- drivers/usb/host/ehci-hcd.c | 2 +- drivers/usb/host/oxu210hp-hcd.c | 4 ++-- drivers/usb/host/r8a66597-hcd.c | 2 +- drivers/usb/misc/usbtest.c | 3 ++- drivers/usb/mon/mon_bin.c | 2 +- drivers/usb/musb/musb_core.c | 2 +- drivers/usb/musb/musb_gadget_ep0.c | 2 +- drivers/usb/musb/musb_host.c | 5 ++--- drivers/usb/serial/io_edgeport.c | 2 +- drivers/usb/serial/sierra.c | 2 +- drivers/usb/storage/sddr09.c | 4 ++-- drivers/usb/storage/sddr55.c | 8 ++++---- 21 files changed, 41 insertions(+), 41 deletions(-)
drivers/usb/core/config.c +1 -1
@@ -924,7 +924,7 @@ int usb_get_configuration(struct usb_device *dev) result = -EINVAL ; goto err; } - length =max ((int )le16_to_cpu (desc->wTotalLength), + length =max_t (int,le16_to_cpu (desc->wTotalLength),USB_DT_CONFIG_SIZE );/* Now that we know the length, get the whole thing */
drivers/usb/gadget/composite.c +6 -6
@@ -1844,7 +1844,7 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) cdev->desc.bcdUSB =cpu_to_le16 (0x0200 ); } - value =min (w_length, (u16 )sizeof cdev->desc); + value =min_t (u16, w_length,sizeof (cdev->desc));memcpy (req->buf, &cdev->desc, value);break ;case USB_DT_DEVICE_QUALIFIER :
@@ -1863,19 +1863,19 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)case USB_DT_CONFIG : value =config_desc (cdev, w_value);if (value >=0 ) - value =min (w_length, (u16 ) value); + value =min_t (u16, w_length, value);break ;case USB_DT_STRING : value =get_string (cdev, req->buf, w_index, w_value &0xff );if (value >=0 ) - value =min (w_length, (u16 ) value); + value =min_t (u16, w_length, value);break ;case USB_DT_BOS :if (gadget_is_superspeed (gadget) || gadget->lpm_capable || cdev->use_webusb) { value =bos_desc (cdev); - value =min (w_length, (u16 ) value); + value =min_t (u16, w_length, value); }break ;case USB_DT_OTG :
@@ -1930,7 +1930,7 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) *(u8 *)req->buf = cdev->config->bConfigurationValue; else *(u8 *)req->buf =0 ; - value =min (w_length, (u16 )1 ); + value =min_t (u16, w_length,1 );break ;/* function drivers must handle get/set altsetting */
@@ -1976,7 +1976,7 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)if (value <0 )break ; *((u8 *)req->buf) = value; - value =min (w_length, (u16 )1 ); + value =min_t (u16, w_length,1 );break ;case USB_REQ_GET_STATUS : if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
drivers/usb/gadget/configfs.c +1 -1
@@ -1184,7 +1184,7 @@ static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,struct gadget_info *gi =os_desc_item_to_gadget_info (item);int res, l; - l =min ((int )len,OS_STRING_QW_SIGN_LEN >>1 ); + l =min_t (int, len,OS_STRING_QW_SIGN_LEN >>1 );if (page[l -1 ] =='\n' ) --l;
drivers/usb/gadget/function/f_fs.c +3 -3
@@ -456,7 +456,7 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf, }/* FFS_SETUP_PENDING and not stall */ - len =min (len, (size_t )le16_to_cpu (ffs->ev.setup.wLength)); + len =min_t (size_t, len,le16_to_cpu (ffs->ev.setup.wLength));spin_unlock_irq (&ffs->ev.waitq.lock);
@@ -590,7 +590,7 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,/* unlocks spinlock */ return __ffs_ep0_read_events (ffs, buf, -min (n, (size_t )ffs->ev.count)); +min_t (size_t, n, ffs->ev.count));case FFS_SETUP_PENDING :if (ffs->ev.setup.bRequestType &USB_DIR_IN ) {
@@ -599,7 +599,7 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf, goto done_mutex; } - len =min (len, (size_t )le16_to_cpu (ffs->ev.setup.wLength)); + len =min_t (size_t, len,le16_to_cpu (ffs->ev.setup.wLength));spin_unlock_irq (&ffs->ev.waitq.lock);
drivers/usb/gadget/function/f_mass_storage.c +4 -4
@@ -500,7 +500,7 @@ static int fsg_setup(struct usb_function *f, *(u8 *)req->buf =_fsg_common_get_max_lun (fsg->common);/* Respond with data/status */ - req->length =min ((u16 )1 , w_length); + req->length =min_t (u16,1 , w_length);return ep0_queue (fsg->common); }
@@ -655,7 +655,7 @@ static int do_read(struct fsg_common *common)* And don't try to read past the end of the file. */ amount =min (amount_left,FSG_BUFLEN ); - amount =min ((loff_t )amount, + amount =min_t (loff_t, amount, curlun->file_length - file_offset);/* Wait for the next buffer to become available */
@@ -1005,7 +1005,7 @@ static int do_verify(struct fsg_common *common)* And don't try to read past the end of the file. */ amount =min (amount_left,FSG_BUFLEN ); - amount =min ((loff_t )amount, + amount =min_t (loff_t, amount, curlun->file_length - file_offset);if (amount ==0 ) { curlun->sense_data =
@@ -2167,7 +2167,7 @@ static int do_scsi_command(struct fsg_common *common)if (reply == -EINVAL ) reply =0 ;/* Error reply length */ if (reply >=0 && common->data_dir ==DATA_DIR_TO_HOST ) { - reply =min ((u32 )reply, common->data_size_from_cmnd); + reply =min_t (u32, reply, common->data_size_from_cmnd); bh->inreq->length = reply; bh->state =BUF_STATE_FULL ; common->residue -= reply;
drivers/usb/gadget/function/uvc_video.c +2 -2
@@ -78,7 +78,7 @@ uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,/* Copy video data to the USB buffer. */ mem = buf->mem + queue->buf_used; - nbytes =min ((unsigned int )len, buf->bytesused - queue->buf_used); + nbytes = min_t(unsigned int, len, buf->bytesused - queue->buf_used);memcpy (data, mem, nbytes); queue->buf_used += nbytes;
@@ -104,7 +104,7 @@ uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video, }/* Process video data. */ - len =min ((int )(video->max_payload_size - video->payload_size), len); + len =min_t (int, video->max_payload_size - video->payload_size, len); ret =uvc_video_encode_data (video, buf, mem, len); video->payload_size += ret;
drivers/usb/gadget/legacy/raw_gadget.c +2 -2
@@ -782,7 +782,7 @@ static int raw_ioctl_ep0_read(struct raw_dev *dev, unsigned long value)if (ret <0 ) goto free; - length =min (io.length, (unsigned int )ret); + length = min_t(unsigned int, io.length, ret);if (copy_to_user ((void __user *)(value +sizeof (io)), data, length)) ret = -EFAULT ;else
@@ -1168,7 +1168,7 @@ static int raw_ioctl_ep_read(struct raw_dev *dev, unsigned long value)if (ret <0 ) goto free; - length =min (io.length, (unsigned int )ret); + length = min_t(unsigned int, io.length, ret);if (copy_to_user ((void __user *)(value +sizeof (io)), data, length)) ret = -EFAULT ;else
drivers/usb/gadget/udc/omap_udc.c +2 -2
@@ -576,13 +576,13 @@ static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)static void next_out_dma (struct omap_ep *ep,struct omap_req *req) { -unsigned packets = req->req.length - req->req.actual; +unsigned int packets = req->req.length - req->req.actual;int dma_trigger =0 ;u16 w;/* set up this DMA transfer, enable the fifo, start */ packets /= ep->ep.maxpacket; - packets =min (packets, (unsigned )UDC_RXN_TC +1 ); + packets = min_t(unsigned int, packets, UDC_RXN_TC + 1); req->dma_bytes = packets * ep->ep.maxpacket; omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16, ep->ep.maxpacket >> 1, packets,
drivers/usb/gadget/usbstring.c +1 -1
@@ -55,7 +55,7 @@ usb_gadget_get_string (const struct usb_gadget_strings *table, int id, u8 *buf)return -EINVAL ;/* string descriptors have length, tag, then UTF16-LE text */ - len =min ((size_t )USB_MAX_STRING_LEN ,strlen (s->s)); + len =min_t (size_t,USB_MAX_STRING_LEN ,strlen (s->s)); len =utf8s_to_utf16s (s->s, len,UTF16_LITTLE_ENDIAN , (wchar_t *) &buf[2 ],USB_MAX_STRING_LEN );if (len <0 )
drivers/usb/host/ehci-hcd.c +1 -1
@@ -547,7 +547,7 @@ static int ehci_init(struct usb_hcd *hcd)* make problems: throughput reduction (!), data errors... */ if (park) { - park =min (park, (unsigned )3 ); + park = min_t(unsigned int, park, 3); temp |=CMD_PARK ; temp |= park <<8 ; }
drivers/usb/host/oxu210hp-hcd.c +2 -2
@@ -902,7 +902,7 @@ static int oxu_buf_alloc(struct oxu_hcd *oxu, struct ehci_qtd *qtd, int len)/* Find a suitable available data buffer */ for (i =0 ; i <BUFFER_NUM ; - i +=max (a_blocks, (int )oxu->db_used[i])) { + i +=max_t (int, a_blocks, oxu->db_used[i])) {/* Check all the required blocks are available */ for (j =0 ; j < a_blocks; j++)
@@ -3040,7 +3040,7 @@ static int oxu_hcd_init(struct usb_hcd *hcd)* make problems: throughput reduction (!), data errors... */ if (park) { - park =min (park, (unsigned )3 ); + park = min_t(unsigned int, park, 3); temp |=CMD_PARK ; temp |= park <<8 ; }
drivers/usb/host/r8a66597-hcd.c +1 -1
@@ -1336,7 +1336,7 @@ static void packet_read(struct r8a66597 *r8a66597, u16 pipenum) buf = (void *)urb->transfer_buffer + urb->actual_length; urb_len = urb->transfer_buffer_length - urb->actual_length; } - bufsize =min (urb_len, (int ) td->maxpacket); + bufsize =min_t (int, urb_len, td->maxpacket);if (rcv_len <= bufsize) { size = rcv_len; }else {
drivers/usb/misc/usbtest.c +2 -1
@@ -2021,7 +2021,8 @@ static struct urb *iso_alloc_urb(for (i =0 ; i < packets; i++) {/* here, only the last packet will be short */ - urb->iso_frame_desc[i].length =min ((unsigned ) bytes, maxp); + urb->iso_frame_desc[i].length = min_t(unsigned int, + bytes, maxp); bytes -= urb->iso_frame_desc[i].length; urb->iso_frame_desc[i].offset = maxp * i;
drivers/usb/mon/mon_bin.c +1 -1
@@ -823,7 +823,7 @@ static ssize_t mon_bin_read(struct file *file, char __user *buf, ep =MON_OFF2HDR (rp, rp->b_out);if (rp->b_read < hdrbytes) { - step_len =min (nbytes, (size_t )(hdrbytes - rp->b_read)); + step_len =min_t (size_t, nbytes, hdrbytes - rp->b_read); ptr = ((char *)ep) + rp->b_read;if (step_len &©_to_user (buf, ptr, step_len)) {mutex_unlock (&rp->fetch_lock);
drivers/usb/musb/musb_core.c +1 -1
@@ -1387,7 +1387,7 @@ fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,/* expect hw_ep has already been zero-initialized */ - size =ffs (max (maxpacket, (u16 )8 )) -1 ; + size =ffs (max_t (u16, maxpacket,8 )) -1 ; maxpacket =1 << size; c_size = size -3 ;
drivers/usb/musb/musb_gadget_ep0.c +1 -1
@@ -533,7 +533,7 @@ static void ep0_txstate(struct musb *musb)/* load the data */ fifo_src = (u8 *) request->buf + request->actual; - fifo_count =min ((unsigned )MUSB_EP0_FIFOSIZE , + fifo_count =min_t (unsigned,MUSB_EP0_FIFOSIZE , request->length - request->actual);musb_write_fifo (&musb->endpoints[0 ], fifo_count, fifo_src); request->actual += fifo_count;
drivers/usb/musb/musb_host.c +2 -3
@@ -798,10 +798,9 @@ static void musb_ep_program(struct musb *musb, u8 epnum, }if (can_bulk_split (musb, qh->type)) - load_count =min ((u32 ) hw_ep->max_packet_sz_tx, - len); + load_count =min_t (u32, hw_ep->max_packet_sz_tx, len);else - load_count =min ((u32 ) packet_sz, len); + load_count =min_t (u32, packet_sz, len);if (dma_channel &&musb_tx_dma_program (dma_controller, hw_ep, qh, urb, offset, len))
drivers/usb/serial/io_edgeport.c +1 -1
@@ -1129,7 +1129,7 @@ static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,spin_lock_irqsave (&edge_port->ep_lock, flags);/* calculate number of bytes to put in fifo */ - copySize =min ((unsigned int )count, + copySize = min_t(unsigned int, count, (edge_port->txCredits - fifo->count)); dev_dbg(&port->dev, "%s of %d byte(s) Fifo room %d -- will copy %d bytes\n",
drivers/usb/serial/sierra.c +1 -1
@@ -421,7 +421,7 @@ static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,unsigned long flags;unsigned char *buffer;struct urb *urb; -size_t writesize =min ((size_t )count, (size_t )MAX_TRANSFER ); +size_t writesize =min_t (size_t, count,MAX_TRANSFER );int retval =0 ;/* verify that we actually have some data to write */
drivers/usb/storage/sddr09.c +2 -2
@@ -752,7 +752,7 @@ sddr09_read_data(struct us_data *us,// a bounce buffer and move the data a piece at a time between the // bounce buffer and the actual transfer buffer. - len =min (sectors, (unsigned int ) info->blocksize) * info->pagesize; + len = min_t(unsigned int, sectors, info->blocksize) * info->pagesize; buffer =kmalloc (len,GFP_NOIO );if (!buffer)return -ENOMEM ;
@@ -997,7 +997,7 @@ sddr09_write_data(struct us_data *us,* at a time between the bounce buffer and the actual transfer buffer. */ - len =min (sectors, (unsigned int ) info->blocksize) * info->pagesize; + len = min_t(unsigned int, sectors, info->blocksize) * info->pagesize; buffer =kmalloc (len,GFP_NOIO );if (!buffer) {kfree (blockbuffer);
drivers/usb/storage/sddr55.c +4 -4
@@ -206,7 +206,7 @@ static int sddr55_read_data(struct us_data *us,// a bounce buffer and move the data a piece at a time between the // bounce buffer and the actual transfer buffer. - len =min ((unsigned int ) sectors, (unsigned int ) info->blocksize >> + len = min_t(unsigned int, sectors, info->blocksize >> info->smallpageshift) *PAGESIZE ; buffer =kmalloc (len,GFP_NOIO );if (buffer ==NULL )
@@ -224,7 +224,7 @@ static int sddr55_read_data(struct us_data *us,// Read as many sectors as possible in this block - pages =min ((unsigned int ) sectors << info->smallpageshift, + pages = min_t(unsigned int, sectors << info->smallpageshift, info->blocksize - page); len = pages << info->pageshift;
@@ -333,7 +333,7 @@ static int sddr55_write_data(struct us_data *us,// a bounce buffer and move the data a piece at a time between the // bounce buffer and the actual transfer buffer. - len =min ((unsigned int ) sectors, (unsigned int ) info->blocksize >> + len = min_t(unsigned int, sectors, info->blocksize >> info->smallpageshift) *PAGESIZE ; buffer =kmalloc (len,GFP_NOIO );if (buffer ==NULL )
@@ -351,7 +351,7 @@ static int sddr55_write_data(struct us_data *us,// Write as many sectors as possible in this block - pages =min ((unsigned int ) sectors << info->smallpageshift, + pages = min_t(unsigned int, sectors << info->smallpageshift, info->blocksize - page); len = pages << info->pageshift;
Signature · 1 line
2.34.1
Greg KH
maintainerQuoted 25 lines · Sabyrzhan Tasbolatov
Scanned the current
drivers/usbcode withmax\(.*\(andmin\(.*\(regexp queries to find casting inside ofmin()andmax()which may lead to subtle bugs or even security vulnerabilities, especially if negative values are involved.Let's refactor to
min_t()andmax_t()specifying the data type to ensure it's applicable for the both compareable arguments. It should address potential type promotion issues and improves type safety.Signed-off-by Sabyrzhan Tasbolatov <[email protected]>--- drivers/usb/core/config.c | 2 +- drivers/usb/gadget/composite.c | 12 ++++++------ drivers/usb/gadget/configfs.c | 2 +- drivers/usb/gadget/function/f_fs.c | 6 +++--- drivers/usb/gadget/function/f_mass_storage.c | 8 ++++---- drivers/usb/gadget/function/uvc_video.c | 4 ++-- drivers/usb/gadget/legacy/raw_gadget.c | 4 ++-- drivers/usb/gadget/udc/omap_udc.c | 4 ++-- drivers/usb/gadget/usbstring.c | 2 +- drivers/usb/host/ehci-hcd.c | 2 +- drivers/usb/host/oxu210hp-hcd.c | 4 ++-- drivers/usb/host/r8a66597-hcd.c | 2 +- drivers/usb/misc/usbtest.c | 3 ++- drivers/usb/mon/mon_bin.c | 2 +-
Quoted 8 lines
drivers/usb/musb/musb_core.c | 2 +- drivers/usb/musb/musb_gadget_ep0.c | 2 +- drivers/usb/musb/musb_host.c | 5 ++--- drivers/usb/serial/io_edgeport.c | 2 +- drivers/usb/serial/sierra.c | 2 +- drivers/usb/storage/sddr09.c | 4 ++-- drivers/usb/storage/sddr55.c | 8 ++++---- 21 files changed, 41 insertions(+), 41 deletions(-)
Can you break these up to at least "one per drivers/usb/*" subdirectory to make it easier to review and apply?
thanks,
greg k-h
v2 cover drivers/usb: refactor min/max with min_t/max_t
This patch series improves type safety in the drivers/usb/*
by using min_t() and max_t() instead of min(), max()
with the casting inside. It should address potential type promotion issues.
Scanned the current drivers/usb code with max\(.*\( and min\(.*\( regexp queries to find casting inside of min() and max() which may lead to subtle bugs or even security vulnerabilities, especially if negative values are involved.
Let's refactor to min_t() and max_t() specifying the data type to ensure it's applicable for the both compareable arguments.
Changes v1 -> v2:
- split a single patch into a patch series per each drivers/usb/* subdirectory (Greg).
Sabyrzhan Tasbolatov (8):
drivers/usb/gadget: refactor min with min_t drivers/usb/core: refactor max with max_t drivers/usb/host: refactor min/max with min_t/max_t drivers/usb/misc: refactor min with min_t drivers/usb/mon: refactor min with min_t drivers/usb/musb: refactor min/max with min_t/max_t drivers/usb/serial: refactor min with min_t drivers/usb/storage: refactor min with min_t
drivers/usb/core/config.c | 2 +- drivers/usb/gadget/composite.c | 12 ++++++------ drivers/usb/gadget/configfs.c | 2 +- drivers/usb/gadget/function/f_fs.c | 6 +++--- drivers/usb/gadget/function/f_mass_storage.c | 8 ++++---- drivers/usb/gadget/function/uvc_video.c | 4 ++-- drivers/usb/gadget/legacy/raw_gadget.c | 4 ++-- drivers/usb/gadget/udc/omap_udc.c | 4 ++-- drivers/usb/gadget/usbstring.c | 2 +- drivers/usb/host/ehci-hcd.c | 2 +- drivers/usb/host/oxu210hp-hcd.c | 4 ++-- drivers/usb/host/r8a66597-hcd.c | 2 +- drivers/usb/misc/usbtest.c | 3 ++- drivers/usb/mon/mon_bin.c | 2 +- drivers/usb/musb/musb_core.c | 2 +- drivers/usb/musb/musb_gadget_ep0.c | 2 +- drivers/usb/musb/musb_host.c | 5 ++--- drivers/usb/serial/io_edgeport.c | 2 +- drivers/usb/serial/sierra.c | 2 +- drivers/usb/storage/sddr09.c | 4 ++-- drivers/usb/storage/sddr55.c | 8 ++++---- 21 files changed, 41 insertions(+), 41 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/8 drivers/usb/gadget: refactor min with min_t
Ensure type safety by using min_t() instead of casted min().
--- drivers/usb/gadget/composite.c | 12 ++++++------ drivers/usb/gadget/configfs.c | 2 +- drivers/usb/gadget/function/f_fs.c | 6 +++--- drivers/usb/gadget/function/f_mass_storage.c | 8 ++++---- drivers/usb/gadget/function/uvc_video.c | 4 ++-- drivers/usb/gadget/legacy/raw_gadget.c | 4 ++-- drivers/usb/gadget/udc/omap_udc.c | 4 ++-- drivers/usb/gadget/usbstring.c | 2 +- 8 files changed, 21 insertions(+), 21 deletions(-)
drivers/usb/gadget/composite.c +6 -6
@@ -1844,7 +1844,7 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) cdev->desc.bcdUSB =cpu_to_le16 (0x0200 ); } - value =min (w_length, (u16 )sizeof cdev->desc); + value =min_t (u16, w_length,sizeof (cdev->desc));memcpy (req->buf, &cdev->desc, value);break ;case USB_DT_DEVICE_QUALIFIER :
@@ -1863,19 +1863,19 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)case USB_DT_CONFIG : value =config_desc (cdev, w_value);if (value >=0 ) - value =min (w_length, (u16 ) value); + value =min_t (u16, w_length, value);break ;case USB_DT_STRING : value =get_string (cdev, req->buf, w_index, w_value &0xff );if (value >=0 ) - value =min (w_length, (u16 ) value); + value =min_t (u16, w_length, value);break ;case USB_DT_BOS :if (gadget_is_superspeed (gadget) || gadget->lpm_capable || cdev->use_webusb) { value =bos_desc (cdev); - value =min (w_length, (u16 ) value); + value =min_t (u16, w_length, value); }break ;case USB_DT_OTG :
@@ -1930,7 +1930,7 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) *(u8 *)req->buf = cdev->config->bConfigurationValue; else *(u8 *)req->buf =0 ; - value =min (w_length, (u16 )1 ); + value =min_t (u16, w_length,1 );break ;/* function drivers must handle get/set altsetting */
@@ -1976,7 +1976,7 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)if (value <0 )break ; *((u8 *)req->buf) = value; - value =min (w_length, (u16 )1 ); + value =min_t (u16, w_length,1 );break ;case USB_REQ_GET_STATUS : if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
drivers/usb/gadget/configfs.c +1 -1
@@ -1184,7 +1184,7 @@ static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,struct gadget_info *gi =os_desc_item_to_gadget_info (item);int res, l; - l =min ((int )len,OS_STRING_QW_SIGN_LEN >>1 ); + l =min_t (int, len,OS_STRING_QW_SIGN_LEN >>1 );if (page[l -1 ] =='\n' ) --l;
drivers/usb/gadget/function/f_fs.c +3 -3
@@ -456,7 +456,7 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf, }/* FFS_SETUP_PENDING and not stall */ - len =min (len, (size_t )le16_to_cpu (ffs->ev.setup.wLength)); + len =min_t (size_t, len,le16_to_cpu (ffs->ev.setup.wLength));spin_unlock_irq (&ffs->ev.waitq.lock);
@@ -590,7 +590,7 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf,/* unlocks spinlock */ return __ffs_ep0_read_events (ffs, buf, -min (n, (size_t )ffs->ev.count)); +min_t (size_t, n, ffs->ev.count));case FFS_SETUP_PENDING :if (ffs->ev.setup.bRequestType &USB_DIR_IN ) {
@@ -599,7 +599,7 @@ static ssize_t ffs_ep0_read(struct file *file, char __user *buf, goto done_mutex; } - len =min (len, (size_t )le16_to_cpu (ffs->ev.setup.wLength)); + len =min_t (size_t, len,le16_to_cpu (ffs->ev.setup.wLength));spin_unlock_irq (&ffs->ev.waitq.lock);
drivers/usb/gadget/function/f_mass_storage.c +4 -4
@@ -500,7 +500,7 @@ static int fsg_setup(struct usb_function *f, *(u8 *)req->buf =_fsg_common_get_max_lun (fsg->common);/* Respond with data/status */ - req->length =min ((u16 )1 , w_length); + req->length =min_t (u16,1 , w_length);return ep0_queue (fsg->common); }
@@ -655,7 +655,7 @@ static int do_read(struct fsg_common *common)* And don't try to read past the end of the file. */ amount =min (amount_left,FSG_BUFLEN ); - amount =min ((loff_t )amount, + amount =min_t (loff_t, amount, curlun->file_length - file_offset);/* Wait for the next buffer to become available */
@@ -1005,7 +1005,7 @@ static int do_verify(struct fsg_common *common)* And don't try to read past the end of the file. */ amount =min (amount_left,FSG_BUFLEN ); - amount =min ((loff_t )amount, + amount =min_t (loff_t, amount, curlun->file_length - file_offset);if (amount ==0 ) { curlun->sense_data =
@@ -2167,7 +2167,7 @@ static int do_scsi_command(struct fsg_common *common)if (reply == -EINVAL ) reply =0 ;/* Error reply length */ if (reply >=0 && common->data_dir ==DATA_DIR_TO_HOST ) { - reply =min ((u32 )reply, common->data_size_from_cmnd); + reply =min_t (u32, reply, common->data_size_from_cmnd); bh->inreq->length = reply; bh->state =BUF_STATE_FULL ; common->residue -= reply;
drivers/usb/gadget/function/uvc_video.c +2 -2
@@ -78,7 +78,7 @@ uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,/* Copy video data to the USB buffer. */ mem = buf->mem + queue->buf_used; - nbytes =min ((unsigned int )len, buf->bytesused - queue->buf_used); + nbytes = min_t(unsigned int, len, buf->bytesused - queue->buf_used);memcpy (data, mem, nbytes); queue->buf_used += nbytes;
@@ -104,7 +104,7 @@ uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video, }/* Process video data. */ - len =min ((int )(video->max_payload_size - video->payload_size), len); + len =min_t (int, video->max_payload_size - video->payload_size, len); ret =uvc_video_encode_data (video, buf, mem, len); video->payload_size += ret;
drivers/usb/gadget/legacy/raw_gadget.c +2 -2
@@ -782,7 +782,7 @@ static int raw_ioctl_ep0_read(struct raw_dev *dev, unsigned long value)if (ret <0 ) goto free; - length =min (io.length, (unsigned int )ret); + length = min_t(unsigned int, io.length, ret);if (copy_to_user ((void __user *)(value +sizeof (io)), data, length)) ret = -EFAULT ;else
@@ -1168,7 +1168,7 @@ static int raw_ioctl_ep_read(struct raw_dev *dev, unsigned long value)if (ret <0 ) goto free; - length =min (io.length, (unsigned int )ret); + length = min_t(unsigned int, io.length, ret);if (copy_to_user ((void __user *)(value +sizeof (io)), data, length)) ret = -EFAULT ;else
drivers/usb/gadget/udc/omap_udc.c +2 -2
@@ -576,13 +576,13 @@ static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)static void next_out_dma (struct omap_ep *ep,struct omap_req *req) { -unsigned packets = req->req.length - req->req.actual; +unsigned int packets = req->req.length - req->req.actual;int dma_trigger =0 ;u16 w;/* set up this DMA transfer, enable the fifo, start */ packets /= ep->ep.maxpacket; - packets =min (packets, (unsigned )UDC_RXN_TC +1 ); + packets = min_t(unsigned int, packets, UDC_RXN_TC + 1); req->dma_bytes = packets * ep->ep.maxpacket; omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16, ep->ep.maxpacket >> 1, packets,
drivers/usb/gadget/usbstring.c +1 -1
@@ -55,7 +55,7 @@ usb_gadget_get_string (const struct usb_gadget_strings *table, int id, u8 *buf)return -EINVAL ;/* string descriptors have length, tag, then UTF16-LE text */ - len =min ((size_t )USB_MAX_STRING_LEN ,strlen (s->s)); + len =min_t (size_t,USB_MAX_STRING_LEN ,strlen (s->s)); len =utf8s_to_utf16s (s->s, len,UTF16_LITTLE_ENDIAN , (wchar_t *) &buf[2 ],USB_MAX_STRING_LEN );if (len <0 )
Signature · 1 line
2.34.1
PATCH v2 2/8 drivers/usb/core: refactor max with max_t
In reply to Sabyrzhan TasbolatovEnsure type safety by using max_t() instead of max().
--- drivers/usb/core/config.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/core/config.c +1 -1
@@ -924,7 +924,7 @@ int usb_get_configuration(struct usb_device *dev) result = -EINVAL ; goto err; } - length =max ((int )le16_to_cpu (desc->wTotalLength), + length =max_t (int,le16_to_cpu (desc->wTotalLength),USB_DT_CONFIG_SIZE );/* Now that we know the length, get the whole thing */
Signature · 1 line
2.34.1
PATCH v2 3/8 drivers/usb/host: refactor min/max with min_t/max_t
In reply to Sabyrzhan TasbolatovEnsure type safety by using min_t/max_t instead of casted min/max.
--- drivers/usb/host/ehci-hcd.c | 2 +- drivers/usb/host/oxu210hp-hcd.c | 4 ++-- drivers/usb/host/r8a66597-hcd.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-)
drivers/usb/host/ehci-hcd.c +1 -1
@@ -547,7 +547,7 @@ static int ehci_init(struct usb_hcd *hcd)* make problems: throughput reduction (!), data errors... */ if (park) { - park =min (park, (unsigned )3 ); + park = min_t(unsigned int, park, 3); temp |=CMD_PARK ; temp |= park <<8 ; }
drivers/usb/host/oxu210hp-hcd.c +2 -2
@@ -902,7 +902,7 @@ static int oxu_buf_alloc(struct oxu_hcd *oxu, struct ehci_qtd *qtd, int len)/* Find a suitable available data buffer */ for (i =0 ; i <BUFFER_NUM ; - i +=max (a_blocks, (int )oxu->db_used[i])) { + i +=max_t (int, a_blocks, oxu->db_used[i])) {/* Check all the required blocks are available */ for (j =0 ; j < a_blocks; j++)
@@ -3040,7 +3040,7 @@ static int oxu_hcd_init(struct usb_hcd *hcd)* make problems: throughput reduction (!), data errors... */ if (park) { - park =min (park, (unsigned )3 ); + park = min_t(unsigned int, park, 3); temp |=CMD_PARK ; temp |= park <<8 ; }
drivers/usb/host/r8a66597-hcd.c +1 -1
@@ -1336,7 +1336,7 @@ static void packet_read(struct r8a66597 *r8a66597, u16 pipenum) buf = (void *)urb->transfer_buffer + urb->actual_length; urb_len = urb->transfer_buffer_length - urb->actual_length; } - bufsize =min (urb_len, (int ) td->maxpacket); + bufsize =min_t (int, urb_len, td->maxpacket);if (rcv_len <= bufsize) { size = rcv_len; }else {
Signature · 1 line
2.34.1
PATCH v2 4/8 drivers/usb/misc: refactor min with min_t
In reply to Sabyrzhan TasbolatovEnsure type safety by using min_t() instead of min().
Also add the explicit unsigned int as scripts/checkpatch.pl warns about:
WARNING: Prefer 'unsigned int' to bare use of 'unsigned'
--- drivers/usb/misc/usbtest.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/misc/usbtest.c +2 -1
@@ -2021,7 +2021,8 @@ static struct urb *iso_alloc_urb(for (i =0 ; i < packets; i++) {/* here, only the last packet will be short */ - urb->iso_frame_desc[i].length =min ((unsigned ) bytes, maxp); + urb->iso_frame_desc[i].length = min_t(unsigned int, + bytes, maxp); bytes -= urb->iso_frame_desc[i].length; urb->iso_frame_desc[i].offset = maxp * i;
Signature · 1 line
2.34.1
PATCH v2 5/8 drivers/usb/mon: refactor min with min_t
In reply to Sabyrzhan TasbolatovEnsure type safety by using min_t() instead of casted min().
--- drivers/usb/mon/mon_bin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/mon/mon_bin.c +1 -1
@@ -823,7 +823,7 @@ static ssize_t mon_bin_read(struct file *file, char __user *buf, ep =MON_OFF2HDR (rp, rp->b_out);if (rp->b_read < hdrbytes) { - step_len =min (nbytes, (size_t )(hdrbytes - rp->b_read)); + step_len =min_t (size_t, nbytes, hdrbytes - rp->b_read); ptr = ((char *)ep) + rp->b_read;if (step_len &©_to_user (buf, ptr, step_len)) {mutex_unlock (&rp->fetch_lock);
Signature · 1 line
2.34.1
PATCH v2 6/8 drivers/usb/musb: refactor min/max with min_t/max_t
In reply to Sabyrzhan TasbolatovEnsure type safety by using min_t()/max_t() instead of casted min()/max().
--- drivers/usb/musb/musb_core.c | 2 +- drivers/usb/musb/musb_gadget_ep0.c | 2 +- drivers/usb/musb/musb_host.c | 5 ++--- 3 files changed, 4 insertions(+), 5 deletions(-)
drivers/usb/musb/musb_core.c +1 -1
@@ -1387,7 +1387,7 @@ fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,/* expect hw_ep has already been zero-initialized */ - size =ffs (max (maxpacket, (u16 )8 )) -1 ; + size =ffs (max_t (u16, maxpacket,8 )) -1 ; maxpacket =1 << size; c_size = size -3 ;
drivers/usb/musb/musb_gadget_ep0.c +1 -1
@@ -533,7 +533,7 @@ static void ep0_txstate(struct musb *musb)/* load the data */ fifo_src = (u8 *) request->buf + request->actual; - fifo_count =min ((unsigned )MUSB_EP0_FIFOSIZE , + fifo_count =min_t (unsigned,MUSB_EP0_FIFOSIZE , request->length - request->actual);musb_write_fifo (&musb->endpoints[0 ], fifo_count, fifo_src); request->actual += fifo_count;
drivers/usb/musb/musb_host.c +2 -3
@@ -798,10 +798,9 @@ static void musb_ep_program(struct musb *musb, u8 epnum, }if (can_bulk_split (musb, qh->type)) - load_count =min ((u32 ) hw_ep->max_packet_sz_tx, - len); + load_count =min_t (u32, hw_ep->max_packet_sz_tx, len);else - load_count =min ((u32 ) packet_sz, len); + load_count =min_t (u32, packet_sz, len);if (dma_channel &&musb_tx_dma_program (dma_controller, hw_ep, qh, urb, offset, len))
Signature · 1 line
2.34.1
PATCH v2 7/8 drivers/usb/serial: refactor min with min_t
In reply to Sabyrzhan TasbolatovEnsure type safety by using min_t() instead of casted min().
--- drivers/usb/serial/io_edgeport.c | 2 +- drivers/usb/serial/sierra.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/usb/serial/io_edgeport.c +1 -1
@@ -1129,7 +1129,7 @@ static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,spin_lock_irqsave (&edge_port->ep_lock, flags);/* calculate number of bytes to put in fifo */ - copySize =min ((unsigned int )count, + copySize = min_t(unsigned int, count, (edge_port->txCredits - fifo->count)); dev_dbg(&port->dev, "%s of %d byte(s) Fifo room %d -- will copy %d bytes\n",
drivers/usb/serial/sierra.c +1 -1
@@ -421,7 +421,7 @@ static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,unsigned long flags;unsigned char *buffer;struct urb *urb; -size_t writesize =min ((size_t )count, (size_t )MAX_TRANSFER ); +size_t writesize =min_t (size_t, count,MAX_TRANSFER );int retval =0 ;/* verify that we actually have some data to write */
Signature · 1 line
2.34.1
PATCH v2 8/8 drivers/usb/storage: refactor min with min_t
In reply to Sabyrzhan TasbolatovEnsure type safety by using min_t() instead of casted min().
--- drivers/usb/storage/sddr09.c | 4 ++-- drivers/usb/storage/sddr55.c | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/usb/storage/sddr09.c +2 -2
@@ -752,7 +752,7 @@ sddr09_read_data(struct us_data *us,// a bounce buffer and move the data a piece at a time between the // bounce buffer and the actual transfer buffer. - len =min (sectors, (unsigned int ) info->blocksize) * info->pagesize; + len = min_t(unsigned int, sectors, info->blocksize) * info->pagesize; buffer =kmalloc (len,GFP_NOIO );if (!buffer)return -ENOMEM ;
@@ -997,7 +997,7 @@ sddr09_write_data(struct us_data *us,* at a time between the bounce buffer and the actual transfer buffer. */ - len =min (sectors, (unsigned int ) info->blocksize) * info->pagesize; + len = min_t(unsigned int, sectors, info->blocksize) * info->pagesize; buffer =kmalloc (len,GFP_NOIO );if (!buffer) {kfree (blockbuffer);
drivers/usb/storage/sddr55.c +4 -4
@@ -206,7 +206,7 @@ static int sddr55_read_data(struct us_data *us,// a bounce buffer and move the data a piece at a time between the // bounce buffer and the actual transfer buffer. - len =min ((unsigned int ) sectors, (unsigned int ) info->blocksize >> + len = min_t(unsigned int, sectors, info->blocksize >> info->smallpageshift) *PAGESIZE ; buffer =kmalloc (len,GFP_NOIO );if (buffer ==NULL )
@@ -224,7 +224,7 @@ static int sddr55_read_data(struct us_data *us,// Read as many sectors as possible in this block - pages =min ((unsigned int ) sectors << info->smallpageshift, + pages = min_t(unsigned int, sectors << info->smallpageshift, info->blocksize - page); len = pages << info->pageshift;
@@ -333,7 +333,7 @@ static int sddr55_write_data(struct us_data *us,// a bounce buffer and move the data a piece at a time between the // bounce buffer and the actual transfer buffer. - len =min ((unsigned int ) sectors, (unsigned int ) info->blocksize >> + len = min_t(unsigned int, sectors, info->blocksize >> info->smallpageshift) *PAGESIZE ; buffer =kmalloc (len,GFP_NOIO );if (buffer ==NULL )
@@ -351,7 +351,7 @@ static int sddr55_write_data(struct us_data *us,// Write as many sectors as possible in this block - pages =min ((unsigned int ) sectors << info->smallpageshift, + pages = min_t(unsigned int, sectors << info->smallpageshift, info->blocksize - page); len = pages << info->pageshift;
Signature · 1 line
2.34.1
Re: [PATCH v2 0/8] drivers/usb: refactor min/max with min_t/max_t
In reply to Sabyrzhan TasbolatovTue, Nov 12, 2024 at 08:58:09PM +0500, Sabyrzhan Tasbolatov kirjoitti:
Quoted 11 lines
This patch series improves type safety in the
drivers/usb/*
by usingmin_t()andmax_t()instead ofmin(),max()
with the casting inside. It should address potential type promotion issues.Scanned the current
drivers/usbcode withmax\(.*\(andmin\(.*\(regexp queries to find casting inside ofmin()andmax()which may lead to subtle bugs or even security vulnerabilities, especially if negative values are involved.Let's refactor to
min_t()andmax_t()specifying the data type to ensure it's applicable for the both compareable arguments.
max_t() is okay to use, but min_t() is quite a beast. Please, reconsider the entire series and tell how had this been tested? I can't imagine how many subtle bugs it might have introduced.
min_t() explicitly casts to the given type and this is a huge problem for the cases when one of the parameter is of signed type. Besisdes that it diminishes the type checking done in the min().
As Linus told, the many cases when you want to have min_t() is actually clamp(). In some cases you wanted umin().
+Cc: David.
Signature · 2 lines
With Best Regards, Andy Shevchenko