Advertisement
Guest User

Untitled

a guest
Mar 8th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. The ACSL specification would be something like:
  2.  
  3. /*@
  4. requires valid_string(s);
  5. behavior good_number:
  6. assumes \exists integer n; string_is_number(s, n) && LONG_MIN <= n <= LONG_MAX;
  7. assigns \result;
  8. ensures \exists integer n; string_is_number{Pre}(s, n) && \result == n;
  9. behavior bad_number:
  10. assumes \forall integer n; ! string_is_number(s, n);
  11. assigns error;
  12. ensures error==1;
  13. */
  14. long str2long (const char * s);
  15.  
  16. The predicate valid_string is conveniently predefined :
  17.  
  18. /*@ predicate valid_string{L}(char *s) =
  19. 0 <= strlen(s) && \valid(s+(0..strlen(s)));
  20. */
  21.  
  22. (The predicate strlen is itself defined in a way such that it makes sense to use it as it is used here)
  23.  
  24. The predicate string_is_number would have to be defined by the specifier. Yes, it would be the same length as (and would like a) prolog implementation of that idea. On the plus side, the predicate could be re-used as-is for the specification of str2int and str2longlong.
  25.  
  26. In effect, when verifying an implementation of str2long against such a specification, you would in some sense be comparing an implementation with another. If there is the same bug in both, it might go unnoticed. But I have to point out that, in most programming languages, and especially in C, nothing prevents you to put together a function f() and a function g() that, while each fine in itself, do not work well together:
  27.  
  28. void f(int*p) // accepts NULL or a valid pointer to int
  29. {
  30. g(p);
  31. }
  32.  
  33. void g(int *p) // expects a valid pointer
  34. {
  35. *p = 1;
  36. }
  37.  
  38. By contrast, with ACSL specifications, the user would be warned when trying to put f() and g() together. Similarly, you my get predicate string_is_number wrong and thus manage to prove that a function that should be rejected, but hopefully you will catch that when str2long is used and the wrong definition for string_is_number prevents another property from being proved.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement