Skip to content

Commit

Permalink
Merge pull request #8237 from tautschnig/features/asprintf
Browse files Browse the repository at this point in the history
C library: model asprintf, test {v,}asprintf
  • Loading branch information
kroening authored Apr 25, 2024
2 parents f9f84f3 + 34fd782 commit 25f5d3a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
16 changes: 16 additions & 0 deletions regression/cbmc-library/asprintf-01/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <assert.h>
#include <stdio.h>

int asprintf(char **ptr, const char *fmt, ...);

int main()
{
char *result = NULL;
int bytes = asprintf(&result, "%d\n", 42);
if(bytes != -1)
{
assert(result[bytes] == 0);
free(result);
}
return 0;
}
8 changes: 8 additions & 0 deletions regression/cbmc-library/asprintf-01/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--pointer-check --bounds-check --unwind 10 --no-unwinding-assertions
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
21 changes: 19 additions & 2 deletions regression/cbmc-library/vasprintf-01/main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>

int vasprintf(char **ptr, const char *fmt, va_list ap);

int xasprintf(char **ptr, const char *format, ...)
{
va_list list;
va_start(list, format);
int result = vasprintf(ptr, format, list);
va_end(list);
return result;
}

int main()
{
vasprintf();
assert(0);
char *result = NULL;
int bytes = xasprintf(&result, "%d\n", 42);
if(bytes != -1)
{
assert(result[bytes] == 0);
free(result);
}
return 0;
}
4 changes: 2 additions & 2 deletions regression/cbmc-library/vasprintf-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
KNOWNBUG
CORE
main.c
--pointer-check --bounds-check --unwind 1 --unwinding-assertions
--pointer-check --bounds-check --unwind 10 --no-unwinding-assertions
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
Expand Down
22 changes: 22 additions & 0 deletions src/ansi-c/library/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,26 @@ __CPROVER_HIDE:;
return result;
}

/* FUNCTION: asprintf */

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

// declare here instead of relying on stdio.h as even those platforms that do
// have it at all may require _GNU_SOURCE to be set
int vasprintf(char **ptr, const char *fmt, va_list ap);

int asprintf(char **ptr, const char *fmt, ...)
{
va_list list;
va_start(list, fmt);
int result = vasprintf(ptr, fmt, list);
va_end(list);
return result;
}

/* FUNCTION: vasprintf */

#ifndef __CPROVER_STDIO_H_INCLUDED
Expand Down Expand Up @@ -1570,6 +1590,8 @@ int vasprintf(char **ptr, const char *fmt, va_list ap)
return -1;

*ptr=malloc(result_buffer_size);
if(!*ptr)
return -1;
int i=0;
for( ; i<result_buffer_size; ++i)
{
Expand Down

0 comments on commit 25f5d3a

Please sign in to comment.