| 1 |
/* See e.g. https://en.cppreference.com/w/c/string/byte/strncpy */ |
| 2 |
|
| 3 |
/* { dg-additional-options "-Wno-stringop-overflow" } */ |
| 4 |
/* { dg-additional-options "-fpermissive" { target c++ } } */ |
| 5 |
|
| 6 |
#include "../../gcc.dg/analyzer/analyzer-decls.h" |
| 7 |
|
| 8 |
typedef __SIZE_TYPE__ size_t; |
| 9 |
|
| 10 |
extern char *strncpy (char *dst, const char *src, size_t count); |
| 11 |
|
| 12 |
char * |
| 13 |
test_passthrough (char *dst, const char *src, size_t count) |
| 14 |
{ |
| 15 |
char *result = strncpy (dst, src, count); |
| 16 |
__analyzer_eval (result == dst); /* { dg-warning "TRUE" } */ |
| 17 |
return result; |
| 18 |
} |
| 19 |
|
| 20 |
char * |
| 21 |
test_null_dst (const char *src, size_t count) |
| 22 |
{ |
| 23 |
return strncpy (NULL, src, 42); /* { dg-warning "use of NULL where non-null expected" } */ |
| 24 |
} |
| 25 |
|
| 26 |
char * |
| 27 |
test_null_src (char *dst, size_t count) |
| 28 |
{ |
| 29 |
return strncpy (dst, NULL, 42); /* { dg-warning "use of NULL where non-null expected" } */ |
| 30 |
} |
| 31 |
|
| 32 |
void |
| 33 |
test_zero_fill (char *dst) |
| 34 |
{ |
| 35 |
strncpy (dst, "", 5); |
| 36 |
__analyzer_eval (dst[0] == '\0'); /* { dg-warning "TRUE" "correct" { xfail *-*-* } } */ |
| 37 |
/* { dg-bogus "UNKNOWN" "status quo" { xfail *-*-* } .-1 } */ |
| 38 |
__analyzer_eval (dst[1] == '\0'); /* { dg-warning "TRUE" "correct" { xfail *-*-* } } */ |
| 39 |
/* { dg-bogus "UNKNOWN" "status quo" { xfail *-*-* } .-1 } */ |
| 40 |
__analyzer_eval (dst[2] == '\0'); /* { dg-warning "TRUE" "correct" { xfail *-*-* } } */ |
| 41 |
/* { dg-bogus "UNKNOWN" "status quo" { xfail *-*-* } .-1 } */ |
| 42 |
__analyzer_eval (dst[3] == '\0'); /* { dg-warning "TRUE" "correct" { xfail *-*-* } } */ |
| 43 |
/* { dg-bogus "UNKNOWN" "status quo" { xfail *-*-* } .-1 } */ |
| 44 |
__analyzer_eval (dst[4] == '\0'); /* { dg-warning "TRUE" "correct" { xfail *-*-* } } */ |
| 45 |
/* { dg-bogus "UNKNOWN" "status quo" { xfail *-*-* } .-1 } */ |
| 46 |
__analyzer_eval (__analyzer_get_strlen (dst) == 0); /* { dg-warning "TRUE" } */ |
| 47 |
__analyzer_eval (__analyzer_get_strlen (dst + 1) == 0); /* { dg-warning "TRUE" } */ |
| 48 |
} |
| 49 |
|
| 50 |
char *test_unterminated_concrete_a (char *dst) |
| 51 |
{ |
| 52 |
char buf[3] = "abc"; /* { dg-warning "initializer-string for '\[^\n\]*' is too long" "" { target c++ } } */ |
| 53 |
/* Should be OK to copy nothing. */ |
| 54 |
return strncpy (dst, buf, 0); /* { dg-bogus "" } */ |
| 55 |
} |
| 56 |
|
| 57 |
char *test_unterminated_concrete_b (char *dst) |
| 58 |
{ |
| 59 |
char buf[3] = "abc"; /* { dg-warning "initializer-string for '\[^\n\]*' is too long" "" { target c++ } } */ |
| 60 |
/* Should be OK as the count limits the accesses to valid |
| 61 |
locations within src buf. */ |
| 62 |
return strncpy (dst, buf, 3); /* { dg-bogus "" } */ |
| 63 |
} |
| 64 |
|
| 65 |
char *test_unterminated_concrete_c (char *dst) |
| 66 |
{ |
| 67 |
char buf[3] = "abc"; /* { dg-warning "initializer-string for '\[^\n\]*' is too long" "" { target c++ } } */ |
| 68 |
/* Should warn: the count is one too high to limit the accesses |
| 69 |
to within src buf. */ |
| 70 |
return strncpy (dst, buf, 4); /* { dg-warning "stack-based buffer over-read" } */ |
| 71 |
} |
| 72 |
|
| 73 |
char *test_terminated_concrete_d (char *dst) |
| 74 |
{ |
| 75 |
char buf[6]; |
| 76 |
__builtin_memset (buf, 'a', 3); |
| 77 |
__builtin_memset (buf + 3, 'b', 3); |
| 78 |
|
| 79 |
/* Shouldn't warn. */ |
| 80 |
return strncpy (dst, buf, 6); /* { dg-bogus "" } */ |
| 81 |
} |
| 82 |
|
| 83 |
char *test_unterminated_concrete_e (char *dst) |
| 84 |
{ |
| 85 |
char buf[6]; |
| 86 |
__builtin_memset (buf, 'a', 3); |
| 87 |
__builtin_memset (buf + 3, 'b', 3); |
| 88 |
|
| 89 |
/* Should warn. */ |
| 90 |
return strncpy (dst, buf, 7); /* { dg-warning "stack-based buffer over-read" } */ |
| 91 |
} |
| 92 |
|
| 93 |
char *test_unterminated_symbolic (char *dst, size_t count) |
| 94 |
{ |
| 95 |
char buf[3] = "abc"; /* { dg-warning "initializer-string for '\[^\n\]*' is too long" "" { target c++ } } */ |
| 96 |
return strncpy (dst, buf, count); |
| 97 |
} |
| 98 |
|
| 99 |
char *test_terminated_symbolic (char *dst, size_t count) |
| 100 |
{ |
| 101 |
const char *src = "abc"; |
| 102 |
return strncpy (dst, src, count); /* { dg-bogus "" } */ |
| 103 |
} |
| 104 |
|
| 105 |
char *test_uninitialized_concrete_a (char *dst) |
| 106 |
{ |
| 107 |
char buf[16]; |
| 108 |
return strncpy (dst, buf, 0); /* { dg-bogus "" } */ |
| 109 |
} |
| 110 |
|
| 111 |
char *test_uninitialized_concrete_b (char *dst) |
| 112 |
{ |
| 113 |
char buf[16]; |
| 114 |
return strncpy (dst, buf, 1); /* { dg-warning "use of uninitialized value" } */ |
| 115 |
} |
| 116 |
|
| 117 |
char *test_initialized_concrete_c (char *dst) |
| 118 |
{ |
| 119 |
char buf[16]; |
| 120 |
buf[0] = 'a'; |
| 121 |
return strncpy (dst, buf, 1); /* { dg-bogus "" } */ |
| 122 |
} |
| 123 |
|
| 124 |
char *test_uninitialized_symbolic (char *dst, size_t count) |
| 125 |
{ |
| 126 |
char buf[16]; |
| 127 |
return strncpy (dst, buf, count); /* { dg-warning "use of uninitialized value" } */ |
| 128 |
} |
| 129 |
|
| 130 |
void test_truncation_1 (const char *src) |
| 131 |
{ |
| 132 |
char buf[16]; |
| 133 |
strncpy (buf, src, 16); |
| 134 |
/* buf might not be terminated (when strlen(src) > 16). */ |
| 135 |
__analyzer_get_strlen (buf); /* { dg-warning "stack-based buffer over-read" "" { xfail *-*-* } } */ |
| 136 |
} |
| 137 |
|
| 138 |
void test_truncation_2 (size_t count) |
| 139 |
{ |
| 140 |
char buf[16]; |
| 141 |
strncpy (buf, "abc", count); |
| 142 |
/* buf might not be terminated (when count <= 3). */ |
| 143 |
__analyzer_get_strlen (buf); /* { dg-warning "stack-based buffer over-read" "" { xfail *-*-* } } */ |
| 144 |
} |
| 145 |
|
| 146 |
void test_too_big_concrete (void) |
| 147 |
{ |
| 148 |
char buf[10]; |
| 149 |
strncpy (buf, "abc", 128); /* { dg-warning "stack-based buffer overflow" } */ |
| 150 |
} |
| 151 |
|
| 152 |
void test_too_big_symbolic (const char *src) |
| 153 |
{ |
| 154 |
char buf[10]; |
| 155 |
strncpy (buf, src, 128); /* { dg-warning "stack-based buffer overflow" } */ |
| 156 |
} |