Skip to content

Commit 8acf339

Browse files
committed
A few minor improvements
Thanks to Thomas Baar.
1 parent 2eea0de commit 8acf339

File tree

1 file changed

+57
-5
lines changed

1 file changed

+57
-5
lines changed

tutorial.tex

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4538,6 +4538,8 @@ \section{Arrays of Pointers}\label{section:arrays-of-pointers}
45384538

45394539
\section{Solutions to Exercises}
45404540

4541+
Note: all of the solutions are also available in directory \verb|tutorial_solutions| of the VeriFast distribution.
4542+
45414543
\subsection{Exercise \ref{exercise:symex-tree}}
45424544

45434545
See Figure~\ref{fig:symex-tree}. Note: there are many alternative ways to express this symbolic execution tree that are equivalent. In particular, replacing a symbolic state by an equivalent one yields an equivalent symbolic execution tree. Two symbolic states are equivalent if they denote the same set of concrete states. For example, if there is only one value $v$ for some symbol $\varsigma$ that satisfies the assumptions, then replacing any occurrence of $\varsigma$ by $v$ yields an equivalent symbolic state. For another example, if a symbol is not mentioned by the assumptions, the heap chunks or the local variable bindings, then removing it from the set of used symbols yields an equivalent symbolic state.
@@ -6193,13 +6195,63 @@ \subsection{Exercise \ref{exercise:leaking}:
61936195

61946196
\subsection{Exercise \ref{exercise:characters}: characters.c}\label{solution:characters}
61956197

6196-
The solution is given in the text of Section~\ref{section:arrays}.
6198+
\begin{lstlisting}
6199+
char getchar(); /*@ requires true; @*/ /*@ ensures true; @*/
6200+
void putchar(char c); /*@ requires true; @*/ /*@ ensures true; @*/
6201+
6202+
/*@
6203+
predicate characters(char *start, int count) =
6204+
count <= 0 ? true : character(start, _) &*& characters(start + 1, count - 1);
6205+
@*/
6206+
6207+
char *malloc(int count);
6208+
//@ requires true;
6209+
//@ ensures characters(result, count);
6210+
6211+
void getchars(char *start, int count)
6212+
//@ requires characters(start, count);
6213+
//@ ensures characters(start, count);
6214+
{
6215+
if (count > 0) {
6216+
//@ open characters(start, count);
6217+
char c = getchar();
6218+
*start = c;
6219+
getchars(start + 1, count - 1);
6220+
//@ close characters(start, count);
6221+
}
6222+
}
6223+
6224+
void putchars(char *start, int count)
6225+
//@ requires characters(start, count);
6226+
//@ ensures characters(start, count);
6227+
{
6228+
if (count > 0) {
6229+
//@ open characters(start, count);
6230+
char c = *start;
6231+
putchar(c);
6232+
putchars(start + 1, count - 1);
6233+
//@ close characters(start, count);
6234+
}
6235+
}
6236+
6237+
int main()
6238+
//@ requires true;
6239+
//@ ensures true;
6240+
{
6241+
char *array = malloc(100);
6242+
getchars(array, 100);
6243+
putchars(array, 100);
6244+
putchars(array, 100);
6245+
//@ leak characters(array, 100);
6246+
return 0;
6247+
}
6248+
\end{lstlisting}
61976249

61986250
\subsection{Exercise \ref{exercise:xor}: xor.c}\label{solution:xor}
61996251

62006252
\begin{lstlisting}
6201-
char getchar(); /*@ requires true; *@/ /*@ ensures true; @*/
6202-
void putchar(char c); /*@ requires true; *@/ /*@ ensures true; @*/
6253+
char getchar(); /*@ requires true; @*/ /*@ ensures true; @*/
6254+
void putchar(char c); /*@ requires true; @*/ /*@ ensures true; @*/
62036255

62046256
/*@
62056257
predicate characters(char *start, int count) =
@@ -6266,8 +6318,8 @@ \subsection{Exercise \ref{exercise:xor}: xor.c}\label{solution:xor}
62666318
\subsection{Exercise \ref{exercise:characters_loop}: characters\_loop.c}\label{solution:characters_loop}
62676319

62686320
\begin{lstlisting}
6269-
char getchar(); /*@ requires true; *@/ /*@ ensures true; @*/
6270-
void putchar(char c); /*@ requires true; *@/ /*@ ensures true; @*/
6321+
char getchar(); /*@ requires true; @*/ /*@ ensures true; @*/
6322+
void putchar(char c); /*@ requires true; @*/ /*@ ensures true; @*/
62716323

62726324
/*@
62736325
predicate characters(char *start, int count) =

0 commit comments

Comments
 (0)