Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit a639a2c

Browse files
author
Andrew Tridgell
committed
px4io: prevent use of uninitialised memory in io_set_arming_state()
the vehicle may not have setup a control_mode. We need to check the return of orb_copy() to ensure we are getting initialised values
1 parent 4d20714 commit a639a2c

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

src/drivers/px4io/px4io.cpp

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,52 +1160,54 @@ PX4IO::io_set_arming_state()
11601160
actuator_armed_s armed; ///< system armed state
11611161
vehicle_control_mode_s control_mode; ///< vehicle_control_mode
11621162

1163-
orb_copy(ORB_ID(actuator_armed), _t_actuator_armed, &armed);
1164-
orb_copy(ORB_ID(vehicle_control_mode), _t_vehicle_control_mode, &control_mode);
1163+
int have_armed = orb_copy(ORB_ID(actuator_armed), _t_actuator_armed, &armed);
1164+
int have_control_mode = orb_copy(ORB_ID(vehicle_control_mode), _t_vehicle_control_mode, &control_mode);
11651165

11661166
uint16_t set = 0;
11671167
uint16_t clear = 0;
11681168

1169-
if (armed.armed) {
1170-
set |= PX4IO_P_SETUP_ARMING_FMU_ARMED;
1171-
1172-
} else {
1173-
clear |= PX4IO_P_SETUP_ARMING_FMU_ARMED;
1174-
}
1175-
1176-
if (armed.lockdown && !_lockdown_override) {
1177-
set |= PX4IO_P_SETUP_ARMING_LOCKDOWN;
1178-
} else {
1179-
clear |= PX4IO_P_SETUP_ARMING_LOCKDOWN;
1180-
}
1169+
if (have_armed == OK) {
1170+
if (armed.armed) {
1171+
set |= PX4IO_P_SETUP_ARMING_FMU_ARMED;
1172+
} else {
1173+
clear |= PX4IO_P_SETUP_ARMING_FMU_ARMED;
1174+
}
11811175

1182-
/* Do not set failsafe if circuit breaker is enabled */
1183-
if (armed.force_failsafe && !_cb_flighttermination) {
1184-
set |= PX4IO_P_SETUP_ARMING_FORCE_FAILSAFE;
1185-
} else {
1186-
clear |= PX4IO_P_SETUP_ARMING_FORCE_FAILSAFE;
1187-
}
1176+
if (armed.lockdown && !_lockdown_override) {
1177+
set |= PX4IO_P_SETUP_ARMING_LOCKDOWN;
1178+
} else {
1179+
clear |= PX4IO_P_SETUP_ARMING_LOCKDOWN;
1180+
}
11881181

1189-
// XXX this is for future support in the commander
1190-
// but can be removed if unneeded
1191-
// if (armed.termination_failsafe) {
1192-
// set |= PX4IO_P_SETUP_ARMING_TERMINATION_FAILSAFE;
1193-
// } else {
1194-
// clear |= PX4IO_P_SETUP_ARMING_TERMINATION_FAILSAFE;
1195-
// }
1182+
/* Do not set failsafe if circuit breaker is enabled */
1183+
if (armed.force_failsafe && !_cb_flighttermination) {
1184+
set |= PX4IO_P_SETUP_ARMING_FORCE_FAILSAFE;
1185+
} else {
1186+
clear |= PX4IO_P_SETUP_ARMING_FORCE_FAILSAFE;
1187+
}
11961188

1197-
if (armed.ready_to_arm) {
1198-
set |= PX4IO_P_SETUP_ARMING_IO_ARM_OK;
1189+
// XXX this is for future support in the commander
1190+
// but can be removed if unneeded
1191+
// if (armed.termination_failsafe) {
1192+
// set |= PX4IO_P_SETUP_ARMING_TERMINATION_FAILSAFE;
1193+
// } else {
1194+
// clear |= PX4IO_P_SETUP_ARMING_TERMINATION_FAILSAFE;
1195+
// }
11991196

1200-
} else {
1201-
clear |= PX4IO_P_SETUP_ARMING_IO_ARM_OK;
1197+
if (armed.ready_to_arm) {
1198+
set |= PX4IO_P_SETUP_ARMING_IO_ARM_OK;
1199+
1200+
} else {
1201+
clear |= PX4IO_P_SETUP_ARMING_IO_ARM_OK;
1202+
}
12021203
}
12031204

1204-
if (control_mode.flag_external_manual_override_ok) {
1205-
set |= PX4IO_P_SETUP_ARMING_MANUAL_OVERRIDE_OK;
1206-
1207-
} else {
1208-
clear |= PX4IO_P_SETUP_ARMING_MANUAL_OVERRIDE_OK;
1205+
if (have_control_mode == OK) {
1206+
if (control_mode.flag_external_manual_override_ok) {
1207+
set |= PX4IO_P_SETUP_ARMING_MANUAL_OVERRIDE_OK;
1208+
} else {
1209+
clear |= PX4IO_P_SETUP_ARMING_MANUAL_OVERRIDE_OK;
1210+
}
12091211
}
12101212

12111213
return io_reg_modify(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_ARMING, clear, set);

0 commit comments

Comments
 (0)