Description
In ciphers/morse_code.cpp, both char_to_morse and morse_to_char call std::exit(0) when they hit an unsupported input. A library/algorithm function should never terminate the caller's process — and terminating with exit code 0 (success) on an error is doubly wrong: a calling program or script sees "success" while the operation actually failed and the process was killed mid-run.
std::string char_to_morse(const char &c) {
switch (c) {
... // a..z, 0..9
default:
std::cerr << "Found invalid character: " << c << ' ' << std::endl;
std::exit(0); // <-- kills the whole program, with SUCCESS code
}
}
char morse_to_char(const std::string &s) {
...
} else {
std::cerr << "Found invalid Morse code: " << s << ' ' << std::endl;
std::exit(0); // <-- same here
}
}
Any character outside [a-z0-9] — a space, punctuation, or an uppercase letter — makes encrypt() abort the entire process. Since encrypt appends a space-separated code per character, multi-word text (e.g. "hello world") cannot be encoded at all: the space silently kills the program.
Steps to reproduce (verified with clang++ -std=c++17)
int main() {
std::cout << "before encrypt\n";
std::string e = ciphers::morse::encrypt("hello world"); // contains a space
std::cout << "after encrypt: [" << e << "]\n"; // never reached
return 42;
}
Output:
before encrypt
Found invalid character:
Process exit code: 0 — "after encrypt" is never printed and return 42 never runs. The program was terminated by std::exit(0) from inside char_to_morse, reporting success.
Expected behavior
char_to_morse / morse_to_char should signal the error to the caller without killing the process — e.g. throw std::invalid_argument, or return a sentinel/std::optional — so the caller can handle unsupported characters. encrypt should be able to process (or explicitly reject) text containing spaces/word boundaries.
Actual behavior
The functions call std::exit(0), terminating the host process with a success exit code on invalid input. Any program embedding this code dies silently (and "successfully") on the first unsupported character.
Suggested fix
Replace std::exit(0) with throw std::invalid_argument(...) (or return std::optional/an error). At minimum, if exiting must be kept for the demo, use a non-zero code (e.g. EXIT_FAILURE) — but a library function should not exit at all.
Description
In
ciphers/morse_code.cpp, bothchar_to_morseandmorse_to_charcallstd::exit(0)when they hit an unsupported input. A library/algorithm function should never terminate the caller's process — and terminating with exit code 0 (success) on an error is doubly wrong: a calling program or script sees "success" while the operation actually failed and the process was killed mid-run.Any character outside
[a-z0-9]— a space, punctuation, or an uppercase letter — makesencrypt()abort the entire process. Sinceencryptappends a space-separated code per character, multi-word text (e.g."hello world") cannot be encoded at all: the space silently kills the program.Steps to reproduce (verified with clang++ -std=c++17)
Output:
Process exit code: 0 —
"after encrypt"is never printed andreturn 42never runs. The program was terminated bystd::exit(0)from insidechar_to_morse, reporting success.Expected behavior
char_to_morse/morse_to_charshould signal the error to the caller without killing the process — e.g. throwstd::invalid_argument, or return a sentinel/std::optional— so the caller can handle unsupported characters.encryptshould be able to process (or explicitly reject) text containing spaces/word boundaries.Actual behavior
The functions call
std::exit(0), terminating the host process with a success exit code on invalid input. Any program embedding this code dies silently (and "successfully") on the first unsupported character.Suggested fix
Replace
std::exit(0)withthrow std::invalid_argument(...)(or returnstd::optional/an error). At minimum, if exiting must be kept for the demo, use a non-zero code (e.g.EXIT_FAILURE) — but a library function should not exit at all.