Skip to content

Commit ef32252

Browse files
committed
C library: model asprintf, test {v,}asprintf
asprintf can be modelled by via the existing vasprintf. Make sure both are covered by tests.
1 parent a8861ed commit ef32252

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
4+
int main()
5+
{
6+
char *result = NULL;
7+
int bytes = asprintf(&result, "%d\n", 42);
8+
if(bytes != -1)
9+
{
10+
assert(result[bytes] == 0);
11+
free(result);
12+
}
13+
return 0;
14+
}
Lines changed: 8 additions & 0 deletions
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
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
#include <assert.h>
2+
#include <stdarg.h>
23
#include <stdio.h>
34

5+
int xasprintf(char **ptr, const char *format, ...)
6+
{
7+
va_list list;
8+
va_start(list, format);
9+
int result = vasprintf(ptr, format, list);
10+
va_end(list);
11+
return result;
12+
}
13+
414
int main()
515
{
6-
vasprintf();
7-
assert(0);
16+
char *result = NULL;
17+
int bytes = xasprintf(&result, "%d\n", 42);
18+
if(bytes != -1)
19+
{
20+
assert(result[bytes] == 0);
21+
free(result);
22+
}
823
return 0;
924
}

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,27 @@ __CPROVER_HIDE:;
15401540
return result;
15411541
}
15421542

1543+
/* FUNCTION: asprintf */
1544+
1545+
#ifndef __CPROVER_STDIO_H_INCLUDED
1546+
# include <stdio.h>
1547+
# define __CPROVER_STDIO_H_INCLUDED
1548+
#endif
1549+
1550+
#ifndef __CPROVER_STDARG_H_INCLUDED
1551+
# include <stdarg.h>
1552+
# define __CPROVER_STDARG_H_INCLUDED
1553+
#endif
1554+
1555+
int asprintf(char **ptr, const char *fmt, ...)
1556+
{
1557+
va_list list;
1558+
va_start(list, fmt);
1559+
int result = vasprintf(ptr, fmt, list);
1560+
va_end(list);
1561+
return result;
1562+
}
1563+
15431564
/* FUNCTION: vasprintf */
15441565

15451566
#ifndef __CPROVER_STDIO_H_INCLUDED
@@ -1570,6 +1591,8 @@ int vasprintf(char **ptr, const char *fmt, va_list ap)
15701591
return -1;
15711592

15721593
*ptr=malloc(result_buffer_size);
1594+
if(!*ptr)
1595+
return -1;
15731596
int i=0;
15741597
for( ; i<result_buffer_size; ++i)
15751598
{

0 commit comments

Comments
 (0)