-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanary.cpp
32 lines (25 loc) · 928 Bytes
/
Canary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "Canary.hpp"
#include <cassert>
#include <cstdlib>
canary_t leftBufCanary(const void *buf)
{
assert(buf != nullptr);
return *(canary_t *)(((char *) buf) - CanarySize);
}
canary_t rightBufCanary(const void *buf, size_t bufSize)
{
assert(buf != nullptr);
return *(canary_t *)(((char *) buf) + bufSize);
}
void *callocBufWithBorderCanaries(size_t bufSize, size_t bufElemSize)
{
auto buffer = (char *) std::calloc(1, CanarySize + bufSize * bufElemSize + CanarySize);
*((canary_t *) buffer) = *((canary_t *) (buffer + CanarySize + bufSize * bufElemSize)) = CanaryValue;
return buffer + CanarySize;
}
void *reallocBufWithBorderCanaries(void *buf, size_t newBufSize)
{
buf = realloc((char *) buf - CanarySize, CanarySize + newBufSize + CanarySize);
*((canary_t *) buf) = *((canary_t *) ((char *) buf + CanarySize + newBufSize)) = CanaryValue;
return (char *) buf + CanarySize;
}