Skip to content

fix: ensuring no segfault when c library function returns oversized string#1794

Open
Angus-Bethke-Bachmann wants to merge 7 commits into
masterfrom
anbt/PRG-4261
Open

fix: ensuring no segfault when c library function returns oversized string#1794
Angus-Bethke-Bachmann wants to merge 7 commits into
masterfrom
anbt/PRG-4261

Conversation

@Angus-Bethke-Bachmann

Copy link
Copy Markdown
Contributor

Changed

  • Ensuring no segfault when c library function returns oversized string
  • Added a new field to string data type information that specifies if user supplied a size in source

Testing

  • Added tests for specific case in call lowerer
  • Added lit test to verify the truncation case
  • Added lit test to verify the c library case

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

Build Artifacts

🐧 Linux

Artifact Link Size
deb-x86_64 Download 38.3 MB
schema Download 0.0 MB
stdlib Download 32.3 MB
plc-x86_64 Download 43.4 MB
deb-aarch64 Download 30.7 MB
plc-aarch64 Download 43.2 MB

From workflow run

🪟 Windows

Artifact Link Size
stdlib.lib Download 4.0 MB
stdlib.dll Download 0.1 MB
plc.exe Download 38.2 MB

From workflow run

Comment thread src/lowering/calls.rs Outdated
Comment thread src/lowering/calls.rs Outdated
Comment thread src/index/indexer/user_type_indexer.rs
Comment thread src/resolver/tests/resolve_literals_tests.rs
Comment thread src/lowering/calls.rs

node_type.is_sized_string()
}
_ => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The logic in this match arm seems like it duplicates the _ arm of is_output_assignment_and_type_cast_needed. Can we extract this into a function and use them in all applicable predicates here?

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.

With more changes to address the above these have diverged far enough apart that this is no longer necessary. But I'm happy to spend more time trying to streamline this file if you'd like?

Comment thread src/lowering/calls.rs Outdated
Comment thread src/lowering/calls.rs Outdated

@mhasel mhasel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Previous comments have all been addressed, however a regression slipped in with the guessing heuristic approach

Comment thread src/lowering/calls.rs Outdated
}
}

let declared_index_with_return_slot = declared_index + 1;

@mhasel mhasel Jul 13, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of this guessing heuristic, can't we thread the correct index through here? I think the lowerer should deterministically know it (either aggregates or fn-pointers, right?)

I think this also caused a regression where it now scans if there is a VAR_OUTPUT either left or right, which can cause mis-fires on arguments that are not VAR_OUTPUTs at all. If you have an aggregate VAR_INPUT that is larger than a neighboring VAR_OUTPUT, it will get a temp variable instead, leading to incorrect behaviour.

Regression example:

FUNCTION Config_GetString : DINT
      VAR_INPUT  Key   : STRING[1023]; END_VAR
      VAR_OUTPUT Value : STRING[28];   END_VAR
      Value := 'hi';
  END_FUNCTION

  PROGRAM mainProg
      VAR keyVar : STRING[10]; outVar : STRING[28]; r : DINT; END_VAR
      keyVar := 'my-key';
      r := Config_GetString(keyVar, outVar);
      PRINTF('key=[%s]$N', keyVar); // will print 'key=[]', `keyVar` passed to VAR_INPUT `Key` was replaced by a temp
  END_PROGRAM

  FUNCTION main
      mainProg();
  END_FUNCTION

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.

Yeah... that was a terrible idea 😞 I've switch it to look at the annotation instead to get the correct parameter index. Only snag was that the lowerer could sometimes change the parameter indexes if a new parameter was inserted, so I added an explicit flag for that. This should cover all the bases (for now anyway).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants