Skip to content

Union: strdup instead of fixed-size buffers for name-list strings - #3166

Open
willend wants to merge 1 commit into
mainfrom
union-target-string-overflow
Open

willend wants to merge 1 commit into
mainfrom
union-target-string-overflow

Conversation

@willend

@willend willend commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Free-form text area

Please describe what your PR is adding in terms of features or bugfixes:

Fixes #2788. The functions that split comma-separated name lists (target_loggers, process_string etc.) copied the input into fixed 124-1024 byte stack buffers with strcpy. A long list overflowed the stack (SIGTRAP on macOS, silent stack corruption elsewhere).

The same pattern was in 33 functions in 22 files, not just the two conditionals named in the issue: Union_conditional_PSD/_standard, all loggers and abs-loggers, Union_make_material (McStas and McXtrace), Union_NCrystal_material and share/union-lib.c. Each now uses strdup and frees before every return. A copy inside a commented-out block in Union_conditional_standard is left alone.

The edit was done with a script with a dry-run mode; the dry-run diff was reviewed before applying.

Numerical behaviour: none. Same-seed runs (-n 1e5 -s 42) give identical detector output to main for Conditional_test, Logger_test, Test_abs_logger_event and Test_abs_logger_nD_scintillator. The reproducer from #2788 crashes on main and runs on this branch.


Declaration of use of AI-tools

  • Please add a checkmark here if you used AI-tools during the work for this contribution
  • Further, please describe how / where and for what the tools were used: Claude Code (Claude Opus 5.5) found the affected functions, wrote and ran the replacement script and the comparison runs, reviewed by the author.
  • AI-assisted commits must carry a Co-Authored-By line naming the tool, see CONTRIBUTING.md

Development OS / boundary conditions

Please describe what OS you developed and tested your additions on, and if any special dependencies are required:

macOS arm64, mccode-dev conda env. Not tested on Linux, Windows or GPU.


PR Checklist for contributing to McStas/McXtrace

  • My contribution includes patches to an existing component file

    • I have ensured that basic use of the component is OK (e.g. an instrument using it compiles?)
    • I have used the mctest utility to test one or more instruments making use of the component (please attach mcviewtest report as screenshot in comments)
    • I have used the mccode-clangformat tool to apply the standard McCode component indentation scheme
  • My work touches / adds to the runtime lib code (.c,.h etc in multiple locations

    • I have added reasoning and documentation for the change below
    • Unit test: not standalone: union-lib.c needs the Union framework; covered by the Union test instruments above.
  • My PR is meant to fix a specific, existing issue


🤖 Generated with Claude Code

The functions that split comma-separated name lists (target_loggers,
process_string etc.) copied them into fixed 124-1024 byte stack buffers
with strcpy, so a long list overflowed the stack. Use strdup and free.
Fixes #2788.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@willend

willend commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

@mads-bertelsen @Lomholy: this fixes #2788. The Union functions that split comma-separated name lists (target_loggers, process_string etc.) copied the input into fixed 124-1024 byte stack buffers with strcpy, so a long list overflowed the stack.

The same pattern was in 33 functions in 22 files, not only the two conditionals: Union_conditional_PSD/_standard, all loggers and abs-loggers, Union_make_material (McStas and McXtrace), Union_NCrystal_material and share/union-lib.c. Each now uses strdup and frees before every return. No change in results: Conditional_test, Logger_test, Test_abs_logger_event and Test_abs_logger_nD_scintillator give identical output to main with the same seed, and the reproducer from #2788 now runs instead of crashing.

@mads-bertelsen, could you review?

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.

[ BUG ] Union_conditional_PSD / Union_conditional_standard: stack buffer overflow when target_loggers is longer than 511 characters

1 participant

⌂ Proxy