C++ Logo

std-proposals

Advanced search

[std-proposals] Contracts: Observe semantic and multiple preconditions

From: <joegottman_at_[hidden]>
Date: Thu, 24 Aug 2023 21:51:59 +0000 (UTC)
I just read the most recent record of contract support (P2521R5) and I have a question about the new possible "observe semantic" for contracts. As I understand it, this means that after a contract violation the program invokes a contract violation handler that presumably logs the failure, and then continues. But what happens if there are two or more preconditions and the first one fails? Will the second precondition be checked? That could cause problems in the case of code like the following:
int foo(int *p) [[pre: p != nullptr]] [[pre: *p > 0]];
If foo(nullptr) is called the first precondition will fail and be logged. If the program then attempts to check the second precondition then it will attempt to dereference the null pointer and crash.
Joe Gottman

Received on 2023-08-24 21:52:02