alelievr / libft-unit-test

456 stars 88 forks source link

Fixed a problem that always crashes when executing `ft_strrchr` test code. #146

Closed keit0728 closed 1 year ago

keit0728 commented 1 year ago

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.