From 687e3cf058fd5d13bc11075bbb45a3b69a232983 Mon Sep 17 00:00:00 2001 From: Peter Veenstra Date: Mon, 1 Apr 2019 16:50:49 +0000 Subject: [PATCH] Ensure room before adding environment variable using set. Noted when running a subshell under NC. Bug left: we don't have a lot of environment space in that case (not the 160 minimum). Imported-from: https://svn.code.sf.net/p/dosbox/code-0/dosbox/trunk@4200 --- src/misc/programs.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/misc/programs.cpp b/src/misc/programs.cpp index ed1d3c96..12ffcb86 100644 --- a/src/misc/programs.cpp +++ b/src/misc/programs.cpp @@ -220,9 +220,16 @@ Bitu Program::GetEnvCount(void) { } bool Program::SetEnv(const char * entry,const char * new_string) { - PhysPt env_read=PhysMake(psp->GetEnvironment(),0); - PhysPt env_write=env_read; - char env_string[1024+1]; + PhysPt env_read = PhysMake(psp->GetEnvironment(),0); + + //Get size of environment. + DOS_MCB mcb(psp->GetEnvironment()-1); + Bit16u envsize = mcb.GetSize()*16; + + + PhysPt env_write = env_read; + PhysPt env_write_start = env_read; + char env_string[1024+1] = { 0 }; do { MEM_StrCopy(env_read,env_string,1024); if (!env_string[0]) break; @@ -235,11 +242,14 @@ bool Program::SetEnv(const char * entry,const char * new_string) { } while (1); /* TODO Maybe save the program name sometime. not really needed though */ /* Save the new entry */ + + //ensure room + if (envsize <= (env_write-env_write_start) + strlen(entry) + 1 + strlen(new_string) + 2) return false; + if (new_string[0]) { std::string bigentry(entry); for (std::string::iterator it = bigentry.begin(); it != bigentry.end(); ++it) *it = toupper(*it); - sprintf(env_string,"%s=%s",bigentry.c_str(),new_string); -// sprintf(env_string,"%s=%s",entry,new_string); //oldcode + snprintf(env_string,1024+1,"%s=%s",bigentry.c_str(),new_string); MEM_BlockWrite(env_write,env_string,(Bitu)(strlen(env_string)+1)); env_write += (PhysPt)(strlen(env_string)+1); }