I am doing a function that reads a line from a file. Inside this function I use the function read (man 2 read) and the number of bytes from the read function is set by the user. This function is a exercise from my school. The prototype is something like this
get_next_line(int fd, char **line);
And the read function I´m using this way:
read(fd, buf, BUFFER_SIZE)
To compile this program the user should use the flag -D BUFFER_SIZE=xx. The BUFFER_SIZE is a macro to define the nbytes from the read function.
I have no issues about how to do this exercise. My main problem is to understand how many bytes does a read function can handle.
I have noticed that if I input 10000000 in my BUFFER_SIZE I get a segmentation fault, and using the sanitize flag it brings me this error:
AddressSanitizer:DEADLYSIGNAL
=================================================================
==84594==ERROR: AddressSanitizer: stack-overflow on address 0x7ffee85a0f20 (pc 0x000106cd68f7 bp 0x7ffee8f2a710 sp 0x7ffee85a0e40 T0)
#0 0x106cd68f6 in ft_readfile (a.out:x86_64+0x1000018f6)
#1 0x106cd6fba in get_next_line (a.out:x86_64+0x100001fba)
#2 0x106cd7a7a in main (a.out:x86_64+0x100002a7a)
#3 0x7fff652af3d4 in start (libdyld.dylib:x86_64+0x163d4)
SUMMARY: AddressSanitizer: stack-overflow (a.out:x86_64+0x1000018f6) in ft_readfile
==84594==ABORTING
[1] 84594 abort ./a.out
The ft_readfile is where I use the read function. I have tried to find this info but i couldn´t find anywhere.
Is there a max size of bytes that the read function can read? If so, how many bytes can it handle and why does it have this limit?
EDIT
Those are the information asked:
The buf is defined like this:
char buf[BUFFER_SIZE + 1];
The function that it reads the file is like this:
int ft_readfile(int fd, t_list *lst)
{
int res;
char buf[BUFFER_SIZE + 1];
char *tmp;
tmp = lst->content;
if (lst->content == NULL)
lst->content = ft_strdup("");
else
lst->content = ft_substr(lst->content, ft_strlen_char(lst->content,
'\n') + 1, ft_strlen_char(lst->content, '\0'));
free(tmp);
buf[0] = '\0';
while (!(ft_haschr(buf, '\n')) && (res = read(fd, buf, BUFFER_SIZE)))
{
if (res == -1)
return (-1);
buf[res] = '\0';
tmp = lst->content;
lst->content = ft_strjoin(lst->content, buf);
free(tmp);
}
return (res);
}