'
This commit is contained in:
commit
d0bd16da2f
9 changed files with 97 additions and 0 deletions
11
beware_overflow/beware_overflow.c
Normal file
11
beware_overflow/beware_overflow.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include "beware_overflow.h"
|
||||
|
||||
void *beware_overflow(void *ptr, size_t nmemb, size_t size)
|
||||
{
|
||||
size_t c;
|
||||
if (__builtin_mul_overflow(nmemb, size, &c))
|
||||
return NULL;
|
||||
|
||||
char *byte_ptr = ptr;
|
||||
return c + byte_ptr;
|
||||
}
|
||||
8
beware_overflow/beware_overflow.h
Normal file
8
beware_overflow/beware_overflow.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef BEWARE_OVERFLOW_H
|
||||
#define BEWARE_OVERFLOW_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void *beware_overflow(void *ptr, size_t nmemb, size_t size);
|
||||
|
||||
#endif /* BEWARE_OVERFLOW_H */
|
||||
21
beware_overflow/main.c
Normal file
21
beware_overflow/main.c
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "beware_overflow.h"
|
||||
|
||||
void print_overflow(void *ptr, size_t nmemb, size_t size)
|
||||
{
|
||||
void *begin = ptr;
|
||||
void *res = beware_overflow(ptr, nmemb, size);
|
||||
if (res)
|
||||
printf("Pointer was incremented from %p to %p.\n", begin, res);
|
||||
else
|
||||
printf("Overflow detected between %ld and %ld.\n", nmemb, size);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
print_overflow((void *)0x1000, 25, 6);
|
||||
print_overflow((void *)0x40000, 12345678904, 12345678904);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue