mardi 17 avril 2018

Throwing Custom Exception in google test lead to stack smashing

I am writing unit tests in c++ using GTest and Bazel, and I keep on getting *** stack smashing detected ***

I was able to narrow down the bug using this simple example:

void throwIfNegative(int n) {
  if (n < 0)
    throw Util::ModelException("Negative value");
}

running the following test code with bazel test is passing successfully.

#include "gtest/gtest.h"

TEST(my_test, throwIfNegative) {
  try {
    throwIfNegative(2); // NOTHING HAPPENS
  } catch (...) {}
}

Changing the input param to a negative value lead to *** stack smashing detected *** and terminates the test:

#include "gtest/gtest.h"

TEST(my_test, throwIfNegative) {
  try {
    throwIfNegative(-2); // stack smashing detected!
  } catch (...) {}
}

If I switch my custom exception with anything else (throw 5 for example), the test passes, so I assume that the problem is with my custom exception, but I am using this exception in many different places and the test builds successfully.

Q: Does anyone here understands the source of this error?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire