patch calloc and add more test
This commit is contained in:
parent
ddaaeaee6d
commit
2c658cc4ee
24
main.c
24
main.c
|
|
@ -179,19 +179,23 @@ test_calloc() {
|
||||||
{
|
{
|
||||||
printf("ft_calloc\n");
|
printf("ft_calloc\n");
|
||||||
|
|
||||||
char *error = ft_calloc(2, 10); // return ""
|
char *normal = ft_calloc(10, 1); // return ""
|
||||||
printf("error: %p\n", error);
|
printf("normal: %s_%p\n", normal, normal);
|
||||||
|
for (int i = 0; i <= 10; i++)
|
||||||
|
printf("'%b' ", normal[i]);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
char *empty = ft_calloc(0, 0);
|
char *empty = ft_calloc(0, 0);
|
||||||
printf("empty: %p\n", empty);
|
printf("empty: %p\n", empty);
|
||||||
|
|
||||||
char *overflow = ft_calloc();
|
char *overflow = ft_calloc(LONG_MAX, LONG_MAX);
|
||||||
|
printf("overflow: %s_%p\n", overflow, overflow);
|
||||||
|
|
||||||
char *buf = ft_calloc(10, 1);
|
char *buf = ft_calloc(10, 1);
|
||||||
strcpy(buf, "test");
|
strcpy(buf, "test");
|
||||||
printf("test: %s_%p\n", buf, buf);
|
printf("test: %s_%p\n", buf, buf);
|
||||||
|
|
||||||
free(error);
|
free(normal);
|
||||||
free(empty);
|
free(empty);
|
||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
@ -202,17 +206,23 @@ test_calloc() {
|
||||||
{
|
{
|
||||||
printf("calloc\n");
|
printf("calloc\n");
|
||||||
|
|
||||||
char *error = calloc(2, 10); // return ""
|
char *normal = ft_calloc(10, 1);
|
||||||
printf("error: %p\n", error);
|
printf("normal: %s_%p\n", normal, normal);
|
||||||
|
for (int i = 0; i <= 10; i++)
|
||||||
|
printf("'%b' ", normal[i]);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
char *empty = calloc(0, 0);
|
char *empty = calloc(0, 0);
|
||||||
printf("empty: %p\n", empty);
|
printf("empty: %p\n", empty);
|
||||||
|
|
||||||
|
char *overflow = calloc(LONG_MAX, LONG_MAX);
|
||||||
|
printf("overflow: %s_%p\n", overflow, overflow);
|
||||||
|
|
||||||
char *buf = calloc(10, 1);
|
char *buf = calloc(10, 1);
|
||||||
strcpy(buf, "test");
|
strcpy(buf, "test");
|
||||||
printf("test: %s_%p\n", buf, buf);
|
printf("test: %s_%p\n", buf, buf);
|
||||||
|
|
||||||
free(error);
|
free(normal);
|
||||||
free(empty);
|
free(empty);
|
||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
main.h
2
main.h
|
|
@ -7,7 +7,7 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <limit.h>
|
#include <limits.h>
|
||||||
|
|
||||||
// DONE
|
// DONE
|
||||||
extern size_t ft_strlen(const char *s);
|
extern size_t ft_strlen(const char *s);
|
||||||
|
|
|
||||||
|
|
@ -6,22 +6,46 @@ section .text
|
||||||
global ft_calloc
|
global ft_calloc
|
||||||
; rdi = nitems, rsi = size
|
; rdi = nitems, rsi = size
|
||||||
ft_calloc:
|
ft_calloc:
|
||||||
|
; Check if one of arguments are 0
|
||||||
|
cmp rdi, 0
|
||||||
|
je malloc_0
|
||||||
|
cmp rsi, 0
|
||||||
|
je malloc_0
|
||||||
|
|
||||||
|
; save rbx
|
||||||
push rbx
|
push rbx
|
||||||
mov rbx, rdi
|
mov rbx, rdi
|
||||||
|
|
||||||
mul rbx, rsi
|
; multiplication and security for overflow
|
||||||
xor rdi, rdi
|
xor rdi, rdi
|
||||||
|
mul rbx, rsi
|
||||||
cmovno rdi, rbx; Move if not overflow
|
cmovno rdi, rbx; Move if not overflow
|
||||||
|
cmp rdi, 0
|
||||||
|
je end_null
|
||||||
|
|
||||||
|
; malloc and security if malloc failed
|
||||||
call malloc wrt ..plt
|
call malloc wrt ..plt
|
||||||
|
|
||||||
cmp rax, 0
|
cmp rax, 0
|
||||||
je end
|
je end_null
|
||||||
|
|
||||||
mov rdi, rax
|
bzero:
|
||||||
mov rsi, rbx
|
dec rbx
|
||||||
call bzero wrt ..plt
|
mov [rax + rbx], 0
|
||||||
|
|
||||||
end:
|
cmp rbx, 0
|
||||||
|
jg bzero
|
||||||
|
|
||||||
|
pop rbx
|
||||||
|
ret
|
||||||
|
|
||||||
|
end_null:
|
||||||
|
xor rax, rax
|
||||||
pop rbx
|
pop rbx
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
malloc_0:
|
||||||
|
mov rdi, 0
|
||||||
|
call malloc wrt ..plt
|
||||||
|
ret
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue