Skip to content

Commit ca1098a

Browse files
Fix some Address Sanitizer errors (#384)
* fix some invalid memory accesses
1 parent e3a50e3 commit ca1098a

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

src/DETHRACE/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ target_include_directories(dethrace_obj
1010
pd
1111
)
1212

13-
# add_compile_options(-fsanitize=address)
14-
# add_link_options(-fsanitize=address)
13+
if (DETHRACE_ASAN)
14+
target_compile_options(dethrace_obj PUBLIC -fsanitize=address)
15+
target_link_options(dethrace_obj PUBLIC -fsanitize=address)
16+
endif()
1517

1618
target_link_libraries(dethrace_obj PUBLIC SDL2::SDL2 smackw32 harness BRender::Full BRender::DDI s3)
1719

18-
1920
if(MSVC)
2021
target_compile_definitions(dethrace_obj PRIVATE -D_CRT_SECURE_NO_WARNINGS)
2122
target_compile_options(dethrace_obj PRIVATE

src/DETHRACE/common/car.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,13 +2769,10 @@ int CollCheck(tCollision_info* c, br_scalar dt) {
27692769
br_matrix34 message_mat;
27702770
LOG_TRACE("(%p, %f)", c, dt);
27712771

2772-
tCar_spec* car_spec; // added for readability
2773-
27742772
// v34 = 0;
27752773
// v35 = 0;
27762774
// v36 = 0x3F800000;
27772775
// v48 = 0x3F800347;
2778-
car_spec = (tCar_spec*)c;
27792776
mat = &c->car_master_actor->t.t.mat;
27802777
oldmat = &c->oldmat;
27812778
k = 0;
@@ -3040,7 +3037,7 @@ int CollCheck(tCollision_info* c, br_scalar dt) {
30403037
BrVector3Set(&normal_force, 0.f, 0.f, 0.f);
30413038
BrVector3Set(&c->omega, 0.f, 0.f, 0.f);
30423039
BrVector3Set(&c->oldomega, 0.f, 0.f, 0.f);
3043-
if (c->driver <= eDriver_non_car || car_spec->max_force_rear == 0.0f) {
3040+
if (c->driver <= eDriver_non_car || CAR(c)->max_force_rear == 0.0f) {
30443041
if (c->driver <= eDriver_non_car) {
30453042
PipeSingleNonCar(c);
30463043
}
@@ -3060,17 +3057,23 @@ int CollCheck(tCollision_info* c, br_scalar dt) {
30603057
}
30613058
BrVector3Accumulate(&c->v, &norm);
30623059
if (c->driver >= eDriver_net_human) {
3063-
BrVector3Scale(&normal_force, &normal_force, gDefensive_powerup_factor[car_spec->power_up_levels[0]]);
3060+
BrVector3Scale(&normal_force, &normal_force, gDefensive_powerup_factor[CAR(c)->power_up_levels[0]]);
30643061
}
30653062
if (c->driver < eDriver_net_human) {
30663063
BrVector3Scale(&normal_force, &normal_force, 0.01f);
30673064
} else {
30683065
BrVector3Scale(&normal_force, &normal_force, 0.75f);
30693066
}
3070-
if (CAR(c)->invulnerable
3071-
|| (c->driver < eDriver_net_human && (c->driver != eDriver_oppo || PointOutOfSight(&c->pos, 150.0f)))
3072-
|| ((v_diff = (car_spec->pre_car_col_velocity.v[1] - c->v.v[1]) * gDefensive_powerup_factor[car_spec->power_up_levels[0]]) >= -20.0f)
3073-
|| CAR(c)->number_of_wheels_on_ground >= 3) {
3067+
if (
3068+
#if defined(DETHRACE_FIX_BUGS)
3069+
// `c` is only a `tCar_spec*` if the driver is an opponent or human, otherwise, it will be a `tNon_car_spec*`. The following code
3070+
// assumes `c` is a `tCar_spec*`, causing invalid memory accesses
3071+
c->driver >= eDriver_oppo &&
3072+
#endif
3073+
(CAR(c)->invulnerable
3074+
|| (c->driver < eDriver_net_human && (c->driver != eDriver_oppo || PointOutOfSight(&c->pos, 150.0f)))
3075+
|| ((v_diff = (CAR(c)->pre_car_col_velocity.v[1] - c->v.v[1]) * gDefensive_powerup_factor[CAR(c)->power_up_levels[0]]) >= -20.0f)
3076+
|| CAR(c)->number_of_wheels_on_ground >= 3)) {
30743077
CrushAndDamageCar(CAR(c), &dir, &normal_force, NULL);
30753078
} else {
30763079
// Cops Special Forces is always stolen if destroyed!
@@ -3079,12 +3082,19 @@ int CollCheck(tCollision_info* c, br_scalar dt) {
30793082
StealCar(CAR(c));
30803083
v_diff = v_diff * 5.0f;
30813084
}
3082-
for (i = 0; i < CAR(c)->car_actor_count; i++) {
3083-
ts2 = (v_diff + 20.0f) * -0.01f;
3084-
TotallySpamTheModel(CAR(c), i, CAR(c)->car_model_actors[i].actor, &CAR(c)->car_model_actors[i].crush_data, ts2);
3085-
}
3086-
for (i = 0; i < COUNT_OF(CAR(c)->damage_units); i++) {
3087-
DamageUnit(CAR(c), i, IRandomPosNeg(5) + (v_diff + 20.0f) * -1.5f);
3085+
#if defined(DETHRACE_FIX_BUGS)
3086+
// `c` is only a `tCar_spec*` if the driver is an opponent or human, otherwise, it will be a `tNon_car_spec*`. The following code
3087+
// assumes `c` is a `tCar_spec*`, causing invalid memory accesses
3088+
if (c->driver >= eDriver_oppo)
3089+
#endif
3090+
{
3091+
for (i = 0; i < CAR(c)->car_actor_count; i++) {
3092+
ts2 = (v_diff + 20.0f) * -0.01f;
3093+
TotallySpamTheModel(CAR(c), i, CAR(c)->car_model_actors[i].actor, &CAR(c)->car_model_actors[i].crush_data, ts2);
3094+
}
3095+
for (i = 0; i < COUNT_OF(CAR(c)->damage_units); i++) {
3096+
DamageUnit(CAR(c), i, IRandomPosNeg(5) + (v_diff + 20.0f) * -1.5f);
3097+
}
30883098
}
30893099
}
30903100
if (!noise_defeat) {
@@ -3094,7 +3104,7 @@ int CollCheck(tCollision_info* c, br_scalar dt) {
30943104
BrVector3InvScale(&tv, &tv, WORLD_SCALE);
30953105
BrMatrix34ApplyV(&bb, &tv, &c->car_master_actor->t.t.mat);
30963106
BrMatrix34ApplyV(&norm, &p_vel, &c->car_master_actor->t.t.mat);
3097-
CreateSparks(&pos, &bb, &norm, gCurrent_race.material_modifiers[gMaterial_index].sparkiness, car_spec);
3107+
CreateSparks(&pos, &bb, &norm, gCurrent_race.material_modifiers[gMaterial_index].sparkiness, CAR(c));
30983108
}
30993109
return k;
31003110
} else {

src/DETHRACE/common/spark.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2491,6 +2491,13 @@ void MungeSplash(tU32 pTime) {
24912491
if (!gAction_replay_mode || GetReplayRate() == 0.0) {
24922492
if (!gAction_replay_mode) {
24932493
for (i = 0; i < gNum_cars_and_non_cars; i++) {
2494+
#if defined(DETHRACE_FIX_BUGS)
2495+
// CreateSpash assumes a `tCar_spec*` argument. In the case a non-car is pushed into the water, a `tNon_car_spec*` is passed,
2496+
// causing invalid memory accesses
2497+
if (gActive_car_list[i]->driver < eDriver_oppo) {
2498+
continue;
2499+
}
2500+
#endif
24942501
if (gActive_car_list[i]->water_d != 10000.0 && gActive_car_list[i]->driver != eDriver_local_human) {
24952502
CreateSplash(gActive_car_list[i], pTime);
24962503
}
@@ -2516,7 +2523,7 @@ void MungeSplash(tU32 pTime) {
25162523
}
25172524
}
25182525
if (gProgram_state.current_car.water_d != 10000.0) {
2519-
CreateSplash(&gProgram_state.current_car, 0x64u);
2526+
CreateSplash(&gProgram_state.current_car, 100);
25202527
}
25212528
}
25222529
if (!gSplash_flags) {

0 commit comments

Comments
 (0)