History

Fix bounds checking new functions.

There was a problem with strncpy and strncmp. Made bound_ptr_add and bound_ptr_indir a little bit faster. Fix statistic counter types. Change long into long long.

herman ten brugge committed 6 years ago

Commit 56e70bf1 parent 39c0ff31 file · +61 −38Browse files

Unified · Split

lib/bcheck.c+61−38
@@ -165,35 +165,35 @@ static int never_fatal = 0; · 164 unmodified lines
165static int no_checking = 0;165static int no_checking = 0;
166 166
167#ifdef BOUND_STATISTIC167#ifdef BOUND_STATISTIC
168static unsigned long bound_ptr_add_count;168static unsigned long long bound_ptr_add_count;
169static unsigned long bound_ptr_indir1_count;169static unsigned long long bound_ptr_indir1_count;
170static unsigned long bound_ptr_indir2_count;170static unsigned long long bound_ptr_indir2_count;
171static unsigned long bound_ptr_indir4_count;171static unsigned long long bound_ptr_indir4_count;
172static unsigned long bound_ptr_indir8_count;172static unsigned long long bound_ptr_indir8_count;
173static unsigned long bound_ptr_indir12_count;173static unsigned long long bound_ptr_indir12_count;
174static unsigned long bound_ptr_indir16_count;174static unsigned long long bound_ptr_indir16_count;
175static unsigned long bound_local_new_count;175static unsigned long long bound_local_new_count;
176static unsigned long bound_local_delete_count;176static unsigned long long bound_local_delete_count;
177static unsigned long bound_malloc_count;177static unsigned long long bound_malloc_count;
178static unsigned long bound_calloc_count;178static unsigned long long bound_calloc_count;
179static unsigned long bound_realloc_count;179static unsigned long long bound_realloc_count;
180static unsigned long bound_free_count;180static unsigned long long bound_free_count;
181static unsigned long bound_memalign_count;181static unsigned long long bound_memalign_count;
182static unsigned long bound_mmap_count;182static unsigned long long bound_mmap_count;
183static unsigned long bound_munmap_count;183static unsigned long long bound_munmap_count;
184static unsigned long bound_alloca_count;184static unsigned long long bound_alloca_count;
185static unsigned long bound_mempcy_count;185static unsigned long long bound_mempcy_count;
186static unsigned long bound_memcmp_count;186static unsigned long long bound_memcmp_count;
187static unsigned long bound_memmove_count;187static unsigned long long bound_memmove_count;
188static unsigned long bound_memset_count;188static unsigned long long bound_memset_count;
189static unsigned long bound_strlen_count;189static unsigned long long bound_strlen_count;
190static unsigned long bound_strcpy_count;190static unsigned long long bound_strcpy_count;
191static unsigned long bound_strncpy_count;191static unsigned long long bound_strncpy_count;
192static unsigned long bound_strcmp_count;192static unsigned long long bound_strcmp_count;
193static unsigned long bound_strncmp_count;193static unsigned long long bound_strncmp_count;
194static unsigned long bound_strcat_count;194static unsigned long long bound_strcat_count;
195static unsigned long bound_strchr_count;195static unsigned long long bound_strchr_count;
196static unsigned long bound_strdup_count;196static unsigned long long bound_strdup_count;
197#define INCR_COUNT(x) x++197#define INCR_COUNT(x) x++
198#else198#else
199#define INCR_COUNT(x)199#define INCR_COUNT(x)
@@ -235,8 +235,12 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset) · 35 unmodified lines
235 WAIT_SEM ();235 WAIT_SEM ();
236 INCR_COUNT(bound_ptr_add_count);236 INCR_COUNT(bound_ptr_add_count);
237 if (tree) {237 if (tree) {
238 tree = splay (addr, tree);
239 addr -= tree->start;238 addr -= tree->start;
239 if (addr >= tree->size) {
240 addr = (size_t)p;
241 tree = splay (addr, tree);
242 addr -= tree->start;
243 }
240 if (addr >= tree->size) {244 if (addr >= tree->size) {
241 addr = (size_t)p;245 addr = (size_t)p;
242 tree = splay_end (addr, tree);246 tree = splay_end (addr, tree);
@@ -273,8 +277,12 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \ · 30 unmodified lines
273 WAIT_SEM (); \277 WAIT_SEM (); \
274 INCR_COUNT(bound_ptr_indir ## dsize ## _count); \278 INCR_COUNT(bound_ptr_indir ## dsize ## _count); \
275 if (tree) { \279 if (tree) { \
276 tree = splay (addr, tree); \
277 addr -= tree->start; \280 addr -= tree->start; \
281 if (addr >= tree->size) { \
282 addr = (size_t)p; \
283 tree = splay (addr, tree); \
284 addr -= tree->start; \
285 } \
278 if (addr >= tree->size) { \286 if (addr >= tree->size) { \
279 addr = (size_t)p; \287 addr = (size_t)p; \
280 tree = splay_end (addr, tree); \288 tree = splay_end (addr, tree); \
@@ -968,12 +976,14 @@ char *__bound_strcpy(char *dst, const char *src) · 687 unmodified lines
968 976
969char *__bound_strncpy(char *dst, const char *src, size_t n)977char *__bound_strncpy(char *dst, const char *src, size_t n)
970{978{
971 size_t len;979 size_t len = n;
972 void *p;980 const char *p = src;
973 981
982 while (len-- && *p++);
983 len = p - src;
974 INCR_COUNT(bound_strncpy_count);984 INCR_COUNT(bound_strncpy_count);
975 __bound_check(dst, n);985 __bound_check(dst, len);
976 __bound_check(src, n);986 __bound_check(src, len);
977 return strncpy (dst, src, n);987 return strncpy (dst, src, n);
978}988}
979 989
@@ -994,10 +1004,23 @@ int __bound_strcmp(const char *s1, const char *s2) · 14 unmodified lines
994 1004
995int __bound_strncmp(const char *s1, const char *s2, size_t n)1005int __bound_strncmp(const char *s1, const char *s2, size_t n)
996{1006{
1007 const unsigned char *u1 = (const unsigned char *) s1;
1008 const unsigned char *u2 = (const unsigned char *) s2;
1009 int retval = 0;
1010
997 INCR_COUNT(bound_strncmp_count);1011 INCR_COUNT(bound_strncmp_count);
998 __bound_check(s1, n);1012 do {
999 __bound_check(s2, n);1013 if ((ssize_t) --n == -1)
1000 return strncmp(s1, s2, n);1014 break;
1015 else if (*u1 != *u2) {
1016 retval = *u1 - *u2;
1017 break;
1018 }
1019 u2++;
1020 } while (*u1++);
1021 __bound_check(s1, ((const char *)u1 - s1) + 1);
1022 __bound_check(s2, ((const char *)u1 - s1) + 1);
1023 return retval;
1001}1024}
1002 1025
1003char *__bound_strcat(char *dest, const char *src)1026char *__bound_strcat(char *dest, const char *src)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t