Skip to content

C library: model asprintf, test {v,}asprintf #8237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading