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
165165 static int no_checking = 0;
166166
167167 #ifdef BOUND_STATISTIC
168−static unsigned long bound_ptr_add_count;
169−static unsigned long bound_ptr_indir1_count;
170−static unsigned long bound_ptr_indir2_count;
171−static unsigned long bound_ptr_indir4_count;
172−static unsigned long bound_ptr_indir8_count;
173−static unsigned long bound_ptr_indir12_count;
174−static unsigned long bound_ptr_indir16_count;
175−static unsigned long bound_local_new_count;
176−static unsigned long bound_local_delete_count;
177−static unsigned long bound_malloc_count;
178−static unsigned long bound_calloc_count;
179−static unsigned long bound_realloc_count;
180−static unsigned long bound_free_count;
181−static unsigned long bound_memalign_count;
182−static unsigned long bound_mmap_count;
183−static unsigned long bound_munmap_count;
184−static unsigned long bound_alloca_count;
185−static unsigned long bound_mempcy_count;
186−static unsigned long bound_memcmp_count;
187−static unsigned long bound_memmove_count;
188−static unsigned long bound_memset_count;
189−static unsigned long bound_strlen_count;
190−static unsigned long bound_strcpy_count;
191−static unsigned long bound_strncpy_count;
192−static unsigned long bound_strcmp_count;
193−static unsigned long bound_strncmp_count;
194−static unsigned long bound_strcat_count;
195−static unsigned long bound_strchr_count;
196−static unsigned long bound_strdup_count;
168+static unsigned long long bound_ptr_add_count;
169+static unsigned long long bound_ptr_indir1_count;
170+static unsigned long long bound_ptr_indir2_count;
171+static unsigned long long bound_ptr_indir4_count;
172+static unsigned long long bound_ptr_indir8_count;
173+static unsigned long long bound_ptr_indir12_count;
174+static unsigned long long bound_ptr_indir16_count;
175+static unsigned long long bound_local_new_count;
176+static unsigned long long bound_local_delete_count;
177+static unsigned long long bound_malloc_count;
178+static unsigned long long bound_calloc_count;
179+static unsigned long long bound_realloc_count;
180+static unsigned long long bound_free_count;
181+static unsigned long long bound_memalign_count;
182+static unsigned long long bound_mmap_count;
183+static unsigned long long bound_munmap_count;
184+static unsigned long long bound_alloca_count;
185+static unsigned long long bound_mempcy_count;
186+static unsigned long long bound_memcmp_count;
187+static unsigned long long bound_memmove_count;
188+static unsigned long long bound_memset_count;
189+static unsigned long long bound_strlen_count;
190+static unsigned long long bound_strcpy_count;
191+static unsigned long long bound_strncpy_count;
192+static unsigned long long bound_strcmp_count;
193+static unsigned long long bound_strncmp_count;
194+static unsigned long long bound_strcat_count;
195+static unsigned long long bound_strchr_count;
196+static unsigned long long bound_strdup_count;
197197 #define INCR_COUNT(x) x++
198198 #else
199199 #define INCR_COUNT(x)
@@ -235,8 +235,12 @@ void * FASTCALL __bound_ptr_add(void *p, size_t offset) · 35 unmodified lines
235235 WAIT_SEM ();
236236 INCR_COUNT(bound_ptr_add_count);
237237 if (tree) {
238− tree = splay (addr, tree);
239238 addr -= tree->start;
239+ if (addr >= tree->size) {
240+ addr = (size_t)p;
241+ tree = splay (addr, tree);
242+ addr -= tree->start;
243+ }
240244 if (addr >= tree->size) {
241245 addr = (size_t)p;
242246 tree = splay_end (addr, tree);
@@ -273,8 +277,12 @@ void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \ · 30 unmodified lines
273277 WAIT_SEM (); \
274278 INCR_COUNT(bound_ptr_indir ## dsize ## _count); \
275279 if (tree) { \
276− tree = splay (addr, tree); \
277280 addr -= tree->start; \
281+ if (addr >= tree->size) { \
282+ addr = (size_t)p; \
283+ tree = splay (addr, tree); \
284+ addr -= tree->start; \
285+ } \
278286 if (addr >= tree->size) { \
279287 addr = (size_t)p; \
280288 tree = splay_end (addr, tree); \
@@ -968,12 +976,14 @@ char *__bound_strcpy(char *dst, const char *src) · 687 unmodified lines
968976
969977 char *__bound_strncpy(char *dst, const char *src, size_t n)
970978 {
971− size_t len;
972− void *p;
979+ size_t len = n;
980+ const char *p = src;
973981
982+ while (len-- && *p++);
983+ len = p - src;
974984 INCR_COUNT(bound_strncpy_count);
975− __bound_check(dst, n);
976− __bound_check(src, n);
985+ __bound_check(dst, len);
986+ __bound_check(src, len);
977987 return strncpy (dst, src, n);
978988 }
979989
@@ -994,10 +1004,23 @@ int __bound_strcmp(const char *s1, const char *s2) · 14 unmodified lines
9941004
9951005 int __bound_strncmp(const char *s1, const char *s2, size_t n)
9961006 {
1007+ const unsigned char *u1 = (const unsigned char *) s1;
1008+ const unsigned char *u2 = (const unsigned char *) s2;
1009+ int retval = 0;
1010+
9971011 INCR_COUNT(bound_strncmp_count);
998− __bound_check(s1, n);
999− __bound_check(s2, n);
1000− return strncmp(s1, s2, n);
1012+ do {
1013+ if ((ssize_t) --n == -1)
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;
10011024 }
10021025
10031026 char *__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