Skip to content

Commit 4308cae

Browse files
committed
Check before narrowing type-conversions in BIOS keyboard
1 parent 1bfee8b commit 4308cae

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/ints/bios_keyboard.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#include "inout.h"
2828
#include "dos_inc.h"
2929

30+
#include "checks.h"
31+
CHECK_NARROWING();
32+
3033
static callback_number_t call_int16 = 0;
3134
static callback_number_t call_irq1 = 0;
3235
static callback_number_t call_irq6 = 0;
@@ -369,9 +372,10 @@ static Bitu IRQ1_Handler(void) {
369372
break;
370373
}
371374
if(flags1 &0x08) {
372-
uint8_t token = mem_readb(BIOS_KEYBOARD_TOKEN);
373-
token = token*10 + (uint8_t)(scan_to_scanascii[scancode].alt&0xff);
374-
mem_writeb(BIOS_KEYBOARD_TOKEN,token);
375+
const auto token = mem_readb(BIOS_KEYBOARD_TOKEN);
376+
const auto alt = get_key_codes_for(scancode).alt & 0xff;
377+
const auto combined = token * 10 + alt;
378+
mem_writeb(BIOS_KEYBOARD_TOKEN, check_cast<uint8_t>(combined));
375379
} else if (flags1 &0x04) {
376380
add_key(scan_to_scanascii[scancode].control);
377381
} else if( ((flags1 &0x3) != 0) ^ ((flags1 &0x20) != 0) ) { //Xor shift and numlock (both means off)
@@ -534,7 +538,8 @@ static Bitu INT16_Handler(void) {
534538
IO_Write(0x60,0x20); // 500 msec delay, 30 cps
535539
} else if (reg_al == 0x05) { // set repeat rate and delay
536540
IO_Write(0x60,0xf3);
537-
IO_Write(0x60,(reg_bh&3)<<5|(reg_bl&0x1f));
541+
const auto rate_and_delay = (reg_bh & 3) << 5 | (reg_bl & 0x1f);
542+
IO_Write(0x60, check_cast<uint8_t>(rate_and_delay));
538543
} else {
539544
LOG(LOG_BIOS,LOG_ERROR)("INT16:Unhandled Typematic Rate Call %2X BX=%X",reg_al,reg_bx);
540545
}
@@ -545,9 +550,11 @@ static Bitu INT16_Handler(void) {
545550
break;
546551
case 0x12: /* GET EXTENDED SHIFT STATES */
547552
reg_al = mem_readb(BIOS_KEYBOARD_FLAGS1);
548-
reg_ah = (mem_readb(BIOS_KEYBOARD_FLAGS2)&0x73) |
549-
((mem_readb(BIOS_KEYBOARD_FLAGS2)&4)<<5) | // SysReq pressed, bit 7
550-
(mem_readb(BIOS_KEYBOARD_FLAGS3)&0x0c); // Right Ctrl/Alt pressed, bits 2,3
553+
reg_ah = check_cast<uint8_t>((mem_readb(BIOS_KEYBOARD_FLAGS2) & 0x73) |
554+
// SysReq pressed, bit 7
555+
((mem_readb(BIOS_KEYBOARD_FLAGS2) & 4) << 5) |
556+
// Right Ctrl/Alt pressed, bits 2,3
557+
(mem_readb(BIOS_KEYBOARD_FLAGS3) & 0x0c));
551558
break;
552559
case 0x55:
553560
/* Weird call used by some dos apps */

0 commit comments

Comments
 (0)