diff --git a/Makefile b/Makefile index fcc6229..d103d97 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC := gcc AS := nasm ASFLAGS := -g -f elf64 -I src/ -CFLAGS := -g -Wall -Wextra -std=c2x +CFLAGS := -g -std=c2x MAIN := $(wildcard *.c) SRC := $(wildcard src/*.s) OBJ_DIR := build @@ -13,7 +13,7 @@ LIB_NAME := asm PATH_LIB_NAME := $(OBJ_DIR)/lib$(LIB_NAME).a LD := $(CC) -LDFLAGS := -L$(OBJ_DIR) -l$(LIB_NAME) -fPIC -fPIE +LDFLAGS := -L$(OBJ_DIR) -l$(LIB_NAME) all: $(NAME) diff --git a/main.c b/main.c index bd2e0c3..9020864 100644 --- a/main.c +++ b/main.c @@ -40,12 +40,11 @@ test_strlen() { char *test = "test"; printf("test: %ld\n", ft_strlen(test)); - char *very_long = calloc(USHRT_MAX, 1); + char *very_long = calloc(USHRT_MAX + 1, 1); very_long = memset(very_long, 'C', USHRT_MAX - 1); very_long[USHRT_MAX] = '\0'; printf("very_long: %ld\n", ft_strlen(very_long)); - // printf("%s\n", very_long); free(very_long); } @@ -61,12 +60,11 @@ test_strlen() { char *test = "test"; printf("test: %ld\n", strlen(test)); - char *very_long = calloc(USHRT_MAX, 1); + char *very_long = calloc(USHRT_MAX + 1, 1); very_long = memset(very_long, 'C', USHRT_MAX - 1); very_long[USHRT_MAX] = '\0'; printf("very_long: %ld\n", strlen(very_long)); - // printf("%s\n", very_long); free(very_long); } } @@ -77,7 +75,7 @@ test_strcpy() { { printf("ft_strcpy:\n"); - ft_strcpy(NULL, NULL); + printf("error double NULL: %s\n", ft_strcpy(NULL, NULL)); char src_empty[] = ""; char dst_empty[] = ""; @@ -85,28 +83,20 @@ test_strcpy() { printf ("ret: %s_%p, src: %s_%p, dest: %s_%p\n", ret, ret, src_empty, src_empty, dst_empty, dst_empty); - char null_char[0]; - ft_strcpy(null_char, NULL); - - ft_strcpy(dst_empty, NULL); - ft_strcpy(NULL, src_empty); - char src[10] = "src"; char dst[10] = "dst"; printf ("src: %s, dest: %s\n", src, dst); ret = ft_strcpy(dst, src); printf ("ret: %s_%p, src: %s_%p, dest: %s_%p\n", ret, ret, src, src, dst, dst); - // printf ("ret: %p, src: %p, dest: %p\n", ret, src, dst); - // - char *very_long_src = calloc(USHRT_MAX, 1); - char *very_long_dst = calloc(USHRT_MAX, 1); + + char *very_long_src = calloc(USHRT_MAX + 1, 1); + char *very_long_dst = calloc(USHRT_MAX + 1, 1); very_long_src = memset(very_long_src, 'C', USHRT_MAX - 1); very_long_src[USHRT_MAX] = '\0'; ret = ft_strcpy(very_long_dst, very_long_src); - printf ("ret: %p, src: %p, dest: %p\n", + printf ("long: %p, src: %p, dest: %p\n", ret, very_long_src, very_long_dst); - // printf("%s\n", very_long); free(very_long_src); free(very_long_dst); } @@ -117,6 +107,8 @@ test_strcpy() { { printf("strcpy:\n"); + printf("error double NULL: %s\n", strcpy(NULL, NULL)); + char src_empty[] = ""; char dst_empty[] = ""; char *ret = strcpy(dst_empty, src_empty); @@ -128,14 +120,13 @@ test_strcpy() { printf ("src: %s, dest: %s\n", src, dst); ret = strcpy(dst, src); printf ("ret: %s_%p, src: %s_%p, dest: %s_%p\n", ret, ret, src, src, dst, dst); - // printf ("ret: %p, src: %p, dest: %p\n", ret, src, dst); - // - char *very_long_src = calloc(USHRT_MAX, 1); - char *very_long_dst = calloc(USHRT_MAX, 1); + + char *very_long_src = calloc(USHRT_MAX + 1, 1); + char *very_long_dst = calloc(USHRT_MAX + 1, 1); very_long_src = memset(very_long_src, 'C', USHRT_MAX - 1); very_long_src[USHRT_MAX] = '\0'; ret = strcpy(very_long_dst, very_long_src); - printf ("ret: %p, src: %p, dest: %p\n", + printf ("long: %p, src: %p, dest: %p\n", ret, very_long_src, very_long_dst); // printf("%s\n", very_long); @@ -151,6 +142,8 @@ test_strcmp() { { printf("ft_strcmp:\n"); + printf("error double NULL: %d\n", ft_strcmp(NULL, NULL)); + printf("ret: %d, s1: '', s2: ''\n", ft_strcmp("", "")); char *s1 = "test4"; @@ -170,6 +163,8 @@ test_strcmp() { { printf("strcmp:\n"); + printf("error double NULL: %d\n", strcmp(NULL, NULL)); + printf("ret: %d, s1: '', s2: ''\n", strcmp("", "")); char *s1 = "test4"; @@ -220,6 +215,8 @@ test_read() { { printf("ft_read:\n"); + printf("error NULL: %ld\n", ft_read(0, NULL, 0)); + // char in[10]; // ft_read(0, in, 10); // printf("%s\n", in); @@ -238,6 +235,8 @@ test_read() { { printf("read:\n"); + printf("error NULL: %ld\n", read(0, NULL, 0)); + // char in[10]; // read(0, in, 10); // printf("%s\n", in); @@ -281,6 +280,7 @@ test_strdup() { free(copy_long); } + printf("\n"); // strdup { @@ -316,6 +316,8 @@ test_calloc() { // ft_calloc { printf("ft_calloc\n"); + + printf("error NULL: %p\n", ft_calloc(-1, -1)); char *normal = ft_calloc(10, 1); // return "" printf("normal: %s_%p\n", normal, normal); @@ -344,6 +346,8 @@ test_calloc() { { printf("calloc\n"); + printf("error NULL: %p\n", calloc(-1, -1)); + char *normal = ft_calloc(10, 1); printf("normal: %s_%p\n", normal, normal); for (int i = 0; i < 10; i++) diff --git a/src/ft_calloc.s b/src/ft_calloc.s index 622a81f..ce12e43 100644 --- a/src/ft_calloc.s +++ b/src/ft_calloc.s @@ -6,11 +6,13 @@ section .text global ft_calloc ; rdi = nitems, rsi = size ft_calloc: - ; Check if one of arguments are 0 + ; Check if one of arguments are 0 or negative cmp rdi, 0 je malloc_0 + jl end_null cmp rsi, 0 je malloc_0 + jl end_null ; save rbx push rbx @@ -21,13 +23,14 @@ section .text mul rbx, rsi cmovno rdi, rbx; Move if not overflow cmp rdi, 0 - je end_null + je end_null_pop ; malloc and security if malloc failed call malloc wrt ..plt cmp rax, 0 - je end_null + je end_null_pop + ; set everything to 0 bzero: dec rbx mov [rax + rbx], 0 @@ -38,9 +41,10 @@ section .text pop rbx ret + end_null_pop: + pop rbx end_null: xor rax, rax - pop rbx ret malloc_0: diff --git a/src/ft_strcmp.s b/src/ft_strcmp.s index 308ed2d..ac87450 100644 --- a/src/ft_strcmp.s +++ b/src/ft_strcmp.s @@ -8,6 +8,11 @@ section .text ft_strcmp: mov rax, -1 ; counter + cmp rdi, 0 + je endloop + cmp rsi, 0 + je endloop + loop: inc rax mov cl, [rdi + rax] ; cl = s1[rax]