Skip to content

Commit 25f5d3a

Browse files
authored
Merge pull request #8237 from tautschnig/features/asprintf
C library: model asprintf, test {v,}asprintf
2 parents f9f84f3 + 34fd782 commit 25f5d3a

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
4+
int asprintf(char **ptr, const char *fmt, ...);
5+
6+
int main()
7+
{
8+
char *result = NULL;
9+
int bytes = asprintf(&result, "%d\n", 42);
10+
if(bytes != -1)
11+
{
12+
assert(result[bytes] == 0);
13+
free(result);
14+
}
15+
return 0;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--pointer-check --bounds-check --unwind 10 --no-unwinding-assertions
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
+19-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
#include <assert.h>
2+
#include <stdarg.h>
23
#include <stdio.h>
34

5+
int vasprintf(char **ptr, const char *fmt, va_list ap);
6+
7+
int xasprintf(char **ptr, const char *format, ...)
8+
{
9+
va_list list;
10+
va_start(list, format);
11+
int result = vasprintf(ptr, format, list);
12+
va_end(list);
13+
return result;
14+
}
15+
416
int main()
517
{
6-
vasprintf();
7-
assert(0);
18+
char *result = NULL;
19+
int bytes = xasprintf(&result, "%d\n", 42);
20+
if(bytes != -1)
21+
{
22+
assert(result[bytes] == 0);
23+
free(result);
24+
}
825
return 0;
926
}

regression/cbmc-library/vasprintf-01/test.desc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
KNOWNBUG
1+
CORE
22
main.c
3-
--pointer-check --bounds-check --unwind 1 --unwinding-assertions
3+
--pointer-check --bounds-check --unwind 10 --no-unwinding-assertions
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$

src/ansi-c/library/stdio.c

+22
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,26 @@ __CPROVER_HIDE:;
15401540
return result;
15411541
}
15421542

1543+
/* FUNCTION: asprintf */
1544+
1545+
#ifndef __CPROVER_STDARG_H_INCLUDED
1546+
# include <stdarg.h>
1547+
# define __CPROVER_STDARG_H_INCLUDED
1548+
#endif
1549+
1550+
// declare here instead of relying on stdio.h as even those platforms that do
1551+
// have it at all may require _GNU_SOURCE to be set
1552+
int vasprintf(char **ptr, const char *fmt, va_list ap);
1553+
1554+
int asprintf(char **ptr, const char *fmt, ...)
1555+
{
1556+
va_list list;
1557+
va_start(list, fmt);
1558+
int result = vasprintf(ptr, fmt, list);
1559+
va_end(list);
1560+
return result;
1561+
}
1562+
15431563
/* FUNCTION: vasprintf */
15441564

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

15721592
*ptr=malloc(result_buffer_size);
1593+
if(!*ptr)
1594+
return -1;
15731595
int i=0;
15741596
for( ; i<result_buffer_size; ++i)
15751597
{

0 commit comments

Comments
 (0)