Skip to content

Commit 6253abd

Browse files
authored
Merge pull request #6639 from tautschnig/bugfixes/getenv
C library/getenv test: actually try to dereference the result
2 parents 107ffa2 + a618231 commit 6253abd

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

regression/cbmc-library/getenv-01/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ int main()
55
char *something;
66
// there should not be any overflow in there
77
something = getenv("HOME");
8+
if(something != NULL)
9+
return something[0];
10+
else
11+
return 0;
812
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CORE
22
main.c
3-
--signed-overflow-check --unsigned-overflow-check --pointer-overflow-check --pointer-check --bounds-check
3+
--signed-overflow-check --unsigned-overflow-check --pointer-overflow-check --pointer-check --bounds-check --memory-leak-check
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$

src/ansi-c/library/stdlib.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,13 @@ inline long atol(const char *nptr)
415415

416416
#undef getenv
417417

418-
#ifndef __CPROVER_LIMITS_H_INCLUDED
419-
#include <limits.h>
420-
#define __CPROVER_LIMITS_H_INCLUDED
418+
#ifndef __CPROVER_STDDEF_H_INCLUDED
419+
# include <stddef.h>
420+
# define __CPROVER_STDDEF_H_INCLUDED
421421
#endif
422422

423423
__CPROVER_bool __VERIFIER_nondet___CPROVER_bool();
424-
__CPROVER_size_t __VERIFIER_nondet___CPROVER_size_t();
425-
426-
inline void *__builtin_alloca(__CPROVER_size_t alloca_size);
424+
ptrdiff_t __VERIFIER_nondet_ptrdiff_t();
427425

428426
inline char *getenv(const char *name)
429427
{
@@ -435,29 +433,35 @@ inline char *getenv(const char *name)
435433
"zero-termination of argument of getenv");
436434
#endif
437435

438-
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
436+
#ifdef __CPROVER_CUSTOM_BITVECTOR_ANALYSIS
439437
__CPROVER_event("invalidate_pointer", "getenv_result");
440438
char *getenv_result;
441439
__CPROVER_set_must(getenv_result, "getenv_result");
442440
return getenv_result;
443441

444-
#else
442+
#else
445443

446444
__CPROVER_bool found=__VERIFIER_nondet___CPROVER_bool();
447445
if(!found) return 0;
448446

449-
__CPROVER_size_t buf_size=__VERIFIER_nondet___CPROVER_size_t();
447+
ptrdiff_t buf_size = __VERIFIER_nondet_ptrdiff_t();
450448

451449
// It's reasonable to assume this won't exceed the signed
452450
// range in practice, but in principle, this could exceed
453451
// the range.
454452

455-
__CPROVER_assume(1<=buf_size && buf_size<=SSIZE_MAX);
456-
char *buffer=(char *)__builtin_alloca(buf_size);
453+
__CPROVER_assume(buf_size >= 1);
454+
char *buffer = (char *)__CPROVER_allocate(buf_size * sizeof(char), 0);
457455
buffer[buf_size-1]=0;
458456

457+
# ifdef __CPROVER_STRING_ABSTRACTION
458+
__CPROVER_assume(__CPROVER_buffer_size(buffer) == buf_size);
459+
__CPROVER_is_zero_string(buffer) = 1;
460+
__CPROVER_zero_string_length(buffer) = buf_size - 1;
461+
# endif
462+
459463
return buffer;
460-
#endif
464+
#endif
461465
}
462466

463467
/* FUNCTION: realloc */

0 commit comments

Comments
 (0)