Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ql/pricingengines/basket/fd2dblackscholesvanillaengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace QuantLib {
// 1. Payoff
const ext::shared_ptr<BasketPayoff> payoff =
ext::dynamic_pointer_cast<BasketPayoff>(arguments_.payoff);
QL_REQUIRE(payoff, "basket payoff expected");

// 2. Mesher
const Time maturity = p1_->time(arguments_.exercise->lastDate());
Expand Down
63 changes: 63 additions & 0 deletions test-suite/basketoption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3033,6 +3033,69 @@ BOOST_AUTO_TEST_CASE(testGaussianCopulaSpreadEngineParityAcrossVols) {
}
}

BOOST_AUTO_TEST_CASE(testFd2dBlackScholesVanillaEngineRejectsNonBasketPayoff) {
BOOST_TEST_MESSAGE(
"Testing Fd2dBlackScholesVanillaEngine's guard against a "
"non-basket payoff...");

// Fd2dBlackScholesVanillaEngine::calculate() dynamic_pointer_casts
// arguments_.payoff to BasketPayoff but, unlike its sibling
// FdNdimBlackScholesVanillaEngine (which QL_REQUIREs the cast to
// succeed), used to have no check at all: a null cast would silently
// flow into FdmLogBasketInnerValue::innerValue()'s unconditional
// (*payoff_)(x) dereference. That is a null-pointer dereference
// (undefined behavior), not a catchable exception, so it cannot safely
// be exercised pre-fix in an automated test -- this test instead
// demonstrates the fixed engine's behavior: driving it directly through
// its own PricingEngine::getArguments()/calculate() interface (the same
// entry point BasketOption itself uses, but without BasketOption's
// constructor-level type safety) with a non-basket payoff throws a
// clean QuantLib::Error instead.

DayCounter dc = Actual360();
Date today = Settings::instance().evaluationDate();

auto spot1 = ext::make_shared<SimpleQuote>(100.0);
auto spot2 = ext::make_shared<SimpleQuote>(100.0);
ext::shared_ptr<YieldTermStructure> qTS1 = flatRate(today, 0.0, dc);
ext::shared_ptr<YieldTermStructure> qTS2 = flatRate(today, 0.0, dc);
ext::shared_ptr<YieldTermStructure> rTS = flatRate(today, 0.05, dc);
ext::shared_ptr<BlackVolTermStructure> volTS1 = flatVol(today, 0.2, dc);
ext::shared_ptr<BlackVolTermStructure> volTS2 = flatVol(today, 0.2, dc);

auto p1 = ext::make_shared<BlackScholesMertonProcess>(
Handle<Quote>(spot1), Handle<YieldTermStructure>(qTS1),
Handle<YieldTermStructure>(rTS), Handle<BlackVolTermStructure>(volTS1));
auto p2 = ext::make_shared<BlackScholesMertonProcess>(
Handle<Quote>(spot2), Handle<YieldTermStructure>(qTS2),
Handle<YieldTermStructure>(rTS), Handle<BlackVolTermStructure>(volTS2));

auto exercise = ext::make_shared<EuropeanExercise>(today + Period(1, Years));

ext::shared_ptr<PricingEngine> engine =
ext::make_shared<Fd2dBlackScholesVanillaEngine>(p1, p2, 0.0, 20, 20, 10);

// Sanity check: a genuine basket payoff, through the normal BasketOption
// path, still prices fine with the fix in place.
BasketOption basketOption(
ext::make_shared<MinBasketPayoff>(
ext::make_shared<PlainVanillaPayoff>(Option::Call, 100.0)),
exercise);
basketOption.setPricingEngine(engine);
BOOST_CHECK_NO_THROW(basketOption.NPV());

// Now drive the same engine instance directly, bypassing BasketOption's
// constructor-level BasketPayoff requirement, with a plain (non-basket)
// payoff -- exactly the misuse the sibling N-dim engine's own guard
// exists to catch.
auto* arguments = dynamic_cast<Option::arguments*>(engine->getArguments());
BOOST_REQUIRE(arguments != nullptr);
arguments->payoff = ext::make_shared<PlainVanillaPayoff>(Option::Call, 100.0);
arguments->exercise = exercise;

BOOST_CHECK_THROW(engine->calculate(), Error);
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()