Skip to content

Commit

Permalink
- fix some compiler/cppcheck warnings
Browse files Browse the repository at this point in the history
- fix a memory hole
- fix a copy/paste error
- fix some variable initialisations

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18599 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Henning Kiel committed Jan 8, 2014
1 parent 786fe41 commit 5faa024
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 52 deletions.
29 changes: 16 additions & 13 deletions Compiler/runtime/SimulationResultsCmpTubes.c
Expand Up @@ -200,7 +200,7 @@ static void generateLowTube(privates *priv, double *x, double *y)
static privates* skipCalculateTubes(double *x, double *y, size_t length)
{
privates *priv = (privates*) GC_malloc(sizeof(privates));
int i;

/* set tStart and tStop */
priv->length = length;
priv->tStart = x[0];
Expand All @@ -212,7 +212,7 @@ static privates* skipCalculateTubes(double *x, double *y, size_t length)
priv->yHigh = (double*)GC_malloc_atomic(sizeof(double)*length);
priv->yLow = (double*)GC_malloc_atomic(sizeof(double)*length);
memcpy(priv->yHigh, y, length * sizeof(double));
memcpy(priv->yHigh, y, length * sizeof(double));
memcpy(priv->yLow, y, length * sizeof(double));
return priv;
}

Expand Down Expand Up @@ -369,18 +369,21 @@ static inline double linearInterpolation(double x, double x0, double x1, double
/* Calibrate the target time+value pair onto the source timeline */
static double* calibrateValues(double* sourceTimeLine, double* targetTimeLine, double* targetValues, size_t *nsource, size_t ntarget, double xabstol)
{
double* interpolatedValues;
int j, i;
double x0, x1, y0, y1;
size_t n;

if (0 == nsource) {
return NULL;
}

double* interpolatedValues = (double*) GC_malloc_atomic(sizeof(double)**nsource);

int j = 1, i;
double x, x0, x1, y0, y1;
size_t n = *nsource;
n = *nsource;
interpolatedValues = (double*) GC_malloc_atomic(sizeof(double)*n);

j = 1;
for (i = 0; i < n; i++) {
x = sourceTimeLine[i];
double x = sourceTimeLine[i];

if (targetTimeLine[j] > sourceTimeLine[n - 1] && targetTimeLine[j-1] > sourceTimeLine[n - 1]) { // Avoid extrapolation by cutting the sequence
interpolatedValues[i] = linearInterpolation(x,x0,x1,y0,y1,xabstol);
Expand All @@ -391,7 +394,7 @@ static double* calibrateValues(double* sourceTimeLine, double* targetTimeLine, d
x1 = targetTimeLine[j];
y1 = targetValues[j];

while (x1 <= x && j + 1 < ntarget) { // step source timline to the current moment
while ((x1 <= x) && ((j + 1) < ntarget)) { // step source timline to the current moment
j++;
x1 = targetTimeLine[j];
y1 = targetValues[j];
Expand Down Expand Up @@ -519,13 +522,13 @@ static addTargetEventTimesRes mergeTimelines(addTargetEventTimesRes ref, addTarg

static addTargetEventTimesRes removeUneventfulPoints(addTargetEventTimesRes in, double reltol, double xabstol)
{
int i,iter;
int i;
addTargetEventTimesRes res;
res.values = (double*) GC_malloc_atomic(in.size * sizeof(double));
res.time = (double*) GC_malloc_atomic(in.size * sizeof(double));

do {
iter=0;
int iter = 0;
/* Don't remove first point */
res.values[0] = in.values[0];
res.time[0] = in.time[0];
Expand Down Expand Up @@ -636,11 +639,9 @@ static double* validate(int n, addTargetEventTimesRes ref, double *low, double *
static unsigned int cmpDataTubes(int isResultCmp, char* varname, DataField *time, DataField *reftime, DataField *data, DataField *refdata, double reltol, double rangeDelta, double reltolDiffMaxMin, DiffDataField *ddf, char **cmpdiffvars, unsigned int vardiffindx, int keepEqualResults, void **diffLst, const char *prefix, int isHtml, char **htmlOut)
{
int withTubes = 0 == rangeDelta;
int i;
FILE *fout = NULL;
char *fname = NULL;
char *html;
size_t html_size=0;
/* The tolerance for detecting events is proportional to the number of output points in the file */
double xabstol = (reftime->data[reftime->n-1]-reftime->data[0])*(withTubes ? rangeDelta : 1e-3) / fmax(time->n,reftime->n);
/* Calculate the tubes without additional events added */
Expand Down Expand Up @@ -677,6 +678,7 @@ static unsigned int cmpDataTubes(int isResultCmp, char* varname, DataField *time
double *error = validate(n,ref,low,high,calibrated_values,reltol,abstol,xabstol);
if (isHtml ) {
#if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
size_t html_size=0;
fout = open_memstream(&html, &html_size);
#endif

Expand Down Expand Up @@ -729,6 +731,7 @@ static unsigned int cmpDataTubes(int isResultCmp, char* varname, DataField *time
}
size_t maxn = intmax(intmax(intmax(ref.size,actual.size),priv->countHigh),priv->countLow);
if (fout) {
int i;
const char *empty = isHtml ? "null" : "";
const char *lbracket = isHtml ? "[" : "";
const char *rbracket = isHtml ? "]," : "";
Expand Down
10 changes: 5 additions & 5 deletions Compiler/runtime/matching.c
Expand Up @@ -731,7 +731,7 @@ void match_abmp(int* col_ptrs, int* col_ids, int* row_ptrs, int* row_ids, int* m
temp, stack_col, ptr, eptr, next_col_i, start_col_i,
pcount = 1, desired_level, L, desired, stack_last, current_col,
lim = 0.1*sqrt(m + n), nunmatched = 0, level_ptr,
update_counter = n, counter_limit = n, tunmatched = 0,level_0, ppcount;
update_counter = n, counter_limit = n, tunmatched = 0,level_0, ppcount = 0;

level_0 = 0;
for(i = 0; i < m; i++) {
Expand Down Expand Up @@ -1233,7 +1233,7 @@ void match_pr_fifo_fair(int* col_ptrs, int* col_ids, int* row_ptrs, int* row_ids
void matching(int* col_ptrs, int* col_ids, int* match, int* row_match, int n, int m, int matching_id, int cheap_id, double relabel_period, int clear_match) {
int* row_ptrs;
int* row_ids;
int i, sp, ep, row;
int i;

if (clear_match==1)
{
Expand Down Expand Up @@ -1261,11 +1261,11 @@ void matching(int* col_ptrs, int* col_ids, int* match, int* row_match, int n, in
row_ids = (int*) malloc(nz * sizeof(int));

for(i = 0; i < n; i++) {
sp = col_ptrs[i];
ep = col_ptrs[i+1];
int sp = col_ptrs[i];
int ep = col_ptrs[i+1];

for(;sp < ep; sp++) {
row = col_ids[sp];
int row = col_ids[sp];
row_ids[t_row_ptrs[row]++] = i;
}
}
Expand Down
12 changes: 5 additions & 7 deletions Compiler/runtime/matching_cheap.c
Expand Up @@ -229,10 +229,9 @@ void sk_cheap_rand(int* col_ptrs, int* col_ids, int* row_ptrs, int* row_ids,

int* randarr = (int*)malloc(n * sizeof(int));
for(i = 0; i < n; i++){randarr[i] = i;}
int temp;
for(i = n-1; i >= 0; i--) {
int z = omc_tinymt64_generate_fast_int_range(&random_seed,i+1);
temp = randarr[i]; randarr[i] = randarr[z]; randarr[z] = temp;
int temp = randarr[i]; randarr[i] = randarr[z]; randarr[z] = temp;
}

int stop = 0;
Expand Down Expand Up @@ -531,7 +530,7 @@ void cheap_matching(int* col_ptrs, int* col_ids, int* row_ptrs, int* row_ids,
void cheapmatching(int* col_ptrs, int* col_ids, int* match, int* row_match, int n, int m, int cheap_id, int clear_match) {
int* row_ptrs;
int* row_ids;
int i, sp, ep, row;
int i;

if (clear_match==1)
{
Expand All @@ -544,7 +543,6 @@ void cheapmatching(int* col_ptrs, int* col_ids, int* match, int* row_match, int
}

if(cheap_id > do_old_cheap) {

row_ptrs = (int*) malloc((m+1) * sizeof(int));
memset(row_ptrs, 0, (m+1) * sizeof(int));

Expand All @@ -559,11 +557,11 @@ void cheapmatching(int* col_ptrs, int* col_ids, int* match, int* row_match, int
row_ids = (int*) malloc(nz * sizeof(int));

for(i = 0; i < n; i++) {
sp = col_ptrs[i];
ep = col_ptrs[i+1];
int sp = col_ptrs[i];
int ep = col_ptrs[i+1];

for(;sp < ep; sp++) {
row = col_ids[sp];
int row = col_ids[sp];
row_ids[t_row_ptrs[row]++] = i;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Compiler/runtime/printimpl.c
Expand Up @@ -399,7 +399,7 @@ static int PrintImpl__writeBufConvertLines(threadData_t *threadData,const char *
#else /* real OSes */
const char *re_str[2] = {"^ */[*]#modelicaLine .([^:]*):([0-9]*):[0-9]*-[0-9]*:[0-9]*.[*]/$", "^ */[*]#endModelicaLine[*]/$"};
#endif
const char *modelicaFileName = NULL;
char *modelicaFileName = NULL;
char* strtmp = NULL;
str[nfilled] = '\0';

Expand Down
11 changes: 8 additions & 3 deletions Compiler/runtime/settingsimpl.c
Expand Up @@ -59,6 +59,7 @@ extern char* _replace(char* source_str,char* search_str,char* replace_str); //De

static char* winPath = NULL;

#if defined(linux) || defined(__APPLE_CC__)
/* Helper function to strip /bin/... from the executable path of omc */
static void stripbinpath(char *omhome)
{
Expand All @@ -67,7 +68,9 @@ static void stripbinpath(char *omhome)
*tmp = '\0';
assert(tmp = strrchr(omhome,'/'));
*tmp = '\0';
return;
}
#endif

/* Do not free or modify the returned variable of getInstallationDirectoryPath. It's part of the environment! */
#if defined(linux)
Expand Down Expand Up @@ -298,9 +301,11 @@ extern const char* SettingsImpl__getTempDirectoryPath(void)
}
#else
const char* str = getenv("TMPDIR");
if (str == NULL)
str = strdup("/tmp");
tempDirectoryPath = strdup(str);
if (str == NULL) {
tempDirectoryPath = strdup("/tmp");
} else {
tempDirectoryPath = strdup(str);
}
#endif
}
return tempDirectoryPath;
Expand Down
4 changes: 2 additions & 2 deletions Compiler/runtime/socketimpl.c
Expand Up @@ -86,7 +86,7 @@ fsync (int fd)
#include "errorext.h"

static int serversocket;
static unsigned int fromlen;
static int fromlen;
static struct sockaddr_in clientAddr;

static int
Expand Down Expand Up @@ -167,7 +167,7 @@ extern char* SocketImpl_handlerequest(int sock)
FD_SET(sock,&sockSet); // create fd set of
if (len == bufSize) { // If we filled the buffer, check for more
while ( select(sock+1,&sockSet,NULL,NULL,&timeout) > 0) {
tmpBufSize*=(int)(bufSize*1.4);
tmpBufSize=(int)(bufSize*1.4);
nAdditionalElts = tmpBufSize-bufSize;
tmpBuf=(char*)malloc(tmpBufSize);
if (tmpBuf == NULL) {
Expand Down
4 changes: 2 additions & 2 deletions Compiler/runtime/systemimpl.c
Expand Up @@ -526,7 +526,7 @@ int SystemImpl__systemCall(const char* str, const char* outFile)
fflush(NULL); /* flush output so the testsuite is deterministic */
#if defined(__MINGW32__) || defined(_MSC_VER)
if (*outFile) {
const char *command = malloc(strlen(str) + strlen(outFile) + 9);
char *command = malloc(strlen(str) + strlen(outFile) + 9);
sprintf(command, "%s > %s 2>&1", str, outFile);
status = system(command);
free(command);
Expand Down Expand Up @@ -643,7 +643,7 @@ static void* systemCallWorkerThread(void *argVoid)
void* SystemImpl__systemCallParallel(void *lst, int numThreads)
{
void *tmp = lst;
int sz = 0, i;
int sz = 0, i = 0;
char **calls;
int *results;
while (RML_NILHDR != RML_GETHDR(tmp)) {
Expand Down
36 changes: 18 additions & 18 deletions Compiler/runtime/unitparser.cpp
Expand Up @@ -396,13 +396,13 @@ string UnitParser::prettyPrintUnit2str(Unit unit) {

Unit UnitParser::solveMIP(Unit unit, bool innerCall) {
#ifndef NO_LPLIB
int numBaseUnits = _base.size();
int numDerivedUnits = 0;
unsigned int numBaseUnits = _base.size();
unsigned int numDerivedUnits = 0;
// Counting the derived units by traversing all units
for (map<string, Unit>::iterator it = _units.begin(); it != _units.end(); it++)
if (!it->second.isBaseUnit())
numDerivedUnits++;
int NU = numBaseUnits + numDerivedUnits;
unsigned int NU = numBaseUnits + numDerivedUnits;

// Create MIP with 2*NU variables(columns)
lprec *lp = make_lp(0, 2 * NU);
Expand All @@ -414,7 +414,7 @@ Unit UnitParser::solveMIP(Unit unit, bool innerCall) {
}

/* Set name of variables for debug printing */
int i;
unsigned int i;
for (i = 1; i <= numBaseUnits; i++) {
char * s1 = (char*) _base[i - 1].unitName.c_str();
char * s2 = (char*) (string("-") + string(s1)).c_str();
Expand Down Expand Up @@ -452,9 +452,9 @@ Unit UnitParser::solveMIP(Unit unit, bool innerCall) {
return unit;
}

int c;
unsigned int c;
// Set the constraint
for (int r = 0; r < numBaseUnits; r++) {
for (unsigned int r = 0; r < numBaseUnits; r++) {
int j = 0;
/* Set 0..numBaseUnits-1 first columns */
for (c = 0; c < numBaseUnits; c++) {
Expand All @@ -471,7 +471,7 @@ Unit UnitParser::solveMIP(Unit unit, bool innerCall) {
c++;
}
}
for (int j2 = 0; j2 < NU; j2++) {
for (unsigned int j2 = 0; j2 < NU; j2++) {
colno[j] = colno[j2] + NU;
row[j++] = -row[j2];
}
Expand All @@ -487,11 +487,11 @@ Unit UnitParser::solveMIP(Unit unit, bool innerCall) {

/* Set the objective */
int j = 0;
int c2;
unsigned int c2;
/* element 0..numBaseUnits-1*/
for (c2 = 0; c2 < numBaseUnits; c2++) {
double cost = 1;
for (int r2 = 0; r2 < numBaseUnits; r2++) {
for (unsigned int r2 = 0; r2 < numBaseUnits; r2++) {
double b = r2 < unit.unitVec.size() ? unit.unitVec[r2].toReal()
: 0.0;
cost += fabs(b - (c2 == r2 ? 1 : 0));
Expand All @@ -506,7 +506,7 @@ Unit UnitParser::solveMIP(Unit unit, bool innerCall) {
double cost = 1;
Unit u = it->second;
if (!u.isBaseUnit()) {
for (int r2 = 0; r2 < numBaseUnits; r2++) {
for (unsigned int r2 = 0; r2 < numBaseUnits; r2++) {
double b1 =
r2 < unit.unitVec.size() ? unit.unitVec[r2].toReal()
: 0.0;
Expand All @@ -521,9 +521,9 @@ Unit UnitParser::solveMIP(Unit unit, bool innerCall) {
}
}
/* elements NU .. NU+numBaseUnits-1 */
for (int c2 = 0; c2 < numBaseUnits; c2++) {
for (unsigned int c2 = 0; c2 < numBaseUnits; c2++) {
double cost = 1;
for (int r2 = 0; r2 < numBaseUnits; r2++) {
for (unsigned int r2 = 0; r2 < numBaseUnits; r2++) {
double b = r2 < unit.unitVec.size() ? unit.unitVec[r2].toReal()
: 0.0;
cost += fabs(b - (c2 == r2 ? -1 : 0));
Expand All @@ -538,7 +538,7 @@ Unit UnitParser::solveMIP(Unit unit, bool innerCall) {
double cost = 1;
Unit u = it->second;
if (!u.isBaseUnit()) {
for (int r2 = 0; r2 < numBaseUnits; r2++) {
for (unsigned int r2 = 0; r2 < numBaseUnits; r2++) {
double b1 =
r2 < unit.unitVec.size() ? unit.unitVec[r2].toReal()
: 0.0;
Expand All @@ -560,7 +560,7 @@ Unit UnitParser::solveMIP(Unit unit, bool innerCall) {
}

/* Set up domain , Reals for base units, Integers for derived units */
int v = 0;
unsigned int v = 0;
for (; v < numBaseUnits; v++)
set_int(lp, v + 1, FALSE);
for (; v < NU; v++)
Expand All @@ -586,7 +586,7 @@ Unit UnitParser::solveMIP(Unit unit, bool innerCall) {
Unit prettyUnit, retVal;
if (res == 0) {
//cout << "result =" << get_var_primalresult(lp,0) << endl;
for (int i = 0; i < 2 * NU; i++) {
for (unsigned int i = 0; i < 2 * NU; i++) {
double res = get_var_primalresult(lp, i + 1 + numBaseUnits);
//cerr << i << " : " << res << endl ;
if (i >= NU) {
Expand Down Expand Up @@ -637,7 +637,7 @@ Unit UnitParser::solveMIP(Unit unit, bool innerCall) {
int UnitParser::actualNumDerived(Unit unit) {
int res = 0;
int numBaseUnits = _base.size();
for (int i = numBaseUnits; i < unit.unitVec.size(); i++) {
for (unsigned int i = numBaseUnits; i < unit.unitVec.size(); i++) {
if (!unit.unitVec[i].isZero()) {
res++;
}
Expand All @@ -662,7 +662,7 @@ Unit UnitParser::minimizeDerivedUnits(Unit unit,Unit origUnit, double factor) {

stack<int> stack; // stack of indices for derived units =! 0
if (actualNumDerived(unit) > 1) {
for (int i = numBaseUnits; i < unit.unitVec.size(); i++) {
for (unsigned int i = numBaseUnits; i < unit.unitVec.size(); i++) {
if (!unit.unitVec[i].isZero()) {
stack.push(i); // store nth position in unit map
}
Expand All @@ -683,7 +683,7 @@ Unit UnitParser::minimizeDerivedUnits(Unit unit,Unit origUnit, double factor) {
resetNthUnitWeight(indx,factor);
}
if (actualNumDerived(newUnit)==1) break;
for (int i = numBaseUnits; i < newUnit.unitVec.size(); i++) {
for (unsigned int i = numBaseUnits; i < newUnit.unitVec.size(); i++) {
if (!newUnit.unitVec[i].isZero()&&_derivedUnitsVisited.find(i) == _derivedUnitsVisited.end()) {
stack.push(i); // store nth position in unit map
cout << "adding " << i << " to stack" << endl;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/runtime/unitparserext.cpp
Expand Up @@ -28,7 +28,7 @@ void UnitParserExtImpl__checkpoint(void)

void UnitParserExtImpl__rollback(void)
{
if (rollbackStack.size() == 0) {
if (rollbackStack.empty()) {
cerr << "Error, rollback on empty stack" << endl;
exit(1);
}
Expand Down

0 comments on commit 5faa024

Please sign in to comment.