Browse Source

Pass base pointer directly to CastShapeCollector::AddHit, avoiding bad pointer UB

Lucien Greathouse 11 months ago
parent
commit
aeaf6fe278
2 changed files with 11 additions and 7 deletions
  1. 3 3
      JoltC/Functions.h
  2. 8 4
      JoltC/JoltC.cpp

+ 3 - 3
JoltC/Functions.h

@@ -313,13 +313,13 @@ JPC_API void JPC_ObjectLayerPairFilter_delete(JPC_ObjectLayerPairFilter* object)
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
 // CastShapeCollector
 // CastShapeCollector
 
 
+typedef struct JPC_CastShapeCollector JPC_CastShapeCollector;
+
 typedef struct JPC_CastShapeCollectorFns {
 typedef struct JPC_CastShapeCollectorFns {
 	void (*Reset)(void *self);
 	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;
 } JPC_CastShapeCollectorFns;
 
 
-typedef struct JPC_CastShapeCollector JPC_CastShapeCollector;
-
 JPC_API JPC_CastShapeCollector* JPC_CastShapeCollector_new(
 JPC_API JPC_CastShapeCollector* JPC_CastShapeCollector_new(
 	void *self,
 	void *self,
 	JPC_CastShapeCollectorFns fns);
 	JPC_CastShapeCollectorFns fns);

+ 8 - 4
JoltC/JoltC.cpp

@@ -489,13 +489,16 @@ JPC_API JPC_ObjectLayerPairFilter* JPC_ObjectLayerPairFilter_new(
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
 // JPC_CastShapeCollector
 // JPC_CastShapeCollector
 
 
+class JPC_CastShapeCollectorBridge;
+OPAQUE_WRAPPER(JPC_CastShapeCollector, JPC_CastShapeCollectorBridge)
+
 class JPC_CastShapeCollectorBridge final : public JPH::CastShapeCollector {
 class JPC_CastShapeCollectorBridge final : public JPH::CastShapeCollector {
 	using ResultType = JPH::ShapeCastResult;
 	using ResultType = JPH::ShapeCastResult;
 
 
 public:
 public:
 	explicit JPC_CastShapeCollectorBridge(void *self, JPC_CastShapeCollectorFns fns) : self(self), fns(fns) {}
 	explicit JPC_CastShapeCollectorBridge(void *self, JPC_CastShapeCollectorFns fns) : self(self), fns(fns) {}
 
 
-	virtual void Reset() override {
+	void Reset() override {
 		JPH::CastShapeCollector::Reset();
 		JPH::CastShapeCollector::Reset();
 
 
 		if (fns.Reset != nullptr) {
 		if (fns.Reset != nullptr) {
@@ -503,9 +506,11 @@ public:
 		}
 		}
 	}
 	}
 
 
-	virtual void AddHit(const ResultType &inResult) override {
+	void AddHit(const ResultType &inResult) override {
 		JPC_ShapeCastResult result = to_jpc(inResult);
 		JPC_ShapeCastResult result = to_jpc(inResult);
-		fns.AddHit(self, &result);
+		JPC_CastShapeCollector *base = to_jpc(this);
+
+		fns.AddHit(self, base, &result);
 	}
 	}
 
 
 private:
 private:
@@ -513,7 +518,6 @@ private:
 	JPC_CastShapeCollectorFns fns;
 	JPC_CastShapeCollectorFns fns;
 };
 };
 
 
-OPAQUE_WRAPPER(JPC_CastShapeCollector, JPC_CastShapeCollectorBridge)
 DESTRUCTOR(JPC_CastShapeCollector)
 DESTRUCTOR(JPC_CastShapeCollector)
 
 
 JPC_API JPC_CastShapeCollector* JPC_CastShapeCollector_new(
 JPC_API JPC_CastShapeCollector* JPC_CastShapeCollector_new(