Skip to content

Commit adc1f12

Browse files
committed
Utilize ModelicaDuplicateString and ModelicaDuplicateStringWithErrorReturn
1 parent 9fa89fa commit adc1f12

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

Modelica/Resources/C-Sources/ModelicaInternal.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* ModelicaInternal.c - External functions for Modelica.Utilities
22
3-
Copyright (C) 2002-2023, Modelica Association and contributors
3+
Copyright (C) 2002-2024, Modelica Association and contributors
44
All rights reserved.
55
66
Redistribution and use in source and binary forms, with or without
@@ -30,6 +30,10 @@
3030
*/
3131

3232
/* Changelog:
33+
Jan. 15, 2024: by Thomas Beutlich
34+
Utilized ModelicaDuplicateString and
35+
ModelicaDuplicateStringWithErrorReturn (ticket #3686)
36+
3337
Nov. 17, 2020: by Thomas Beutlich
3438
Fixed reading files with Unix-style line endings on Windows
3539
for ModelicaInternal_readLine/_readFile (ticket #3631)
@@ -633,8 +637,7 @@ _Ret_z_ const char* ModelicaInternal_fullPathName(_In_z_ const char* name) {
633637
if (tempName == NULL) {
634638
goto FALLBACK_getcwd;
635639
}
636-
fullName = ModelicaAllocateString(strlen(tempName) + 1);
637-
strcpy(fullName, tempName);
640+
fullName = ModelicaDuplicateString(tempName);
638641
ModelicaConvertToUnixDirectorySeparator(fullName);
639642
/* Retain trailing slash to match _fullpath behaviour */
640643
len = strlen(name);
@@ -1073,8 +1076,7 @@ _Ret_z_ const char* ModelicaInternal_readLine(_In_z_ const char* fileName,
10731076
fclose(fp);
10741077
CloseCachedFile(fileName);
10751078
*endOfFile = 1;
1076-
line = ModelicaAllocateString(0);
1077-
line[0] = '\0';
1079+
line = ModelicaDuplicateString("");
10781080
return line;
10791081

10801082
Modelica_OOM_ERROR2:
@@ -1150,18 +1152,16 @@ void ModelicaInternal_getenv(_In_z_ const char* name, int convertToSlash,
11501152
#endif
11511153

11521154
if (value == NULL) {
1153-
result = ModelicaAllocateString(0);
1154-
result[0] = '\0';
1155+
result = ModelicaDuplicateString("");
11551156
*exist = 0;
11561157
}
11571158
else {
11581159
#if defined(_MSC_VER) && _MSC_VER >= 1400
1159-
result = ModelicaAllocateStringWithErrorReturn(len); /* (len - 1) actually is sufficient */
1160+
result = ModelicaDuplicateStringWithErrorReturn(value);
11601161
if (result) {
11611162
#else
1162-
result = ModelicaAllocateString(strlen(value));
1163+
result = ModelicaDuplicateString(value);
11631164
#endif
1164-
strcpy(result, value);
11651165
if ( convertToSlash == 1 ) {
11661166
ModelicaConvertToUnixDirectorySeparator(result);
11671167
}

Modelica/Resources/C-Sources/ModelicaStrings.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* ModelicaStrings.c - External functions for Modelica.Utilities.Strings
22
3-
Copyright (C) 2002-2020, Modelica Association and contributors
3+
Copyright (C) 2002-2024, Modelica Association and contributors
44
All rights reserved.
55
66
Redistribution and use in source and binary forms, with or without
@@ -30,6 +30,10 @@
3030
*/
3131

3232
/* Changelog:
33+
Jan. 15, 2024: by Thomas Beutlich
34+
Utilized ModelicaDuplicateString and
35+
ModelicaDuplicateStringWithErrorReturn (ticket #3686)
36+
3337
Mar. 15, 2020: by Thomas Beutlich
3438
Improved fault-tolerance of ModelicaStrings_substring w.r.t.
3539
index arguments (ticket #3503)
@@ -231,9 +235,7 @@ void ModelicaStrings_scanIdentifier(_In_z_ const char* string,
231235
/* Token missing or not identifier. */
232236
*nextIndex = startIndex;
233237
{
234-
char* s = ModelicaAllocateString(0);
235-
s[0] = '\0';
236-
*identifier = s;
238+
*identifier = ModelicaDuplicateString("");
237239
}
238240
return;
239241
}
@@ -458,9 +460,7 @@ void ModelicaStrings_scanString(_In_z_ const char* string, int startIndex,
458460

459461
Modelica_ERROR:
460462
{
461-
char* s = ModelicaAllocateString(0);
462-
s[0] = '\0';
463-
*result = s;
463+
*result = ModelicaDuplicateString("");
464464
}
465465
*nextIndex = startIndex;
466466
return;

0 commit comments

Comments
 (0)