Skip to content

Commit 3d1fa58

Browse files
authored
Merge pull request #58 from OpenSimulationInterface/fix/undefined-behaviors
Fix undefined behaviors in C++ examples
2 parents 1a4478c + 62438ea commit 3d1fa58

File tree

5 files changed

+39
-31
lines changed

5 files changed

+39
-31
lines changed

examples/OSMPCNetworkProxy/OSMPCNetworkProxy.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ fmi2Status doExitInitializationMode(OSMPCNetworkProxy component)
359359
return fmi2OK;
360360
}
361361

362-
fmi2Status doCalc(OSMPCNetworkProxy component, fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize, fmi2Boolean noSetFMUStatePriorToCurrentPointfmi2Component)
362+
fmi2Status doCalc(OSMPCNetworkProxy component, fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize, fmi2Boolean noSetFMUStatePriorToCurrentPoint)
363363
{
364364
void* buffer=NULL;
365365
int buffersize=0;

examples/OSMPDummySensor/OSMPDummySensor.cpp

+20-14
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ bool COSMPDummySensor::get_fmi_sensor_view_config(osi3::SensorViewConfiguration&
114114

115115
void COSMPDummySensor::set_fmi_sensor_view_config_request(const osi3::SensorViewConfiguration& data)
116116
{
117-
data.SerializeToString(&currentConfigRequestBuffer);
118-
encode_pointer_to_integer(currentConfigRequestBuffer.data(),integer_vars[FMI_INTEGER_SENSORVIEW_CONFIG_REQUEST_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_CONFIG_REQUEST_BASELO_IDX]);
119-
integer_vars[FMI_INTEGER_SENSORVIEW_CONFIG_REQUEST_SIZE_IDX]=(fmi2Integer)currentConfigRequestBuffer.length();
120-
normal_log("OSMP","Providing %08X %08X, writing from %p ...",integer_vars[FMI_INTEGER_SENSORVIEW_CONFIG_REQUEST_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_CONFIG_REQUEST_BASELO_IDX],currentConfigRequestBuffer.data());
117+
data.SerializeToString(currentConfigRequestBuffer);
118+
encode_pointer_to_integer(currentConfigRequestBuffer->data(),integer_vars[FMI_INTEGER_SENSORVIEW_CONFIG_REQUEST_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_CONFIG_REQUEST_BASELO_IDX]);
119+
integer_vars[FMI_INTEGER_SENSORVIEW_CONFIG_REQUEST_SIZE_IDX]=(fmi2Integer)currentConfigRequestBuffer->length();
120+
normal_log("OSMP","Providing %08X %08X, writing from %p ...",integer_vars[FMI_INTEGER_SENSORVIEW_CONFIG_REQUEST_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_CONFIG_REQUEST_BASELO_IDX],currentConfigRequestBuffer->data());
121121
swap(currentConfigRequestBuffer,lastConfigRequestBuffer);
122122
}
123123

@@ -142,10 +142,10 @@ bool COSMPDummySensor::get_fmi_sensor_view_in(osi3::SensorView& data)
142142

143143
void COSMPDummySensor::set_fmi_sensor_data_out(const osi3::SensorData& data)
144144
{
145-
data.SerializeToString(&currentOutputBuffer);
146-
encode_pointer_to_integer(currentOutputBuffer.data(),integer_vars[FMI_INTEGER_SENSORDATA_OUT_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORDATA_OUT_BASELO_IDX]);
147-
integer_vars[FMI_INTEGER_SENSORDATA_OUT_SIZE_IDX]=(fmi2Integer)currentOutputBuffer.length();
148-
normal_log("OSMP","Providing %08X %08X, writing from %p ...",integer_vars[FMI_INTEGER_SENSORDATA_OUT_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORDATA_OUT_BASELO_IDX],currentOutputBuffer.data());
145+
data.SerializeToString(currentOutputBuffer);
146+
encode_pointer_to_integer(currentOutputBuffer->data(),integer_vars[FMI_INTEGER_SENSORDATA_OUT_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORDATA_OUT_BASELO_IDX]);
147+
integer_vars[FMI_INTEGER_SENSORDATA_OUT_SIZE_IDX]=(fmi2Integer)currentOutputBuffer->length();
148+
normal_log("OSMP","Providing %08X %08X, writing from %p ...",integer_vars[FMI_INTEGER_SENSORDATA_OUT_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORDATA_OUT_BASELO_IDX],currentOutputBuffer->data());
149149
swap(currentOutputBuffer,lastOutputBuffer);
150150
}
151151

@@ -255,7 +255,7 @@ void rotatePoint(double x, double y, double z,double yaw,double pitch,double rol
255255
rz = matrix[2][0] * x + matrix[2][1] * y + matrix[2][2] * z;
256256
}
257257

258-
fmi2Status COSMPDummySensor::doCalc(fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize, fmi2Boolean noSetFMUStatePriorToCurrentPointfmi2Component)
258+
fmi2Status COSMPDummySensor::doCalc(fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize, fmi2Boolean noSetFMUStatePriorToCurrentPoint)
259259
{
260260
DEBUGBREAK();
261261

@@ -373,6 +373,10 @@ COSMPDummySensor::COSMPDummySensor(fmi2String theinstanceName, fmi2Type thefmuTy
373373
loggingOn(!!theloggingOn),
374374
simulation_started(false)
375375
{
376+
currentOutputBuffer=new string();
377+
lastOutputBuffer=new string();
378+
currentConfigRequestBuffer=new string();
379+
lastConfigRequestBuffer=new string();
376380
loggingCategories.clear();
377381
loggingCategories.insert("FMI");
378382
loggingCategories.insert("OSMP");
@@ -381,22 +385,24 @@ COSMPDummySensor::COSMPDummySensor(fmi2String theinstanceName, fmi2Type thefmuTy
381385

382386
COSMPDummySensor::~COSMPDummySensor()
383387
{
384-
388+
delete currentOutputBuffer;
389+
delete lastOutputBuffer;
390+
delete currentConfigRequestBuffer;
391+
delete lastConfigRequestBuffer;
385392
}
386393

387-
388394
fmi2Status COSMPDummySensor::SetDebugLogging(fmi2Boolean theloggingOn, size_t nCategories, const fmi2String categories[])
389395
{
390396
fmi_verbose_log("fmi2SetDebugLogging(%s)", theloggingOn ? "true" : "false");
391397
loggingOn = theloggingOn ? true : false;
392398
if (categories && (nCategories > 0)) {
393399
loggingCategories.clear();
394400
for (size_t i=0;i<nCategories;i++) {
395-
if (categories[i] == "FMI")
401+
if (0==strcmp(categories[i],"FMI"))
396402
loggingCategories.insert("FMI");
397-
else if (categories[i] == "OSMP")
403+
else if (0==strcmp(categories[i],"OSMP"))
398404
loggingCategories.insert("OSMP");
399-
else if (categories[i] == "OSI")
405+
else if (0==strcmp(categories[i],"OSI"))
400406
loggingCategories.insert("OSI");
401407
}
402408
} else {

examples/OSMPDummySensor/OSMPDummySensor.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ class COSMPDummySensor {
202202
fmi2Real real_vars[FMI_REAL_VARS];
203203
string string_vars[FMI_STRING_VARS];
204204
bool simulation_started;
205-
string currentOutputBuffer;
206-
string lastOutputBuffer;
207-
string currentConfigRequestBuffer;
208-
string lastConfigRequestBuffer;
205+
string* currentOutputBuffer;
206+
string* lastOutputBuffer;
207+
string* currentConfigRequestBuffer;
208+
string* lastConfigRequestBuffer;
209209

210210
/* Simple Accessors */
211211
fmi2Boolean fmi_valid() { return boolean_vars[FMI_BOOLEAN_VALID_IDX]; }

examples/OSMPDummySource/OSMPDummySource.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ void encode_pointer_to_integer(const void* ptr,fmi2Integer& hi,fmi2Integer& lo)
102102

103103
void COSMPDummySource::set_fmi_sensor_view_out(const osi3::SensorView& data)
104104
{
105-
data.SerializeToString(&currentBuffer);
106-
encode_pointer_to_integer(currentBuffer.data(),integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASELO_IDX]);
107-
integer_vars[FMI_INTEGER_SENSORVIEW_OUT_SIZE_IDX]=(fmi2Integer)currentBuffer.length();
108-
normal_log("OSMP","Providing %08X %08X, writing from %p ...",integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASELO_IDX],currentBuffer.data());
105+
data.SerializeToString(currentBuffer);
106+
encode_pointer_to_integer(currentBuffer->data(),integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASELO_IDX]);
107+
integer_vars[FMI_INTEGER_SENSORVIEW_OUT_SIZE_IDX]=(fmi2Integer)currentBuffer->length();
108+
normal_log("OSMP","Providing %08X %08X, writing from %p ...",integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASEHI_IDX],integer_vars[FMI_INTEGER_SENSORVIEW_OUT_BASELO_IDX],currentBuffer->data());
109109
swap(currentBuffer,lastBuffer);
110110
}
111111

@@ -183,7 +183,7 @@ void rotatePoint(double x, double y, double z,double yaw,double pitch,double rol
183183
rz = matrix[2][0] * x + matrix[2][1] * y + matrix[2][2] * z;
184184
}
185185

186-
fmi2Status COSMPDummySource::doCalc(fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize, fmi2Boolean noSetFMUStatePriorToCurrentPointfmi2Component)
186+
fmi2Status COSMPDummySource::doCalc(fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize, fmi2Boolean noSetFMUStatePriorToCurrentPoint)
187187
{
188188
DEBUGBREAK();
189189

@@ -280,6 +280,8 @@ COSMPDummySource::COSMPDummySource(fmi2String theinstanceName, fmi2Type thefmuTy
280280
visible(!!thevisible),
281281
loggingOn(!!theloggingOn)
282282
{
283+
currentBuffer = new string();
284+
lastBuffer = new string();
283285
loggingCategories.clear();
284286
loggingCategories.insert("FMI");
285287
loggingCategories.insert("OSMP");
@@ -288,22 +290,22 @@ COSMPDummySource::COSMPDummySource(fmi2String theinstanceName, fmi2Type thefmuTy
288290

289291
COSMPDummySource::~COSMPDummySource()
290292
{
291-
293+
delete currentBuffer;
294+
delete lastBuffer;
292295
}
293296

294-
295297
fmi2Status COSMPDummySource::SetDebugLogging(fmi2Boolean theloggingOn, size_t nCategories, const fmi2String categories[])
296298
{
297299
fmi_verbose_log("fmi2SetDebugLogging(%s)", theloggingOn ? "true" : "false");
298300
loggingOn = theloggingOn ? true : false;
299301
if (categories && (nCategories > 0)) {
300302
loggingCategories.clear();
301303
for (size_t i=0;i<nCategories;i++) {
302-
if (categories[i] == "FMI")
304+
if (0==strcmp(categories[i],"FMI"))
303305
loggingCategories.insert("FMI");
304-
else if (categories[i] == "OSMP")
306+
else if (0==strcmp(categories[i],"OSMP"))
305307
loggingCategories.insert("OSMP");
306-
else if (categories[i] == "OSI")
308+
else if (0==strcmp(categories[i],"OSI"))
307309
loggingCategories.insert("OSI");
308310
}
309311
} else {

examples/OSMPDummySource/OSMPDummySource.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ class COSMPDummySource {
190190
fmi2Integer integer_vars[FMI_INTEGER_VARS];
191191
fmi2Real real_vars[FMI_REAL_VARS];
192192
string string_vars[FMI_STRING_VARS];
193-
string currentBuffer;
194-
string lastBuffer;
193+
string* currentBuffer;
194+
string* lastBuffer;
195195

196196
/* Simple Accessors */
197197
fmi2Boolean fmi_valid() { return boolean_vars[FMI_BOOLEAN_VALID_IDX]; }

0 commit comments

Comments
 (0)