You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As a follow-on to writing the spec for add_0.c, it would be nice to have a tutorial for how a function can call add(x, y). We've run into a similar issue, and I'm not sure how to write the code to fulfill add(x,y)'s require clause. It's like:
int anotherfunc(...){
...
//calculate x
//calculate y
if (???){ //Not sure what to put here.
add(x,y);
}
}
Remember that specifications are always defined/meaningful.
Both signed and unsigned integer addition in the specs are allowed to overflow, so we can take advantage of that.
This is what I came up with...
intadd(intx, inty)
/*@ requires (x >= 0i32 || y >= 0i32) implies x <= x + y && y <= x + y; x < 0i32 && y < 0i32 implies x + y <= x && x + y <= y; ensures return == x + y;@*/
{
returnx+y;
}
Isn't that spec wrong, e.g, when x is 1 and y is -3? The link has a proper check in the "Compliant Solution" subsection of "Addition". Consider providing an example spec based on that and mentioning the link in the tutorial to help others.
In the original question, I was actually asking what to put in the if (...) of the function "another_func" which is calling add. Copying from the coding standards guide, the IF statement is still not sufficient though:
Here's the self-contained code of a representative example of what we're trying to do.
Activity
dc-mak commentedon Dec 27, 2024
Remember that specifications are always defined/meaningful.
Both signed and unsigned integer addition in the specs are allowed to overflow, so we can take advantage of that.
This is what I came up with...
You could always try to just copy secure coding standards into specs, but I think that would be ugly
https://wiki.sei.cmu.edu/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow
r-ross commentedon Jan 3, 2025
Isn't that spec wrong, e.g, when x is 1 and y is -3? The link has a proper check in the "Compliant Solution" subsection of "Addition". Consider providing an example spec based on that and mentioning the link in the tutorial to help others.
dc-mak commentedon Jan 4, 2025
Please submit a PR if it's urgent.
lwli11 commentedon Jan 6, 2025
In the original question, I was actually asking what to put in the if (...) of the function "another_func" which is calling add. Copying from the coding standards guide, the IF statement is still not sufficient though:
Here's the self-contained code of a representative example of what we're trying to do.