Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

C++ : updated kata to Clang8C++17 passed by function without body #820

Closed
g964 opened this issue May 28, 2020 · 12 comments
Closed

C++ : updated kata to Clang8C++17 passed by function without body #820

g964 opened this issue May 28, 2020 · 12 comments

Comments

@g964
Copy link

g964 commented May 28, 2020

@kazk
Could you have a look at: https://www.codewars.com/kata/566fc12495810954b1000030/discuss/cpp
post by @gkontadakis. Someone updated the kata to Clang8C++17.

I tried https://www.codewars.com/kata/5518a860a73e708c0a000027/train/cpp too with a function without body and this solution also passed.
Same with a few others I tried. All we get are "STDERR" but it get passed.

I don't understand what happens:-(

@kazk kazk transferred this issue from codewars/codewars.com May 28, 2020
@kazk
Copy link
Member

kazk commented May 28, 2020

Looks like the -O2 flag that was added is messing with the test framework. If I remove that flag, the test fails the same way as C++14 version with exit code 132 (Illegal Instruction).

For "Last digit of a huge number", solution without body crashes in random tests (segfaults).

@g964
Copy link
Author

g964 commented May 28, 2020

For "Last digit of a huge number", solution without body crashes in random tests (segfaults).

But passes "TEST".

@kazk
Copy link
Member

kazk commented May 28, 2020

I know, I'm just listing my observations. It seems to be caused by -O2 flag like I wrote above.

@error256
Copy link

Not returning a value from a function is undefined behavior, which gives the compiler freedom to do anything, not necessarily to return an unspecified value, so, apparently, the function has been optimized to always return what is expected (and Clang is known for sophisticated optimization as in famous https://kristerw.blogspot.com/2017/09/why-undefined-behavior-may-call-never.html). So it's not like something is wrong with the framework. I'm not sure what can be done about it though...

@g964
Copy link
Author

g964 commented May 28, 2020

Amongst the kata I tried some pass and some rightly fail.

@kazk
Copy link
Member

kazk commented May 28, 2020

Yeah, you usually don't have optimization flag on while testing/developing. Most languages on Codewars doesn't have optimization/production flag on.

@g964 Why not just provide valid initial solution?

#include <list>

using namespace std;
int last_digit(list<int> array) {
  // Write your code here
  return 0; // <- added
}

This won't prevent users submitting by removing that line, but it should prevent accidental ones.

@error256
Copy link

error256 commented May 28, 2020

I think a wrapper with noinline may work, like

[[gnu::noinline]]
int last_digit_wrapper(list<int> array) {
  return last_digit(array);
}

It seems that it works even without noinline in this kata, but it's probably less reliable.

@kazk
Copy link
Member

kazk commented May 28, 2020

Amongst the kata I tried some pass and some rightly fail.

Maybe those that fails have dotest wrapper that you often use?

@error256
Copy link

https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
Compiling with -fsanitize=return should help too.

@kazk
Copy link
Member

kazk commented May 28, 2020

@error256 Nice find! The kata fails with

main.cpp:9:5: runtime error: execution reached the end of a value-returning function without returning a value

I still think a valid initial solution returning the empty value should be provided though. So add return {};/return 0;/return std::string(); whatever that makes sense.

@kazk
Copy link
Member

kazk commented Jun 1, 2020

Deployed the fix.

@kazk kazk closed this as completed Jun 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants