Skip to content

Commit 6c7647b

Browse files
committed
C library: model getpwnam, getpwuid
Modelling these, even with crude abstractions, helps avoid spurious counterexamples resulting from invalid pointers.
1 parent e02fd74 commit 6c7647b

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <assert.h>
2+
3+
#ifndef _WIN32
4+
# include <pwd.h>
5+
6+
int main()
7+
{
8+
const char *user = "user";
9+
struct passwd *result = getpwnam(user);
10+
(void)*result;
11+
return 0;
12+
}
13+
#else
14+
int main()
15+
{
16+
}
17+
#endif
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
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <assert.h>
2+
3+
#ifndef _WIN32
4+
# include <pwd.h>
5+
6+
int main()
7+
{
8+
struct passwd *result = getpwuid(0);
9+
(void)*result;
10+
return 0;
11+
}
12+
#else
13+
int main()
14+
{
15+
}
16+
#endif
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
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/ansi-c/library/pwd.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* FUNCTION: getpwnam */
2+
3+
#ifndef _WIN32
4+
5+
# ifndef __CPROVER_PWD_H_INCLUDED
6+
# include <pwd.h>
7+
# define __CPROVER_PWD_H_INCLUDED
8+
# endif
9+
10+
unsigned __VERIFIER_nondet_unsigned(void);
11+
12+
struct passwd __CPROVER_passwd;
13+
14+
struct passwd *getpwnam(const char *name)
15+
{
16+
// make some fields non-null
17+
__CPROVER_passwd.pw_name = (char *)name;
18+
__CPROVER_passwd.pw_uid = __VERIFIER_nondet_unsigned();
19+
__CPROVER_passwd.pw_gid = __VERIFIER_nondet_unsigned();
20+
return &__CPROVER_passwd;
21+
}
22+
23+
#endif
24+
25+
/* FUNCTION: getpwuid */
26+
27+
#ifndef _WIN32
28+
29+
# ifndef __CPROVER_PWD_H_INCLUDED
30+
# include <pwd.h>
31+
# define __CPROVER_PWD_H_INCLUDED
32+
# endif
33+
34+
unsigned __VERIFIER_nondet_unsigned(void);
35+
36+
# ifndef LIBRARY_CHECK
37+
struct passwd __CPROVER_passwd;
38+
# endif
39+
40+
struct passwd *getpwuid(uid_t uid)
41+
{
42+
// make some fields non-null
43+
__CPROVER_passwd.pw_uid = uid;
44+
__CPROVER_passwd.pw_gid = __VERIFIER_nondet_unsigned();
45+
return &__CPROVER_passwd;
46+
}
47+
48+
#endif

0 commit comments

Comments
 (0)