# Launching the missiles [closed]

The software for launching the missiles was designed so that both captain and co-captain had to press their LAUNCH button at the same time in order to trigger a launch. During testing it was discovered that if either operator pressed their LAUNCH button, the missiles would launch.

"I don't get it," said Barbara the software engineer as she looked at the code. "It clearly says AND:"

if (button1Voltage == HIGH && button2Voltage == HIGH) {
missileRelay.setVoltage(HIGH);
}
else {
missileRelay.setVoltage(LOW);
}


What was causing the bug?

• Maybe && is not AND but OR. – user18031 Jun 28 '16 at 7:41
• If && = OR, then the code is worthless, since any of it could mean anything. – JakeSteam Jun 28 '16 at 8:24
• After recent cutbacks, the military has been forced to use StackExchange for all debugging and Q/A of missile launch systems... – Travis Don Kindred Jun 28 '16 at 14:31

The system uses low-true logic, the button value is high only if it isn't pressed, and the missiles are only launched if voltage is low.

• If either of them press the button, both button voltage would be different. Isn't it? – Mohit Jain Jun 28 '16 at 5:44
• @MohitJain if either of them press the button, both voltages wouldn't be HIGH anymore, meaning it'd go to the else and launch the missile. – Quark Jun 28 '16 at 5:45
• This is what I thought too – Mario Garcia Jun 28 '16 at 8:43
• If so, it's a pretty dangerous missile system; if the power fails to the relay but not to the launching mechanism the missiles also launch :( (unless LOW is still a true voltage I guess) – Jonathan Allan Jun 28 '16 at 8:50
• If you invert all inputs and outputs of an and-gate, it is the same as an or-gate, which is basically what happens here. – Kruga Jun 28 '16 at 9:02

Surely in a true missile system...

you wouldn't set the voltage "low" as it could still invoke enough voltage to initiate launch. A more reliable solution would be to "ground" both firing lines using a dual line relay.

Also,

the inference of a "Captain" implies this is installed on a ship/platform? then it could be the movement of the metal hulled ship passing through the earths magnetic fields, inducing current into the superstructure without suitable degaussing in place.

Just sayin'

Another possible bug:

button1Voltage and button2Voltage are references to the same variable.

Which could look something like this in C++:

bool button1Voltage; bool &button2Voltage = button1Voltage; // so now setting either will set them both.

• That would not launch if either of them pressed button. – Edheldil Jun 28 '16 at 16:53
• @Edheldil just added some clarification. I know this is a huge assumption. – flu Jun 28 '16 at 17:03
• That isn't valid C; that's a C++ism. (You can achieve the same effect in C by using the preprocessor.) – Peregrine Rook Jun 28 '16 at 19:37

It is possible that

The values of button2Voltage and button1Voltage are not being reset so they remain HIGH once pressed.

The && operator is potentially

overloaded to the || operator, making it an OR instead of an AND

• Dear god I would certainly hope not – Business Cat Jun 28 '16 at 16:25

Perhaps the bug is

in the hardware rather than the software. The buttons have been wired up in parallel rather than in series, and the voltage measurements for both buttons are effectively measuring the same voltage.

Since this isn't the entire source code of the program, it's easy to contrive a scenario in which a bug has been introduced on a new line.

Take this one, for example:

button1Voltage = (button1Voltage == HIGH || button2Voltage == HIGH) ? HIGH : LOW;
button2Voltage = button1Voltage;
if (button1Voltage == HIGH && button2Voltage == HIGH) {
missileRelay.setVoltage(HIGH);
}
else {
missileRelay.setVoltage(LOW);
}


Maybe somewhere in the process the values are coerced into boolean?

const bool HIGH=true;
const bool LOW=false;

int main() {
bool a;
bool b;

a=-1;
b=1;

if (a==HIGH && b==HIGH) {
puts("boom!");
} else {
puts("...");
}
}


... or buttonVoltage has a type with == overloaded to do that coercion on its arguments

I was thinking the same as nikamed earlier, but there's not too much other code which would help us know that.

Maybe the missiles get launched when the voltage is set to LOW?

Another possibility is that

HIGH=2 and LOW=1 ... and LOW is good enough to launch :)

Or even better:

LOW=-1, HIGH=1 and any nonzero voltage will cause the launch

HIGH = 0; LOW = 1;

Edit:

To clarify, if the HIGH and LOW constants were accidentally set the wrong way around in the code, then the desired bug would appear. When neither button is pressed (both HIGH), then button1Voltage == HIGH && button2Voltage == HIGH, so the missile is set to HIGH i.e. not launched. If either or both buttons are pressed, the missile is launched.

.

This is not quite the same as Quark's answer, but has the same result.

• Can you explain why? – Beastly Gerbil Jun 28 '16 at 18:20
• ok, I've updated the answer – Zantier Jun 29 '16 at 9:06