diff --git a/include/support.h b/include/support.h index 87d910df..704cc105 100644 --- a/include/support.h +++ b/include/support.h @@ -34,6 +34,8 @@ //#define nocasestrcmp(a,b) stricmp(a,b) #endif +#define safe_strncpy(a,b,n) do { strncpy((a),(b),(n)-1); (a)[(n)-1] = 0; } while (0) + #ifdef HAVE_STRINGS_H #include #endif diff --git a/src/debug/debug.cpp b/src/debug/debug.cpp index 65e8503c..57119421 100644 --- a/src/debug/debug.cpp +++ b/src/debug/debug.cpp @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: debug.cpp,v 1.63 2005-04-18 18:46:27 qbix79 Exp $ */ +/* $Id: debug.cpp,v 1.64 2005-07-19 19:45:15 qbix79 Exp $ */ #include #include @@ -175,7 +175,7 @@ bool GetDescriptorInfo(char* selname, char* out1, char* out2) class CDebugVar { public: - CDebugVar(char* _name, PhysPt _adr) { adr=_adr; (strlen(name)<15)?strcpy(name,_name):strncpy(name,_name,15); name[15]=0; }; + CDebugVar(char* _name, PhysPt _adr) { adr=_adr; safe_strncpy(name,_name,16); }; char* GetName(void) { return name; }; PhysPt GetAdr (void) { return adr; }; @@ -1673,7 +1673,7 @@ public: char filename[128]; char args[256]; cmd->FindCommand(1,temp_line); - strncpy(filename,temp_line.c_str(),128); + safe_strncpy(filename,temp_line.c_str(),128); // Read commandline Bit16u i =2; bool ok = false; diff --git a/src/dos/cdrom_aspi_win32.cpp b/src/dos/cdrom_aspi_win32.cpp index 87861f31..ca8360f1 100644 --- a/src/dos/cdrom_aspi_win32.cpp +++ b/src/dos/cdrom_aspi_win32.cpp @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: cdrom_aspi_win32.cpp,v 1.13 2005-02-10 10:20:50 qbix79 Exp $ */ +/* $Id: cdrom_aspi_win32.cpp,v 1.14 2005-07-19 19:45:16 qbix79 Exp $ */ #if defined (WIN32) @@ -219,8 +219,7 @@ bool CDROM_Interface_Aspi::GetVendor(BYTE HA_num, BYTE SCSI_Id, BYTE SCSI_Lun, c strcpy (szBuffer, "error" ); return false; } else { - strncpy(szBuffer,szBuffer+8,25); - szBuffer[25] = 0; + safe_strncpy(szBuffer,szBuffer+8,26); int len = strlen(szBuffer); for (int i=0; i #include @@ -29,6 +29,7 @@ #include #include "cdrom.h" #include "drives.h" +#include "support.h" #if !defined(WIN32) #include @@ -399,8 +400,7 @@ static string dirname(const char * file) { else { int len = (int)(sep - file); char tmp[MAX_FILENAME_LENGTH]; - strncpy(tmp, file, len); - tmp[len] = '\0'; + safe_strncpy(tmp, file, len+1); return tmp; } } @@ -417,7 +417,7 @@ bool CDROM_Interface_Image::LoadCueSheet(char *cuefile) bool success; bool canAddTrack = false; char tmp[MAX_FILENAME_LENGTH]; // dirname can change its argument - strncpy(tmp, cuefile, MAX_FILENAME_LENGTH); + safe_strncpy(tmp, cuefile, MAX_FILENAME_LENGTH); string pathname(dirname(tmp)); ifstream in; in.open(cuefile, ios::in); @@ -595,7 +595,7 @@ bool CDROM_Interface_Image::GetRealFileName(string &filename, string &pathname) // finally check if file is in a dosbox local drive char fullname[CROSS_LEN]; char tmp[CROSS_LEN]; - strncpy(tmp, filename.c_str(), CROSS_LEN); + safe_strncpy(tmp, filename.c_str(), CROSS_LEN); Bit8u drive; if (!DOS_MakeName(tmp, fullname, &drive)) return false; diff --git a/src/dos/cdrom_ioctl_linux.cpp b/src/dos/cdrom_ioctl_linux.cpp index 4aacb3ed..6a492aa0 100644 --- a/src/dos/cdrom_ioctl_linux.cpp +++ b/src/dos/cdrom_ioctl_linux.cpp @@ -19,6 +19,7 @@ #include #include "cdrom.h" +#include "support.h" #if defined (LINUX) #include @@ -45,7 +46,7 @@ bool CDROM_Interface_Ioctl::GetUPC(unsigned char& attr, char* upc) if (ret > 0) { attr = 0; - strncpy(upc, (char*)cdrom_mcn.medium_catalog_number, 14); + safe_strncpy(upc, (char*)cdrom_mcn.medium_catalog_number, 14); } return (ret > 0); @@ -86,7 +87,7 @@ bool CDROM_Interface_Ioctl::SetDevice(char* path, int forceCD) if (success) { const char* tmp = SDL_CDName(forceCD); - if (tmp) strncpy(device_name, tmp, 512); + if (tmp) safe_strncpy(device_name, tmp, 512); else success = false; } diff --git a/src/dos/dos_programs.cpp b/src/dos/dos_programs.cpp index 6e73336c..6f9339a6 100644 --- a/src/dos/dos_programs.cpp +++ b/src/dos/dos_programs.cpp @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: dos_programs.cpp,v 1.36 2005-04-06 20:48:14 qbix79 Exp $ */ +/* $Id: dos_programs.cpp,v 1.37 2005-07-19 19:45:16 qbix79 Exp $ */ #include #include @@ -422,7 +422,7 @@ void LOADFIX::Run(void) if (cmd->FindCommand(commandNr++,temp_line)) { // get Filename char filename[128]; - strncpy(filename,temp_line.c_str(),128); + safe_strncpy(filename,temp_line.c_str(),128); // Setup commandline bool ok; char args[256]; @@ -570,10 +570,10 @@ public: // convert dosbox filename to system filename char fullname[CROSS_LEN]; char tmp[CROSS_LEN]; - strncpy(tmp, temp_line.c_str(), CROSS_LEN); + safe_strncpy(tmp, temp_line.c_str(), CROSS_LEN); Bit8u drive; - if (!DOS_MakeName(tmp, fullname, &drive)) { + if (!DOS_MakeName(tmp, fullname, &drive) || strncmp(Drives[drive]->GetInfo(),"local directory",15)) { WriteOut(MSG_Get("PROGRAM_IMGMOUNG_FILE_NOT_FOUND")); return; } diff --git a/src/dos/drive_cache.cpp b/src/dos/drive_cache.cpp index f8623d33..24fbdb00 100644 --- a/src/dos/drive_cache.cpp +++ b/src/dos/drive_cache.cpp @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: drive_cache.cpp,v 1.43 2005-04-21 18:43:28 qbix79 Exp $ */ +/* $Id: drive_cache.cpp,v 1.44 2005-07-19 19:45:31 qbix79 Exp $ */ #include "drives.h" #include "dos_inc.h" @@ -262,8 +262,7 @@ void DOS_Drive_Cache::CacheOut(const char* path, bool ignoreLastDir) char tmp[CROSS_LEN] = { 0 }; Bit32s len = strrchr(path,CROSS_FILESPLIT) - path; if (len>0) { - strncpy(tmp,path,len); - tmp[len] = 0; + safe_strncpy(tmp,path,len+1); } else { strcpy(tmp,path); } @@ -463,11 +462,10 @@ void DOS_Drive_Cache::CreateShortName(CFileInfo* curDir, CFileInfo* info) info->shortNr = CreateShortNameID(curDir,tmpName); sprintf(buffer,"%d",info->shortNr); // Copy first letters - Bit16u tocopy; + Bit16u tocopy = 0; if (len+strlen(buffer)+1>8) tocopy = 8 - strlen(buffer) - 1; else tocopy = len; - strncpy(info->shortname,tmpName,tocopy); - info->shortname[tocopy] = 0; + safe_strncpy(info->shortname,tmpName,tocopy+1); // Copy number strcat(info->shortname,"~"); strcat(info->shortname,buffer); @@ -527,7 +525,7 @@ DOS_Drive_Cache::CFileInfo* DOS_Drive_Cache::FindDirInfo(const char* path, char* do { // bool errorcheck = false; pos = strchr(start,CROSS_FILESPLIT); - if (pos) { strncpy(dir,start,pos-start); dir[pos-start] = 0; /*errorcheck = true;*/ } + if (pos) { safe_strncpy(dir,start,pos-start+1); /*errorcheck = true;*/ } else { strcpy(dir,start); }; // Path found diff --git a/src/dos/drive_iso.cpp b/src/dos/drive_iso.cpp index 7cfbb0b1..abeda36a 100644 --- a/src/dos/drive_iso.cpp +++ b/src/dos/drive_iso.cpp @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: drive_iso.cpp,v 1.7 2005-03-25 09:06:46 qbix79 Exp $ */ +/* $Id: drive_iso.cpp,v 1.8 2005-07-19 19:45:31 qbix79 Exp $ */ #include #include @@ -446,7 +446,7 @@ bool isoDrive :: lookup(isoDirEntry *de, const char *path) if (!strcmp(path, "")) return true; char isoPath[ISO_MAXPATHNAME]; - strncpy(isoPath, path, ISO_MAXPATHNAME); + safe_strncpy(isoPath, path, ISO_MAXPATHNAME); strreplace(isoPath, '\\', '/'); int beginPos = 0; @@ -456,8 +456,7 @@ bool isoDrive :: lookup(isoDirEntry *de, const char *path) char name[38]; if (pos - beginPos >= 38) return false; if (beginPos >= ISO_MAXPATHNAME) return false; - strncpy(name, &isoPath[beginPos], pos - beginPos); - name[pos - beginPos] = 0; + safe_strncpy(name, &isoPath[beginPos], pos - beginPos + 1); beginPos = pos + 1; if (!IS_DIR(de->fileFlags)) return false; if (!lookupSingle(de, name, EXTENT_LOCATION(*de), DATA_LENGTH(*de))) return false; diff --git a/src/gui/midi_alsa.h b/src/gui/midi_alsa.h index ac8caa53..fa295e22 100644 --- a/src/gui/midi_alsa.h +++ b/src/gui/midi_alsa.h @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: midi_alsa.h,v 1.11 2005-02-10 10:20:52 qbix79 Exp $ */ +/* $Id: midi_alsa.h,v 1.12 2005-07-19 19:45:31 qbix79 Exp $ */ #define ALSA_PCM_OLD_HW_PARAMS_API #define ALSA_PCM_OLD_SW_PARAMS_API @@ -129,7 +129,7 @@ public: // try to use port specified in config file if (conf && conf[0]) { - strncpy(var, conf, 10); + safe_strncpy(var, conf, 10); if (parse_addr(var, &seq_client, &seq_port) < 0) { LOG_MSG("ALSA:Invalid alsa port %s", var); return false; diff --git a/src/gui/midi_oss.h b/src/gui/midi_oss.h index 8a604a04..7aef2565 100644 --- a/src/gui/midi_oss.h +++ b/src/gui/midi_oss.h @@ -29,7 +29,7 @@ public: char * GetName(void) { return "oss";}; bool Open(const char * conf) { char devname[512]; - if (conf && conf[0]) strncpy(devname,conf,512); + if (conf && conf[0]) safe_strncpy(devname,conf,512); else strcpy(devname,"/dev/sequencer"); char * devfind=(strrchr(devname,',')); if (devfind) { diff --git a/src/gui/sdl_mapper.cpp b/src/gui/sdl_mapper.cpp index ba3d94d7..c82b2055 100644 --- a/src/gui/sdl_mapper.cpp +++ b/src/gui/sdl_mapper.cpp @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: sdl_mapper.cpp,v 1.12 2005-06-13 14:48:02 qbix79 Exp $ */ +/* $Id: sdl_mapper.cpp,v 1.13 2005-07-19 19:45:31 qbix79 Exp $ */ #define OLD_JOYSTICK 1 @@ -95,7 +95,7 @@ static CBindList holdlist; class CEvent { public: CEvent(char * _entry) { - strncpy(entry,_entry,16); + safe_strncpy(entry,_entry,16); events.push_back(this); bindlist.clear(); activity=0; @@ -1444,6 +1444,7 @@ void MAPPER_Run(void) { /* Be sure that there is no update in progress */ GFX_EndUpdate(); mapper.surface=SDL_SetVideoMode(640,480,8,0); + if (mapper.surface == NULL) E_Exit("Could not initialize video mode for mapper: %s",SDL_GetError()); /* Set some palette entries */ SDL_SetPalette(mapper.surface, SDL_LOGPAL|SDL_PHYSPAL, map_pal, 0, 4); diff --git a/src/gui/sdlmain.cpp b/src/gui/sdlmain.cpp index 39842348..88fdb3d3 100644 --- a/src/gui/sdlmain.cpp +++ b/src/gui/sdlmain.cpp @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: sdlmain.cpp,v 1.85 2005-05-18 21:59:58 qbix79 Exp $ */ +/* $Id: sdlmain.cpp,v 1.86 2005-07-19 19:45:32 qbix79 Exp $ */ #ifndef _GNU_SOURCE #define _GNU_SOURCE @@ -316,18 +316,24 @@ static SDL_Surface * GFX_SetupSurfaceScaled(Bit32u sdl_flags,Bit32u bpp) { } sdl.clip.x=(Sint16)((sdl.desktop.width-sdl.clip.w)/2); sdl.clip.y=(Sint16)((sdl.desktop.height-sdl.clip.h)/2); - return sdl.surface=SDL_SetVideoMode(sdl.desktop.width,sdl.desktop.height,bpp,sdl_flags|SDL_FULLSCREEN|SDL_HWSURFACE); + sdl.surface=SDL_SetVideoMode(sdl.desktop.width,sdl.desktop.height,bpp,sdl_flags|SDL_FULLSCREEN|SDL_HWSURFACE); + if (sdl.surface == NULL) E_Exit("Could not set fullscreen video mode %ix%i-%i: %s",sdl.desktop.width,sdl.desktop.height,bpp,SDL_GetError()); + return sdl.surface; } else { sdl.clip.x=0;sdl.clip.y=0; sdl.clip.w=(Bit16u)(sdl.draw.width*sdl.draw.scalex); sdl.clip.h=(Bit16u)(sdl.draw.height*sdl.draw.scaley); - return sdl.surface=SDL_SetVideoMode(sdl.clip.w,sdl.clip.h,bpp,sdl_flags|SDL_FULLSCREEN|SDL_HWSURFACE); + sdl.surface=SDL_SetVideoMode(sdl.clip.w,sdl.clip.h,bpp,sdl_flags|SDL_FULLSCREEN|SDL_HWSURFACE); + if (sdl.surface == NULL) E_Exit("Could not set fullscreen video mode %ix%i-%i: %s",sdl.clip.w,sdl.clip.h,bpp,SDL_GetError()); + return sdl.surface; } } else { sdl.clip.x=0;sdl.clip.y=0; sdl.clip.w=(Bit16u)(sdl.draw.width*sdl.draw.scalex*sdl.desktop.hwscale); sdl.clip.h=(Bit16u)(sdl.draw.height*sdl.draw.scaley*sdl.desktop.hwscale); - return sdl.surface=SDL_SetVideoMode(sdl.clip.w,sdl.clip.h,bpp,sdl_flags|SDL_HWSURFACE); + sdl.surface=SDL_SetVideoMode(sdl.clip.w,sdl.clip.h,bpp,sdl_flags|SDL_HWSURFACE); + if (sdl.surface == NULL) E_Exit("Could not set windowed video mode %ix%i-%i: %s",sdl.clip.w,sdl.clip.h,bpp,SDL_GetError()); + return sdl.surface; } } @@ -357,14 +363,17 @@ dosurface: sdl.clip.y=(Sint16)((sdl.desktop.height-height)/2); sdl.surface=SDL_SetVideoMode(sdl.desktop.width,sdl.desktop.height,bpp, SDL_FULLSCREEN|SDL_HWSURFACE|(sdl.desktop.doublebuf ? SDL_DOUBLEBUF|SDL_ASYNCBLIT : 0)|SDL_HWPALETTE); + if (sdl.surface == NULL) E_Exit("Could not set fullscreen video mode %ix%i-%i: %s",sdl.desktop.width,sdl.desktop.height,bpp,SDL_GetError()); } else { sdl.clip.x=0;sdl.clip.y=0; sdl.surface=SDL_SetVideoMode(width,height,bpp, SDL_FULLSCREEN|SDL_HWSURFACE|(sdl.desktop.doublebuf ? SDL_DOUBLEBUF|SDL_ASYNCBLIT : 0)|SDL_HWPALETTE); + if (sdl.surface == NULL) E_Exit("Could not set fullscreen video mode %ix%i-%i: %s",width,height,bpp,SDL_GetError()); } } else { sdl.clip.x=0;sdl.clip.y=0; sdl.surface=SDL_SetVideoMode(width,height,bpp,SDL_HWSURFACE); + if (sdl.surface == NULL) E_Exit("Could not set windowed video mode %ix%i-%i: %s",width,height,bpp,SDL_GetError()); } if (sdl.surface) switch (sdl.surface->format->BitsPerPixel) { case 8:sdl.draw.mode=GFX_8;break; @@ -857,6 +866,10 @@ static void GUI_StartUp(Section * sec) { #if C_OPENGL if(sdl.desktop.want_type==SCREEN_OPENGL){ /* OPENGL is requested */ sdl.surface=SDL_SetVideoMode(640,400,0,SDL_OPENGL); + if (sdl.surface == NULL) { + LOG_MSG("Could not initialize OpenGL, switching back to surface"); + sdl.desktop.want_type=SCREEN_SURFACE; + } else { sdl.opengl.framebuf=0; sdl.opengl.texture=0; sdl.opengl.displaylist=0; @@ -877,11 +890,13 @@ static void GUI_StartUp(Section * sec) { } else { sdl.opengl.packed_pixel=sdl.opengl.paletted_texture=false; } + } } /* OPENGL is requested end */ #endif //OPENGL /* Initialize screen for first time */ sdl.surface=SDL_SetVideoMode(640,400,0,0); + if (sdl.surface == NULL) E_Exit("Could not initialize video: %s",SDL_GetError()); sdl.desktop.bpp=sdl.surface->format->BitsPerPixel; if (sdl.desktop.bpp==24) { LOG_MSG("SDL:You are running in 24 bpp mode, this will slow down things!"); diff --git a/src/hardware/mixer.cpp b/src/hardware/mixer.cpp index 06424fa6..be75c972 100644 --- a/src/hardware/mixer.cpp +++ b/src/hardware/mixer.cpp @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: mixer.cpp,v 1.33 2005-04-29 13:45:26 qbix79 Exp $ */ +/* $Id: mixer.cpp,v 1.34 2005-07-19 19:45:32 qbix79 Exp $ */ /* Remove the sdl code from here and have it handeld in the sdlmain. @@ -486,7 +486,7 @@ static void MIXER_ProgramStart(Program * * make) { MixerChannel* MixerObject::Install(MIXER_Handler handler,Bitu freq,char * name){ if(!installed) { if(strlen(name) > 31) E_Exit("Too long mixer channel name"); - strncpy(m_name,name,31); + safe_strncpy(m_name,name,32); installed = true; return MIXER_AddChannel(handler,freq,name); } else { diff --git a/src/misc/setup.cpp b/src/misc/setup.cpp index c7953dcc..460f00b1 100644 --- a/src/misc/setup.cpp +++ b/src/misc/setup.cpp @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: setup.cpp,v 1.28 2005-04-22 09:09:09 qbix79 Exp $ */ +/* $Id: setup.cpp,v 1.29 2005-07-19 19:45:33 qbix79 Exp $ */ #include "dosbox.h" #include "cross.h" @@ -337,7 +337,7 @@ bool Config::ParseConfigFile(const char* configfilename){ void Config::ParseEnv(char ** envp) { for(char** env=envp; *env;env++) { char copy[1024]; - strncpy(copy,*env,1024); + safe_strncpy(copy,*env,1024); if(strncasecmp(copy,"DOSBOX_",7)) continue; char* sec_name = ©[7];