From 94c4994a12c70d91af44ef0806c83b623846a495 Mon Sep 17 00:00:00 2001 From: false_genesis Date: Wed, 23 Jan 2008 22:13:05 +0000 Subject: [PATCH] * fixed memory allocation crash when using ?{fread,file ALL} in scripts * found a bug with causes block mismatches in def scripts with multiple nested if/loop/endif/endloop blocks. not yet fixed, but added small check. --- src/Client/DefScript/DefScript.cpp | 5 +++++ src/Client/DefScript/DefScriptFileFunctions.cpp | 13 +++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Client/DefScript/DefScript.cpp b/src/Client/DefScript/DefScript.cpp index 930c069..23e13ed 100644 --- a/src/Client/DefScript/DefScript.cpp +++ b/src/Client/DefScript/DefScript.cpp @@ -375,6 +375,11 @@ bool DefScriptPackage::LoadScriptFromFile(std::string fn){ } else if(line=="loop") Blocks.push_back(BLOCK_LOOP); + else if(line=="exitloop" && !Blocks.size()) // TODO: check: at least one BLOCK_LOOP must be opened + { + cantload=true; + break; + } else if(line=="endloop") { if(Blocks.empty() || Blocks.back()!=BLOCK_LOOP) diff --git a/src/Client/DefScript/DefScriptFileFunctions.cpp b/src/Client/DefScript/DefScriptFileFunctions.cpp index 51bfaac..25ff680 100644 --- a/src/Client/DefScript/DefScriptFileFunctions.cpp +++ b/src/Client/DefScript/DefScriptFileFunctions.cpp @@ -113,13 +113,22 @@ DefReturnResult DefScriptPackage::func_fwrite(CmdSet& Set) DefReturnResult DefScriptPackage::func_fread(CmdSet& Set) { - std::fstream *fh = files.GetNoCreate(_NormalizeVarName(Set.arg[0],Set.myname)); + std::string fn = _NormalizeVarName(Set.arg[0],Set.myname); + std::fstream *fh = files.GetNoCreate(fn); if(fh && fh->is_open()) { uint64 bytes; uint64 read = 0; if(stringToLower(Set.defaultarg) == "all") - bytes = -1LL; + { + std::fstream::pos_type begin_pos, end_pos, old_pos = fh->tellg(); + fh->seekg(0, std::ios_base::beg); + begin_pos=fh->tellg(); + fh->seekg(0, std::ios_base::end); + end_pos = fh->tellg(); + fh->seekg(old_pos, std::ios_base::beg); + bytes = (end_pos - begin_pos); + } else bytes = toUint64(Set.defaultarg); std::string ret;