Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PM_TraceModel may return uninitialized trace #3283

Open
a1batross opened this issue Jun 26, 2022 · 4 comments
Open

PM_TraceModel may return uninitialized trace #3283

a1batross opened this issue Jun 26, 2022 · 4 comments

Comments

@a1batross
Copy link

Related issue: FWGS/xash3d-fwgs#885

In short, on some maps compiled with ZHLT it's possible that TraceModel may return uninitialized trace values, which then used in ladder code: https://github.com/ValveSoftware/halflife/blob/master/pm_shared/pm_shared.c#L2092

Following the execution, these uninitialized values may get interpreted as NaN, poisoning other float variables down to player's velocity: https://github.com/ValveSoftware/halflife/blob/master/pm_shared/pm_shared.c#L2173

Further execution gets to another trace function, this time PM_PlayerTrace: https://github.com/ValveSoftware/halflife/blob/master/pm_shared/pm_shared.c#L830 which then causes an infinite loop in engine's PM_HullPointContents.

The fix is simple: PM_TraceModel should initialize trace_t early, like this:

static inline void PM_InitTrace( trace_t *trace, const vec3_t end )
{
	memset( trace, 0, sizeof( *trace ));
	VectorCopy( end, trace->endpos );
	trace->allsolid = true;
	trace->fraction = 1.0f;
}

static float GAME_EXPORT pfnTraceModel( physent_t *pe, float *start, float *end, trace_t *trace )
{
	// variable declarations...
	PM_InitTrace( trace, end );
	...
}

This way result data will be interpreted as "didn't hit anything, stuck in solid", thus fraction = 1.0, allsolid = true and endpos as end point. On game code side, this can be fixed similarly, just initializing trace structure before any call to TraceModel.

Relevant fix in xash3d-fwgs source: FWGS/xash3d-fwgs@c076f4f and FWGS/xash3d-fwgs@85895c5

Thanks to @FreeSlave for finding a way to reproduce this bug and Unkle Mike for correct trace struct initialization.

@tschumann
Copy link

tschumann commented Jun 28, 2022 via email

@a1batross
Copy link
Author

@tschumann yes!

You may copy the PM_InitTrace function from the example above and call it before PM_TraceModel, assuming that it may not return an initialized trace.

@tschumann
Copy link

tschumann commented Oct 11, 2022 via email

@a1batross
Copy link
Author

@tschumann I don't remember now but at least this bug shouldn't happen with original compilers.

hammermaps added a commit to sohl-modders/Updated-SOHL-1.2 that referenced this issue Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants