From 33916513233ce53b340752a33eff6c9c3914d5c9 Mon Sep 17 00:00:00 2001 From: krcroft Date: Sat, 18 Jan 2020 15:29:10 -0800 Subject: [PATCH] Fix NULL issues in shell code --- src/shell/shell.cpp | 9 +++++++++ src/shell/shell_cmds.cpp | 19 ++++++++++++------- src/shell/shell_misc.cpp | 8 ++++---- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/shell/shell.cpp b/src/shell/shell.cpp index 7441dc96..3b44fdbf 100644 --- a/src/shell/shell.cpp +++ b/src/shell/shell.cpp @@ -202,6 +202,11 @@ Bitu DOS_Shell::GetRedirection(char *s, char **ifn, char **ofn,bool * append) { // else // *lr=0; t = (char*)malloc(lr-*ofn+1); + if (t == nullptr) { + E_Exit("SHELL: Could not allocate %u bytes in parser", + static_cast(lr-*ofn+1)); + } + safe_strncpy(t,*ofn,lr-*ofn+1); *ofn=t; continue; @@ -216,6 +221,10 @@ Bitu DOS_Shell::GetRedirection(char *s, char **ifn, char **ofn,bool * append) { // else // *lr=0; t = (char*)malloc(lr-*ifn+1); + if (t == nullptr) { + E_Exit("SHELL: Could not allocate %u bytes in parser", + static_cast(lr-*ifn+1)); + } safe_strncpy(t,*ifn,lr-*ifn+1); *ifn=t; continue; diff --git a/src/shell/shell_cmds.cpp b/src/shell/shell_cmds.cpp index 1c8020b6..bed75ad3 100644 --- a/src/shell/shell_cmds.cpp +++ b/src/shell/shell_cmds.cpp @@ -1319,20 +1319,25 @@ void DOS_Shell::CMD_CHOICE(char * args){ HELP("CHOICE"); static char defchoice[3] = {'y','n',0}; char *rem = NULL, *ptr; - bool optN = ScanCMDBool(args,"N"); - bool optS = ScanCMDBool(args,"S"); //Case-sensitive matching - ScanCMDBool(args,"T"); //Default Choice after timeout + bool optN = false; + bool optS = false; if (args) { + optN = ScanCMDBool(args,"N"); + optS = ScanCMDBool(args,"S"); //Case-sensitive matching + ScanCMDBool(args,"T"); //Default Choice after timeout char *last = strchr(args,0); StripSpaces(args); rem = ScanCMDRemain(args); + if (rem && *rem && (tolower(rem[1]) != 'c')) { WriteOut(MSG_Get("SHELL_ILLEGAL_SWITCH"),rem); return; } if (args == rem) { assert(args); - args = strchr(rem, '\0') + 1; + if (rem != nullptr) { + args = strchr(rem, '\0') + 1; + } } if (rem) rem += 2; if(rem && rem[0]==':') rem++; /* optional : after /c */ @@ -1379,10 +1384,10 @@ void DOS_Shell::CMD_ATTRIB(char *args){ void DOS_Shell::CMD_PATH(char *args){ HELP("PATH"); - if(args && *args && strlen(args)){ + if(args && strlen(args)){ char pathstring[DOS_PATHLENGTH+CROSS_LEN+20]={ 0 }; strcpy(pathstring,"set PATH="); - while(args && *args && (*args=='='|| *args==' ')) + while(args && *args && (*args=='='|| *args==' ')) args++; strcat(pathstring,args); this->ParseLine(pathstring); @@ -1398,7 +1403,7 @@ void DOS_Shell::CMD_PATH(char *args){ void DOS_Shell::CMD_VER(char *args) { HELP("VER"); - if(args && *args) { + if(args && strlen(args)) { char* word = StripWord(args); if(strcasecmp(word,"set")) return; word = StripWord(args); diff --git a/src/shell/shell_misc.cpp b/src/shell/shell_misc.cpp index e0282898..38702b90 100644 --- a/src/shell/shell_misc.cpp +++ b/src/shell/shell_misc.cpp @@ -254,11 +254,11 @@ void DOS_Shell::InputCommand(char * line) { // build the completion list char mask[DOS_PATHLENGTH] = {0}; - if (strlen(p_completion_start) + 3 >= DOS_PATHLENGTH) { - //Beep; - break; - } if (p_completion_start) { + if (strlen(p_completion_start) + 3 >= DOS_PATHLENGTH) { + //Beep; + break; + } safe_strncpy(mask, p_completion_start,DOS_PATHLENGTH); char* dot_pos=strrchr(mask,'.'); char* bs_pos=strrchr(mask,'\\');