Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/pci Avoid undefined behavior in pciiide macros



details:   https://anonhg.NetBSD.org/src/rev/970a2cdd239d
branches:  trunk
changeset: 324448:970a2cdd239d
user:      kamil <kamil%NetBSD.org@localhost>
date:      Wed Jul 04 03:00:46 2018 +0000

description:
Avoid undefined behavior in pciiide macros

Cast the 'bytes' argument in PIIX_IDETIM_SET() and PIIX_IDETIM_CLEAR()
to unsigned int. This prevents UB because of shifting the bits and changing
the bit of signedness.

sys/dev/pci/piixide.c:714:11, left shift of 65535 by 16 places cannot be represented in type 'int'
sys/dev/pci/piixide.c:720:11, left shift of 32768 by 16 places cannot be represented in type 'int'

Detected with Kernel Undefined Behavior Sanitizer.

Reported by <Harry Pantazis>

diffstat:

 sys/dev/pci/pciide_piix_reg.h |  6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diffs (21 lines):

diff -r 017f44601d55 -r 970a2cdd239d sys/dev/pci/pciide_piix_reg.h
--- a/sys/dev/pci/pciide_piix_reg.h     Wed Jul 04 02:19:02 2018 +0000
+++ b/sys/dev/pci/pciide_piix_reg.h     Wed Jul 04 03:00:46 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pciide_piix_reg.h,v 1.14 2009/10/19 18:41:16 bouyer Exp $      */
+/*     $NetBSD: pciide_piix_reg.h,v 1.15 2018/07/04 03:00:46 kamil Exp $       */
 
 /*
  * Copyright (c) 1998 Manuel Bouyer.
@@ -49,9 +49,9 @@
 #define PIIX_IDETIM 0x40
 #define PIIX_IDETIM_READ(x, channel) (((x) >> (16 * (channel))) & 0x0000FFFF)
 #define PIIX_IDETIM_SET(x, bytes, channel) \
-       ((x) | ((bytes) << (16 * (channel))))
+       ((x) | ((unsigned int)(bytes) << (16 * (channel))))
 #define PIIX_IDETIM_CLEAR(x, bytes, channel) \
-       ((x) & ~((bytes) << (16 * (channel))))
+       ((x) & ~((unsigned int)(bytes) << (16 * (channel))))
 
 #define PIIX_IDETIM_IDE                0x8000 /* PIIX decode IDE registers */
 #define PIIX_IDETIM_SITRE      0x4000 /* slaves IDE timing registers



Home | Main Index | Thread Index | Old Index