Skip to content

Commit

Permalink
MdeModulePkg: UsbNetwork: fix Ethernet functional descriptor processing
Browse files Browse the repository at this point in the history
This patch fixes wrong condition because of UINT16 value to integer
promotion. NumberMcFilters is UINT16 value, so when bitwise shift operator
applied to small integer type, the operation is preceded by integral
promotion. This is described in MISRA-C:2004 guideline as Rule 10.5:
"If the bitwise operators ~ and << are applied to an operand of underlying
type unsigned char or unsigned short, the result shall be immediately cast
to the underlying type of the operand."

A simple fix for this issue would be the following:
  if ((UINT16)(UsbEthFunDescriptor.NumberMcFilters << 1) == 0)

But this patch proposes to use bitwise AND operation with a proper bit mask
rather than shifting to prevent similar mistakes in future.

Cc: Richard Ho <richardho@ami.com>
Cc: Rebecca Cran <rebecca@bsdio.com>
Signed-off-by: Mike Maslenkin <mike.maslenkin@gmail.com>
  • Loading branch information
ghbaccount authored and mergify[bot] committed Oct 16, 2023
1 parent 326b9e1 commit e079482
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ SetFilter (
}

Nic->UsbEth->UsbEthFunDescriptor (Nic->UsbEth, &UsbEthFunDescriptor);
if ((UsbEthFunDescriptor.NumberMcFilters << 1) == 0) {
if ((UsbEthFunDescriptor.NumberMcFilters & MAC_FILTERS_MASK) == 0) {
Nic->RxFilter |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST;
Nic->UsbEth->SetUsbEthPacketFilter (Nic->UsbEth, Nic->RxFilter);
} else {
Expand Down
2 changes: 1 addition & 1 deletion MdeModulePkg/Bus/Usb/UsbNetwork/UsbCdcEcm/UsbEcmFunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ SetUsbEthMcastFilter (
return Status;
}

if ((UsbEthFunDescriptor.NumberMcFilters << 1) == 0) {
if ((UsbEthFunDescriptor.NumberMcFilters & MAC_FILTERS_MASK) == 0) {
return EFI_UNSUPPORTED;
}

Expand Down
2 changes: 1 addition & 1 deletion MdeModulePkg/Bus/Usb/UsbNetwork/UsbCdcNcm/UsbNcmFunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ SetUsbEthMcastFilter (
return Status;
}

if ((UsbEthFunDescriptor.NumberMcFilters << 1) == 0) {
if ((UsbEthFunDescriptor.NumberMcFilters & MAC_FILTERS_MASK) == 0) {
return EFI_UNSUPPORTED;
}

Expand Down
4 changes: 2 additions & 2 deletions MdeModulePkg/Bus/Usb/UsbNetwork/UsbRndis/UsbRndisFunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ SetUsbRndisMcastFilter (
return Status;
}

if ((UsbEthFunDescriptor.NumberMcFilters << 1) == 0) {
if ((UsbEthFunDescriptor.NumberMcFilters & MAC_FILTERS_MASK) == 0) {
return EFI_UNSUPPORTED;
}

Expand Down Expand Up @@ -856,7 +856,7 @@ RndisUndiReceiveFilter (
}

Nic->UsbEth->UsbEthFunDescriptor (Nic->UsbEth, &UsbEthFunDescriptor);
if ((UsbEthFunDescriptor.NumberMcFilters << 1) == 0) {
if ((UsbEthFunDescriptor.NumberMcFilters & MAC_FILTERS_MASK) == 0) {
Nic->RxFilter |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST;
DEBUG ((DEBUG_INFO, "SetUsbEthPacketFilter Nic %lx Nic->UsbEth %lx ", Nic, Nic->UsbEth));
Nic->UsbEth->SetUsbEthPacketFilter (Nic->UsbEth, Nic->RxFilter);
Expand Down
2 changes: 2 additions & 0 deletions MdeModulePkg/Include/Protocol/UsbEthernetProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ typedef struct _EDKII_USB_ETHERNET_PROTOCOL EDKII_USB_ETHERNET_PROTOCOL;
#define NETWORK_CONNECTED 0x01
#define NETWORK_DISCONNECT 0x00

#define MAC_FILTERS_MASK 0x7FFF

// USB Header functional Descriptor
typedef struct {
UINT8 FunctionLength;
Expand Down

0 comments on commit e079482

Please sign in to comment.