Skip to content

Commit

Permalink
C library: implement {v,}dprintf
Browse files Browse the repository at this point in the history
The model implementation has no side effects, but will make sure that
the format string pointer can be dereferenced.
  • Loading branch information
tautschnig committed Mar 11, 2024
1 parent a8861ed commit 3ab9c21
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
9 changes: 9 additions & 0 deletions regression/cbmc-library/dprintf-01/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <assert.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
dprintf(1, "some string %s: %d\n", argv[0], 42);
dprintf(1, "some other string\n");
return 0;
}
8 changes: 8 additions & 0 deletions regression/cbmc-library/dprintf-01/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--pointer-check --bounds-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
17 changes: 17 additions & 0 deletions regression/cbmc-library/vdprintf-01/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdarg.h>
#include <stdio.h>

int xdprintf(int fd, const char *format, ...)
{
va_list list;
va_start(list, format);
int result = vdprintf(fd, format, list);
va_end(list);
return result;
}

int main()
{
xdprintf(1, "%d\n", 42);
return 0;
}
8 changes: 8 additions & 0 deletions regression/cbmc-library/vdprintf-01/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--pointer-check --bounds-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
49 changes: 49 additions & 0 deletions src/ansi-c/library/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,55 @@ __CPROVER_HIDE:;
return result;
}

/* FUNCTION: dprintf */

#ifndef __CPROVER_STDIO_H_INCLUDED
# include <stdio.h>
# define __CPROVER_STDIO_H_INCLUDED
#endif

#ifndef __CPROVER_STDARG_H_INCLUDED
# include <stdarg.h>
# define __CPROVER_STDARG_H_INCLUDED
#endif

int dprintf(int fd, const char *restrict format, ...)
{
__CPROVER_HIDE:;
va_list list;
va_start(list, format);
int result = dprintf(fd, format, list);
va_end(list);
return result;
}

/* FUNCTION: vdprintf */

#ifndef __CPROVER_STDIO_H_INCLUDED
# include <stdio.h>
# define __CPROVER_STDIO_H_INCLUDED
#endif

#ifndef __CPROVER_STDARG_H_INCLUDED
# include <stdarg.h>
# define __CPROVER_STDARG_H_INCLUDED
#endif

int __VERIFIER_nondet_int(void);

int vdprintf(int fd, const char *restrict format, va_list arg)
{
__CPROVER_HIDE:;

int result = __VERIFIER_nondet_int();

(void)fd;
(void)*format;
(void)arg;

return result;
}

/* FUNCTION: vasprintf */

#ifndef __CPROVER_STDIO_H_INCLUDED
Expand Down

0 comments on commit 3ab9c21

Please sign in to comment.