buffer​ overflow

*** stack smashing detected ***

A buffer​ overflow occurs when the user input exceeds the buffer capacity. https://www.educative.io/edpresso/what-is-the-stack-smashing-detected-error

Find the potential exception

1
void* memcpy(void* pvTo, void* pvFrom, size_t size){
2
3
byte* pbTo = (byte*)pvTo;
4
byte* pbFrom = (byte*)pvFrom;
5
6
7
while(size-- > 0)
8
*pbTo++ = *pbFrom++;
9
10
11
return pvTo;
12
}
Copied!

Code:

1
#include <stdio.h>
2
typedef unsigned char byte;
3
4
void* z_memcpy(void* restrict pvTo, void* pvFrom, size_t size){
5
byte* pbTo = (byte*)pvTo;
6
byte* pbFrom = (byte*)pvFrom;
7
8
9
while(size-- > 0){
10
printf("TO: %c, From: %c \n", *pbTo, *pbFrom);
11
*pbTo++ = *pbFrom++;
12
}
13
return pvTo;
14
}
Copied!

Test code

1
int main() {
2
3
char to[4] = "";
4
char from[] = "abcdefg";
5
6
z_memcpy(to, from, 14);
7
8
return 0;
9
}
Copied!