Testing ft_strrchr always crushes with test_ft_strrchr_electric_memory.
ft_strrchr: [OK] [OK] [OK] [OK] [OK] [OK] [CRASH] [OK]
[crash]: your strrchr crash because it read too many bytes or attempt to write on s !
Test code:
char *src = electric_alloc(10);
__builtin___strcpy_chk (src, "123456789", __builtin_object_size (src, 2 > 1 ? 1 : 0));
ft_strrchr(src, 'a');
src = electric_alloc_rev(10);
__builtin___strcpy_chk (src, "123456789", __builtin_object_size (src, 2 > 1 ? 1 : 0));
ft_strrchr(src, 'a');
exit(TEST_SUCCESS);
Upon investigation, I found that it was due to mprotect(ptr, 4096, PROT_NONE); in the electric_alloc_rev function.
I measured the page size on my Macbook pro m1 with the following code and it was 16384.
long page_size = sysconf(_SC_PAGESIZE);
So it should be mprotect(ptr, 16384, PROT_NONE);.
After modifying the code to allocate memory space based on page size, this problem no longer occurs.
Testing
ft_strrchr
always crushes withtest_ft_strrchr_electric_memory
.Upon investigation, I found that it was due to
mprotect(ptr, 4096, PROT_NONE);
in theelectric_alloc_rev
function. I measured the page size on my Macbook pro m1 with the following code and it was16384
.So it should be
mprotect(ptr, 16384, PROT_NONE);
. After modifying the code to allocate memory space based on page size, this problem no longer occurs.