Skip to content

Commit

Permalink
-Fixed memory access violation bug in System.trimChar
Browse files Browse the repository at this point in the history
-Changed floor to follow specification (Real->Real, NOT Real -> Integer)
-Changed System.stringReplace implementation to c++ (in systemimplmisc.cpp) (This was probably not necessary since bug was in trimChar, but impl. is now much more readable ;)

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@4734 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Jan 5, 2010
1 parent 9c7c2f0 commit 3196f8e
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 106 deletions.
2 changes: 1 addition & 1 deletion Compiler/Builtin.mo
Expand Up @@ -3022,7 +3022,7 @@ algorithm
env = Env.extendFrameT(env, "rem", realReal2real);
env = Env.extendFrameT(env, "rem", intInt2int);
env = Env.extendFrameT(env, "ceil", real2real);
envb = Env.extendFrameT(env, "floor", real2int);
envb = Env.extendFrameT(env, "floor", real2real);
env = Env.extendFrameT(envb, "integer", real2int);
env = Env.extendFrameT(env, "Integer", enumeration2int);
env = Env.extendFrameT(env, "abs", real2real) "differentiable functions" ;
Expand Down
3 changes: 1 addition & 2 deletions Compiler/Ceval.mo
Expand Up @@ -2731,9 +2731,8 @@ algorithm
equation
(cache,Values.REAL(rv),_) = ceval(cache,env, exp, impl, st, NONE, msg);
rv_1 = realFloor(rv);
iv=realInt(rv_1);
then
(cache,Values.INTEGER(iv),st);
(cache,Values.REAL(rv_1),st);
end matchcontinue;
end cevalBuiltinFloor;

Expand Down
2 changes: 1 addition & 1 deletion Compiler/DAEUtil.mo
Expand Up @@ -1976,7 +1976,7 @@ algorithm
case (NONE) then "";
case (SOME(SCode.COMMENT(annopt,SOME(cmt))))
equation
str = Util.stringAppendList({" \"",cmt,"\""});
str = Util.stringAppendList({"\"",cmt,"\""});
then
str;
case (SOME(SCode.COMMENT(annopt,NONE))) then "";
Expand Down
16 changes: 16 additions & 0 deletions Compiler/Exp.mo
Expand Up @@ -3819,6 +3819,22 @@ algorithm
end matchcontinue;
end abs;

public function arrayDimensionsToSubscripts "transform array dimensions (in DAE.ExpType) to Dae.Subscript's "
input list<Option<Integer>> dims;
output list<DAE.Subscript> subs;
algorithm
subs := matchcontinue(dims)
local Integer i;
case({}) then {};
case(NONE::dims) equation
subs = arrayDimensionsToSubscripts(dims);
then DAE.WHOLEDIM()::subs;
case(SOME(i)::dims) equation
subs = arrayDimensionsToSubscripts(dims);
then DAE.INDEX(DAE.ICONST(i))::subs;
end matchcontinue;
end arrayDimensionsToSubscripts;

public function arrayTypeDimensions
"Return the array dimensions of a type."
input Type tp;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Makefile.common
Expand Up @@ -16,7 +16,7 @@ else
endif

AST = $(srcdir)/absyn_builder/absyn_builder.a
RTOBJ = $(srcdir)/runtime/systemimpl.o $(srcdir)/runtime/dynload.o \
RTOBJ = $(srcdir)/runtime/systemimpl.o $(srcdir)/runtime/systemimplmisc.o $(srcdir)/runtime/dynload.o \
$(srcdir)/../c_runtime/libc_runtime.a \
$(srcdir)/runtime/rtopts.o $(srcdir)/runtime/socketimpl.o \
$(srcdir)/runtime/printimpl.o $(srcdir)/runtime/ptolemyio.o \
Expand Down
6 changes: 3 additions & 3 deletions Compiler/Util.mo
Expand Up @@ -4511,10 +4511,10 @@ algorithm
res_str = System.stringReplace(str_1, from, to);
then
res_str;
case (_,_)
case (str,_)
equation
print("- Util.modelicaStringToCStr1 failed\n");
then
print("- Util.modelicaStringToCStr1 failed for str:"+&str+&"\n");
then
fail();
end matchcontinue;
end modelicaStringToCStr1;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/runtime/Makefile.vc
Expand Up @@ -17,7 +17,7 @@ SHELL = /bin/sh
CC = cl
IDL = $(OMDEV)\bin\mico\idl

OBJ = rtopts.obj socketimpl.obj printimpl.obj systemimpl.obj dynload.obj settingsimpl.obj \
OBJ = rtopts.obj socketimpl.obj printimpl.obj systemimpl.obj systemimplmisc.obj dynload.obj settingsimpl.obj \
ptolemyio.obj daeext.obj ErrorMessage.obj errorext.obj optmanager.obj \
omc_communication.obj omc_communication_impl.obj corbaimpl.obj \
unitparser.obj unitparserext.obj SimulationResults.obj
Expand Down
103 changes: 6 additions & 97 deletions Compiler/runtime/systemimpl.c
Expand Up @@ -161,97 +161,6 @@ static int set_ldflags(char *str)
return 0;
}

/*
* Description:
* Find and replace text within a string.
*
* Parameters:
* source_src (in) - pointer to source string
* search_str (in) - pointer to search text
* replace_str (in) - pointer to replacement text
*
* Returns:
* Returns a pointer to dynamically-allocated memory containing string
* with occurences of the text pointed to by 'search_str' replaced by with the
* text pointed to by 'replace_str'.
*/

static char *_strcat(char *buf, size_t *buf_size, char **ptr,
const char *addon, size_t addon_len)
{
size_t pos = (*ptr) - buf;
char *ret = buf;
if ((pos + addon_len) > (*buf_size)) {
(*buf_size) = (pos + addon_len);
ret = realloc(buf, (*buf_size) + 1);
if (ret == NULL) {
free(buf);
return NULL;
}
*ptr = ret + pos;
}
memcpy(*ptr, addon, addon_len);
(*ptr) += addon_len;
return ret;
}

char* _replace(const char* source_str,
const char* search_str,
const char* replace_str)
{
char *ostr, *out = NULL;
const char *pos = NULL, *last = NULL;
const size_t nreplace = strlen(replace_str);
const size_t nsearch = strlen(search_str);
size_t ostr_allocated;

if (!source_str || !search_str || !replace_str) {
printf("Not enough arguments\n");
return NULL;
}

ostr_allocated = strlen(source_str);
ostr = malloc(ostr_allocated + 1);
if (!ostr) {
printf("Insufficient memory available\n");
return NULL;
}

last = source_str;
out = ostr;
while((pos = strstr(last, search_str)) != NULL) {
if (last < pos) {
ostr = _strcat(ostr, &ostr_allocated, &out, last, pos - last);
if (ostr == NULL) {
printf("Insufficient memory available\n");
return NULL;
}
}

ostr = _strcat(ostr, &ostr_allocated, &out, replace_str, nreplace);
if (ostr == NULL) {
printf("Insufficient memory available\n");
return NULL;
}

last = pos + nsearch;
}

if (*last != '\0') {
ostr = _strcat(ostr, &ostr_allocated, &out, last, strlen(last));
if (ostr == NULL) {
printf("Insufficient memory available\n");
return NULL;
}
}

*out = '\0';

return ostr;
}



// windows and mingw32
#if defined(__MINGW32__) || defined(_MSC_VER)

Expand Down Expand Up @@ -559,8 +468,8 @@ RML_BEGIN_LABEL(System__trimChar)
}
if(end_pos > start_pos){
res= (char*)malloc(end_pos - start_pos +1);
strncpy(res,&str[start_pos],end_pos - start_pos + 1);
res[end_pos - start_pos + 1] = '\0';
strncpy(res,&str[start_pos],end_pos - start_pos);
res[end_pos - start_pos] = '\0';
rmlA0 = (void*) mk_scon(res);
free(res);
RML_TAILCALLK(rmlSC);
Expand Down Expand Up @@ -775,17 +684,17 @@ RML_BEGIN_LABEL(System__loadLibrary)
_snprintf(libname, MAXPATHLEN, "%s\\%s.dll", currentDirectory, str);
#else
snprintf(libname, MAXPATHLEN, "%s\\%s.dll", currentDirectory, str);
#endif
#endif

h = LoadLibrary(libname);
if (h == NULL) {
//fprintf(stderr, "Unable to load '%s': %lu.\n", libname, GetLastError());
fflush(stderr);
RML_TAILCALLK(rmlFC);
}
}
libIndex = alloc_ptr();
if (libIndex < 0) {
//fprintf(stderr, "Error loading library %s!\n", libname); fflush(stderr);
//fprintf(stderr, "Error loading library %s!\n", libname); fflush(stderr);
FreeLibrary(h);
h = NULL;
RML_TAILCALLK(rmlFC);
Expand Down
42 changes: 42 additions & 0 deletions Compiler/runtime/systemimplmisc.cpp
@@ -0,0 +1,42 @@

/* Miscellaneous C++ file for systemimpl. */


#include <string>

using namespace std;

void FindAndReplace( std::string& tInput, std::string tFind, std::string tReplace )
{
size_t uPos = 0; size_t uFindLen = tFind.length(); size_t uReplaceLen = tReplace.length();

if( uFindLen == 0 )
{
return;
}

for( ;(uPos = tInput.find( tFind, uPos )) != std::string::npos; )
{
tInput.replace( uPos, uFindLen, tReplace );
uPos += uReplaceLen;
}

}


extern "C" {

#include <string.h>

char* _replace(const char* source_str, const char* search_str, const char* replace_str)
{
string str(source_str);
FindAndReplace(str,string(search_str),string(replace_str));

char* res = strdup(str.c_str());
return res;
}

}


10 changes: 10 additions & 0 deletions Compiler/runtime/systemimplmisc.h
@@ -0,0 +1,10 @@
#ifndef SYSTEMIMPLMISC_H
#define SYSTEMIMPLMISC_H

extern "C" {

char* _replace(const char* source_str, const char* search_str, const char* replace_str);

}

#endif

0 comments on commit 3196f8e

Please sign in to comment.