1
0
Fork 0

Fix unsafe memory operations and warnings in the fatDrive class

- Move imageDiskList from pointer to vector of unique_ptr
- Replace string operations with size-limited versions
- Initialize members
- Eliminate unecessary casts
- Eliminate memory-leak on pointer assignment
This commit is contained in:
krcroft 2019-12-07 11:55:38 -08:00 committed by Patryk Obara
parent e942a02fcb
commit c9198b2944
11 changed files with 219 additions and 131 deletions

View file

@ -19,7 +19,9 @@
#ifndef DOSBOX_BIOS_DISK_H
#define DOSBOX_BIOS_DISK_H
#include <memory>
#include <stdio.h>
#include <vector>
#ifndef DOSBOX_MEM_H
#include "mem.h"
#endif
@ -55,6 +57,8 @@ public:
Bit8u GetBiosType(void);
Bit32u getSectSize(void);
imageDisk(FILE *imgFile, const char *imgName, Bit32u imgSizeK, bool isHardDisk);
imageDisk(const imageDisk&) = delete; // prevent copy
imageDisk& operator=(const imageDisk&) = delete; // prevent assignment
~imageDisk() { if(diskimg != NULL) { fclose(diskimg); } };
bool hardDrive;
@ -77,8 +81,8 @@ void incrementFDD(void);
#define MAX_DISK_IMAGES (2 + MAX_HDD_IMAGES)
extern imageDisk *imageDiskList[MAX_DISK_IMAGES];
extern imageDisk *diskSwap[MAX_SWAPPABLE_DISKS];
extern std::vector<std::unique_ptr<imageDisk>> imageDiskList;
extern std::vector<std::unique_ptr<imageDisk>> diskSwap;
extern Bit32s swapPosition;
extern Bit16u imgDTASeg; /* Real memory location of temporary DTA pointer for fat image disk access */
extern RealPt imgDTAPtr; /* Real memory location of temporary DTA pointer for fat image disk access */

View file

@ -279,7 +279,12 @@ protected:
class DOS_PSP :public MemStruct {
public:
DOS_PSP (Bit16u segment) { SetPt(segment);seg=segment;};
DOS_PSP(Bit16u segment)
: seg(0)
{
SetPt(segment);
seg = segment;
}
void MakeNew (Bit16u memSize);
void CopyFileTable (DOS_PSP* srcpsp,bool createchildpsp);
Bit16u FindFreeFileEntry (void);
@ -311,32 +316,32 @@ private:
#pragma pack(1)
#endif
struct sPSP {
Bit8u exit[2]; /* CP/M-like exit poimt */
Bit16u next_seg; /* Segment of first byte beyond memory allocated or program */
Bit8u fill_1; /* single char fill */
Bit8u far_call; /* far call opcode */
RealPt cpm_entry; /* CPM Service Request address*/
RealPt int_22; /* Terminate Address */
RealPt int_23; /* Break Address */
RealPt int_24; /* Critical Error Address */
Bit16u psp_parent; /* Parent PSP Segment */
Bit8u files[20]; /* File Table - 0xff is unused */
Bit16u environment; /* Segment of evironment table */
RealPt stack; /* SS:SP Save point for int 0x21 calls */
Bit16u max_files; /* Maximum open files */
RealPt file_table; /* Pointer to File Table PSP:0x18 */
RealPt prev_psp; /* Pointer to previous PSP */
Bit8u interim_flag;
Bit8u truename_flag;
Bit16u nn_flags;
Bit16u dos_version;
Bit8u fill_2[14]; /* Lot's of unused stuff i can't care aboue */
Bit8u service[3]; /* INT 0x21 Service call int 0x21;retf; */
Bit8u fill_3[9]; /* This has some blocks with FCB info */
Bit8u fcb1[16]; /* first FCB */
Bit8u fcb2[16]; /* second FCB */
Bit8u fill_4[4]; /* unused */
CommandTail cmdtail;
Bit8u exit[2]; /* CP/M-like exit poimt */
Bit16u next_seg; /* Segment of first byte beyond memory allocated or program */
Bit8u fill_1; /* single char fill */
Bit8u far_call; /* far call opcode */
RealPt cpm_entry; /* CPM Service Request address*/
RealPt int_22; /* Terminate Address */
RealPt int_23; /* Break Address */
RealPt int_24; /* Critical Error Address */
Bit16u psp_parent; /* Parent PSP Segment */
Bit8u files[20]; /* File Table - 0xff is unused */
Bit16u environment; /* Segment of evironment table */
RealPt stack; /* SS:SP Save point for int 0x21 calls */
Bit16u max_files; /* Maximum open files */
RealPt file_table; /* Pointer to File Table PSP:0x18 */
RealPt prev_psp; /* Pointer to previous PSP */
Bit8u interim_flag;
Bit8u truename_flag;
Bit16u nn_flags;
Bit16u dos_version;
Bit8u fill_2[14]; /* Lot's of unused stuff i can't care aboue */
Bit8u service[3]; /* INT 0x21 Service call int 0x21;retf; */
Bit8u fill_3[9]; /* This has some blocks with FCB info */
Bit8u fcb1[16]; /* first FCB */
Bit8u fcb2[16]; /* second FCB */
Bit8u fill_4[4]; /* unused */
CommandTail cmdtail;
} GCC_ATTRIBUTE(packed);
#ifdef _MSC_VER
#pragma pack()
@ -348,7 +353,12 @@ public:
class DOS_ParamBlock:public MemStruct {
public:
DOS_ParamBlock(PhysPt addr) {pt=addr;}
DOS_ParamBlock(PhysPt addr)
: exec{0, 0, 0, 0, 0, 0},
overlay{0, 0}
{
pt = addr;
}
void Clear(void);
void LoadData(void);
void SaveData(void); /* Save it as an exec block */
@ -376,7 +386,9 @@ public:
class DOS_InfoBlock:public MemStruct {
public:
DOS_InfoBlock () {};
DOS_InfoBlock()
: seg(0)
{}
void SetLocation(Bit16u seg);
void SetFirstMCB(Bit16u _first_mcb);
void SetBuffers(Bit16u x,Bit16u y);

View file

@ -94,9 +94,11 @@ private:
class DOS_Device : public DOS_File {
public:
DOS_Device(const DOS_Device& orig):DOS_File(orig) {
devnum=orig.devnum;
open=true;
DOS_Device(const DOS_Device& orig)
: DOS_File(orig),
devnum(orig.devnum)
{
open = true;
}
DOS_Device & operator= (const DOS_Device & orig) {
DOS_File::operator=(orig);

View file

@ -85,7 +85,7 @@ static INLINE void host_writed(HostPt off,Bit32u val) {
#else
static INLINE Bit8u host_readb(HostPt off) {
return *(Bit8u *)off;
return *off;
}
static INLINE Bit16u host_readw(HostPt off) {
return *(Bit16u *)off;
@ -94,7 +94,7 @@ static INLINE Bit32u host_readd(HostPt off) {
return *(Bit32u *)off;
}
static INLINE void host_writeb(HostPt off,Bit8u val) {
*(Bit8u *)(off)=val;
*off = val;
}
static INLINE void host_writew(HostPt off,Bit16u val) {
*(Bit16u *)(off)=val;
@ -107,7 +107,7 @@ static INLINE void host_writed(HostPt off,Bit32u val) {
static INLINE void var_write(Bit8u * var, Bit8u val) {
host_writeb((HostPt)var, val);
host_writeb(var, val);
}
static INLINE void var_write(Bit16u * var, Bit16u val) {

View file

@ -68,6 +68,8 @@ private:
class Program {
public:
Program();
Program(const Program&) = delete; // prevent copy
Program& operator=(const Program&) = delete; // prevent assignment
virtual ~Program(){
delete cmd;
delete psp;

View file

@ -45,6 +45,8 @@ extern DOS_Shell * first_shell;
class BatchFile {
public:
BatchFile(DOS_Shell * host,char const* const resolved_name,char const* const entered_name, char const * const cmd_line);
BatchFile(const BatchFile&) = delete; // prevent copying
BatchFile& operator=(const BatchFile&) = delete; // prevent assignment
virtual ~BatchFile();
virtual bool ReadLine(char * line);
bool Goto(char * where);
@ -70,7 +72,8 @@ private:
public:
DOS_Shell();
DOS_Shell(const DOS_Shell&) = delete; // prevent copy
DOS_Shell& operator=(const DOS_Shell&) = delete; // prevent assignment
void Run(void);
void RunInternal(void); //for command /C
/* A load of subfunctions */
@ -136,7 +139,10 @@ private:
bool installed;
std::string buf;
public:
AutoexecObject():installed(false){ };
AutoexecObject()
: installed(false),
buf("")
{}
void Install(std::string const &in);
void InstallBefore(std::string const &in);
~AutoexecObject();

View file

@ -55,6 +55,13 @@ char * safe_strcpy(char (& dst)[N], const char * src) noexcept {
return & dst[0];
}
template<size_t N>
char * safe_strcat(char (& dst)[N], const char * src) noexcept {
const size_t dst_size = sizeof(dst);
strncat(dst, src, dst_size - strnlen(dst, dst_size) - 1);
return & dst[0];
}
#define safe_strncpy(a,b,n) do { strncpy((a),(b),(n)-1); (a)[(n)-1] = 0; } while (0)
#ifdef HAVE_STRINGS_H