Skip to content

Commit

Permalink
Pass base pointer directly to CastShapeCollector::AddHit, avoiding ba…
Browse files Browse the repository at this point in the history
…d pointer UB
  • Loading branch information
LPGhatguy committed Oct 23, 2024
1 parent d756b66 commit aeaf6fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions JoltC/Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,13 @@ JPC_API void JPC_ObjectLayerPairFilter_delete(JPC_ObjectLayerPairFilter* object)
////////////////////////////////////////////////////////////////////////////////
// CastShapeCollector

typedef struct JPC_CastShapeCollector JPC_CastShapeCollector;

typedef struct JPC_CastShapeCollectorFns {
void (*Reset)(void *self);
void (*AddHit)(void *self, const JPC_ShapeCastResult *Result);
void (*AddHit)(void *self, JPC_CastShapeCollector *base, const JPC_ShapeCastResult *Result);
} JPC_CastShapeCollectorFns;

typedef struct JPC_CastShapeCollector JPC_CastShapeCollector;

JPC_API JPC_CastShapeCollector* JPC_CastShapeCollector_new(
void *self,
JPC_CastShapeCollectorFns fns);
Expand Down
12 changes: 8 additions & 4 deletions JoltC/JoltC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,31 +489,35 @@ JPC_API JPC_ObjectLayerPairFilter* JPC_ObjectLayerPairFilter_new(
////////////////////////////////////////////////////////////////////////////////
// JPC_CastShapeCollector

class JPC_CastShapeCollectorBridge;
OPAQUE_WRAPPER(JPC_CastShapeCollector, JPC_CastShapeCollectorBridge)

class JPC_CastShapeCollectorBridge final : public JPH::CastShapeCollector {
using ResultType = JPH::ShapeCastResult;

public:
explicit JPC_CastShapeCollectorBridge(void *self, JPC_CastShapeCollectorFns fns) : self(self), fns(fns) {}

virtual void Reset() override {
void Reset() override {
JPH::CastShapeCollector::Reset();

if (fns.Reset != nullptr) {
fns.Reset(self);
}
}

virtual void AddHit(const ResultType &inResult) override {
void AddHit(const ResultType &inResult) override {
JPC_ShapeCastResult result = to_jpc(inResult);
fns.AddHit(self, &result);
JPC_CastShapeCollector *base = to_jpc(this);

fns.AddHit(self, base, &result);
}

private:
void* self;
JPC_CastShapeCollectorFns fns;
};

OPAQUE_WRAPPER(JPC_CastShapeCollector, JPC_CastShapeCollectorBridge)
DESTRUCTOR(JPC_CastShapeCollector)

JPC_API JPC_CastShapeCollector* JPC_CastShapeCollector_new(
Expand Down

0 comments on commit aeaf6fe

Please sign in to comment.