Skip to content

Commit

Permalink
Utilize ModelicaDuplicateString and ModelicaDuplicateStringWithErrorR…
Browse files Browse the repository at this point in the history
…eturn
  • Loading branch information
beutlich committed Dec 13, 2020
1 parent c96655c commit 78607f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
18 changes: 18 additions & 0 deletions .CI/Test/Common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char* ModelicaAllocateString(size_t len) {
void *data = malloc(len + 1); /* Never free'd in the test programs */
Expand All @@ -13,6 +14,23 @@ char* ModelicaAllocateStringWithErrorReturn(size_t len) {
return malloc(len + 1); /* Never free'd in the test programs */
}

char* ModelicaDuplicateString(const char* str) {
void *data = malloc(strlen(len) + 1); /* Never free'd in the test programs */
assert(data);
if (NULL != data) {
strcpy(data, str);
}
return data;
}

char* ModelicaDuplicateStringWithErrorReturn(const char* str) {
void *data = malloc(strlen(len) + 1); /* Never free'd in the test programs */
if (NULL != data) {
strcpy(data, str);
}
return data;
}

void ModelicaMessage(const char *string) {
printf("%s\n", string);
}
Expand Down
18 changes: 6 additions & 12 deletions Modelica/Resources/C-Sources/ModelicaInternal.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ void ModelicaInternal_readDirectory(_In_z_ const char* directory, int nFiles,
}

/* Allocate Modelica memory for file/directory name and copy name */
pName = ModelicaAllocateStringWithErrorReturn(strlen(pinfo->d_name));
pName = ModelicaDuplicateStringWithErrorReturn(pinfo->d_name);
if ( pName == NULL ) {
errnoTemp = errno;
closedir(pdir);
Expand All @@ -528,7 +528,6 @@ void ModelicaInternal_readDirectory(_In_z_ const char* directory, int nFiles,
directory, strerror(errnoTemp));
}
}
strcpy(pName, pinfo->d_name);

/* Save pointer to file */
files[iFiles] = pName;
Expand Down Expand Up @@ -612,8 +611,7 @@ _Ret_z_ const char* ModelicaInternal_fullPathName(_In_z_ const char* name) {
name, strerror(errno));
return "";
}
fullName = ModelicaAllocateString(strlen(tempName));
strcpy(fullName, tempName);
fullName = ModelicaDuplicateString(tempName);
ModelicaConvertToUnixDirectorySeparator(fullName);
return fullName;
#elif (_BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED || _POSIX_VERSION >= 200112L)
Expand Down Expand Up @@ -678,8 +676,7 @@ _Ret_z_ const char* ModelicaInternal_temporaryFileName(void) {
ModelicaFormatError("Not possible to get temporary filename\n%s", strerror(errno));
return "";
}
fullName = ModelicaAllocateString(strlen(tempName));
strcpy(fullName, tempName);
fullName = ModelicaDuplicateString(tempName);
ModelicaConvertToUnixDirectorySeparator(fullName);

return fullName;
Expand Down Expand Up @@ -1002,12 +999,11 @@ void ModelicaInternal_readFile(_In_z_ const char* fileName,
while (iLines <= nLines) {
readLine(&buf, &bufLen, fp);

line = ModelicaAllocateStringWithErrorReturn(strlen(buf));
line = ModelicaDuplicateStringWithErrorReturn(buf);
if ( line == NULL ) {
goto Modelica_OOM_ERROR1;
}

strcpy(line, buf);
string[iLines - 1] = line;
iLines++;
}
Expand Down Expand Up @@ -1054,12 +1050,11 @@ _Ret_z_ const char* ModelicaInternal_readLine(_In_z_ const char* fileName,
}
}

line = ModelicaAllocateStringWithErrorReturn(strlen(buf));
line = ModelicaDuplicateStringWithErrorReturn(buf);
if (line == NULL) {
goto Modelica_OOM_ERROR2;
}

strcpy(line, buf);
CacheFileForReading(fp, fileName, lineNumber, buf, bufLen);
*endOfFile = 0;
return line;
Expand Down Expand Up @@ -1125,8 +1120,7 @@ _Ret_z_ const char* ModelicaInternal_getcwd(int dummy) {
cwd = "";
}
#endif
directory = ModelicaAllocateString(strlen(cwd));
strcpy(directory, cwd);
directory = ModelicaDuplicateString(cwd);
ModelicaConvertToUnixDirectorySeparator(directory);
return directory;
}
Expand Down

0 comments on commit 78607f6

Please sign in to comment.