Skip to content

DRAFT: fix undefined casting behavior through new SGRawValue type#1303

Closed
heshpdx wants to merge 2 commits into
JSBSim-Team:masterfrom
heshpdx:master
Closed

DRAFT: fix undefined casting behavior through new SGRawValue type#1303
heshpdx wants to merge 2 commits into
JSBSim-Team:masterfrom
heshpdx:master

Conversation

@heshpdx

@heshpdx heshpdx commented Jul 14, 2025

Copy link
Copy Markdown
Contributor

We are seeing undefined behavior due to the casting of enums as integers, and that comes from calling the casted function pointers to tie property values.

This PR is a proof-of-concept of how to fix the issue using std::function with lambdas to wrap the casting. We should figure out if this is the best approach for changing the code. Right now this patch fixes the first error exposed by the LLVM optimizer, but there are 25+ more files of changes we would have to take on to make it clean.

#834

heshpdx added 2 commits July 14, 2025 21:26
First attempt. There is a build error with the latest code.
@heshpdx heshpdx mentioned this pull request Jul 14, 2025
else {
if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
if (!getter) property->setAttribute(SGPropertyNode::READ, false);
tied_properties.push_back(PropertyState(property, nullptr));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is causing the segfault in the TestPlanet CI test.

The issue has to do with BindingInstance in PropertyState being set to nullptr. In FGFDMExec.cpp line 796, a special 'Unbind()' assumes that all of the properties under 'atmosphere' have a BindingInstance set to that model. This is new code not in the SPEC CPU codebase.

@bcoconni

Copy link
Copy Markdown
Member

Thanks for the PR.

I have a number of comments:

  1. If we choose to address this issue by creating a new class inheriting from SGRawValue then it cannot be located in src/sim gear/props.[ch]xx for the reasons I have exposed in JSBSim for SPEC CPUv8 #834 (comment). Instead the new class should be located in src/input_output/FGPropertyManager.h so that JSBSim can compile in FlightGear.
  2. Once we have decided to use a new class, using lambdas and std::function seem a bit overkill to me. I'd rather pass the enum value to the class constructor and it would be stored as a member of the class. That way the calls to FGPropertyManager::Tie would look very much like what they currently are.

@bcoconni

Copy link
Copy Markdown
Member

Right now this patch fixes the first error exposed by the LLVM optimizer, but there are 25+ more files of changes we would have to take on to make it clean.

This is mostly addressed in the PR #1304 so that we can focus on the actual issue in this PR.

@bcoconni bcoconni added the bug label Jul 15, 2025
@heshpdx heshpdx closed this Jul 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants