Skip to content

Pure-SoA particle type lacks compile-time guard that position components exist #5062

@WeiqunZhang

Description

@WeiqunZhang
  • Type: Correctness / API safety
  • Severity: Medium
  • Component: Pure-SoA particle container/type contracts
  • Location:
    • Src/Particle/AMReX_ParticleContainer.H:1562
    • Src/Particle/AMReX_ParticleTile.H:432

Problem

ParticleContainerPureSoA<T_NArrayReal,...> allows any T_NArrayReal, but pure-SoA code assumes the first AMREX_SPACEDIM real components are particle positions (e.g., \ SoAParticle::pos() directly indexes m_rdata[0..AMREX_SPACEDIM-1]).

There is no compile-time guard enforcing T_NArrayReal >= AMREX_SPACEDIM.

Impact

  • Invalid template instantiations compile but can perform out-of-bounds accesses at runtime.
  • Failure mode is silent UB rather than a clear compile-time error.

Suggested patch

Add a compile-time contract for pure-SoA instantiations.

--- a/Src/Particle/AMReX_ParticleContainer.H
+++ b/Src/Particle/AMReX_ParticleContainer.H
@@
 class ParticleContainer_impl : public ParticleContainerBase
 {
 public:
@@
+    static_assert(!ParticleType::is_soa_particle || T_NArrayReal >= AMREX_SPACEDIM,
+                  "Pure SoA particle containers require at least AMREX_SPACEDIM real components for positions");

(Equivalent static_assert in SoAParticle<T_NArrayReal,...> is also acceptable.)

Prepared by Codex

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions