Unexpected zero return code from `throw std::runtime_error`
Csaba Raduly
rcsaba@gmail.com
Sat Jul 2 20:19:44 GMT 2022
On Sat, 2 Jul 2022 at 15:22, Adam Dinwoodie wrote:
>
> I'm currently experimenting with compiling rdfind for Cygwin, and one of
> the testcases is failing because std::runtime_error is expected to
> result in the compiled program exiting with a non-zero return code,
> but on Cygwin, it just seems to cause the program to terminate with a
> zero return code.
>
> I've attached a simple test case. Compare the output on Cygwin...
>
> $ ./test.sh
> + cat
> + g++ test.cc
> + [[ -x a.exe ]]
> + ./a.exe
> + rc=0
> + (( rc == 0 ))
> + echo 'Unexpected zero return code from execution'
> Unexpected zero return code from execution
> + exit 1
>
> ...with the output from one of my Debian boxes...
>
> $ ./test.sh
> + cat
> + g++ test.cc
> + [[ -x a.exe ]]
> + [[ -x a.out ]]
> + ./a.out
> terminate called after throwing an instance of 'std::runtime_error'
> what(): Test error
> ./test.sh: line 21: 566327 Aborted ./a.out
> + rc=134
> + (( rc == 0 ))
> + echo 'Expected non-zero return code received: 134'
> Expected non-zero return code received: 134
> + exit 0
>
> I'm not massively familiar with C++, so I could well be missing
> something obvious, but this seems like an unexpected difference between
> Cygwin and other *nix platforms. Is this a Cygwin bug, or am I doing
> something wrong?
Works just fine for me, unless I misunderstood something.
$ /cygdrive/c/Users/Csaba/Downloads/test.sh
+ cat
+ g++ test.cc
+ [[ -x a.exe ]]
+ ./a.exe
terminate called after throwing an instance of 'std::runtime_error'
what(): Test error
/cygdrive/c/Users/Csaba/Downloads/test.sh: line 21: 641 Aborted
(core dumped) ./a.exe
+ rc=134
+ (( rc == 0 ))
+ echo 'Expected non-zero return code received: 134'
Expected non-zero return code received: 134
+ exit 0
The if goes in the "else" case because rc is not 0, as it should be.
(shoudn't the condition be $rc == 0 ?)
After
$ diff -u /cygdrive/c/Users/Csaba/Downloads/test.sh test.sh
--- /cygdrive/c/Users/Csaba/Downloads/test.sh 2022-07-02
22:09:21.506377100 +0200
+++ test.sh 2022-07-02 22:15:08.670809700 +0200
@@ -20,7 +20,8 @@
exit 2
fi
-if (( rc == 0 )); then
+if [[ $rc == 0 ]]
+then
echo 'Unexpected zero return code from execution'
exit 1
else
I get
$ ./test.sh
+ cat
+ g++ test.cc
+ [[ -x a.exe ]]
+ ./a.exe
terminate called after throwing an instance of 'std::runtime_error'
what(): Test error
./test.sh: line 21: 660 Aborted (core dumped) ./a.exe
+ rc=134
+ [[ 134 == 0 ]]
+ echo 'Expected non-zero return code received: 134'
Expected non-zero return code received: 134
+ exit 0
Csaba
--
You can get very substantial performance improvements
by not doing the right thing. - Scott Meyers, An Effective C++11/14 Sampler
So if you're looking for a completely portable, 100% standards-conformant way
to get the wrong information: this is what you want. - Scott Meyers (C++TDaWYK)
More information about the Cygwin
mailing list