Search code examples
cmemory-leaksvalgrind

Can't figure out where I have memory leak


I'm new to C and still don't really know how to work with valgrind. I'm doing a project where i need to create a function that returns a line of text from a file descriptor each time it's called using just one static variable.

Repeated calls (e.g., using a loop) to your get_next_line() function should let you read the text file pointed to by the file descriptor, one line at a time.

I have come up with this but I can't find where the memory leak is:

char    *output(char **backup, char *rbackup, int ret, int fd)
{
    int     value;
    char    *temp;

    if (ret < 0)
        return (NULL);
    else if (ret == 0 && backup[fd] == NULL)
        return (NULL);
    value = (int)(ft_strchr(backup[fd], '\n') - backup[fd] + 1);
    rbackup = ft_substr(backup[fd], 0, value);
    temp = ft_substr(backup[fd], value, BUFFER_SIZE * BUFFER_SIZE);
    free(backup[fd]);
    if (temp[0] == '\0')
    {
        free(temp);
        temp = NULL;
    }
    backup[fd] = temp;
    return (rbackup);
}

char    *get_next_line(int fd)
{
    int         ret;
    char        buf[BUFFER_SIZE + 1];
    static char *backup[NUM_OF_FD];
    char        *rbackup;

    if (fd < 0 || fd > NUM_OF_FD)
        return (NULL);
    while (ft_strchr(backup[fd], '\n') == NULL)
    {
        ret = read(fd, buf, BUFFER_SIZE);
        buf[ret] = '\0';
        if (ret <= 0)
            break ;
        if (backup[fd] == NULL)
            backup[fd] = ft_strdup(buf);
        else
        {
            rbackup = ft_strjoin(backup[fd], buf);
            free(backup[fd]);
            backup[fd] = rbackup;
        }
    }
    return (output(backup, rbackup, ret, fd));
}

The ft_functions are equivalent to the LibC counterparts but in case of having a bug I'll post them here:

void    *ft_memcpy(void *dst, const void *src, size_t n)
{
    size_t  i;

    i = -1;
    if ((dst != src) && n)
        while (++i < n)
            ((unsigned char *)dst)[i] = ((unsigned char *)src)[i];
    return (dst);
}

size_t  ft_strlen(const char *s)
{
    size_t  i;

    i = 0;
    while (s[i])
    {
        i++;
    }
    return (i);
}

char    *ft_strchr(const char *s, int c)
{
    char    chr;

    chr = (char)c;
    if (s == NULL)
        return (NULL);
    while (*s && *s != chr)
        s++;
    if (*s == chr)
        return ((char *)s);
    else
        return (NULL);
}

char    *ft_substr(char const *s, unsigned int start, size_t len)
{
    char    *str;

    if (!s)
        return (NULL);
    if (len > ft_strlen(s))
        len = ft_strlen(s);
    if (start > ft_strlen(s))
        len = 0;
    str = malloc(sizeof(char) * (len + 1));
    if (!str)
        return (NULL);
    str = ft_memcpy(str, &s[start], len);
    str[len] = '\0';
    return (str);
}

char    *ft_strdup(const char *s1)
{
    size_t  len;
    void    *new;

    len = ft_strlen(s1) + 1;
    new = malloc(len);
    if (new == NULL)
        return (NULL);
    return ((char *) ft_memcpy(new, s1, len));
}

char    *ft_strjoin(char const *s1, char const *s2)
{
    int     i;
    char    *str;
    size_t  size;

    if (!s1 || !s2)
        return (NULL);
    i = 0;
    size = (ft_strlen(s1) + ft_strlen(s2) + 1);
    str = malloc(sizeof(char) * size);
    if (!str)
        return (NULL);
    while (*s1)
        str[i++] = *s1++;
    while (*s2)
        str[i++] = *s2++;
    str[i] = '\0';
    return (str);
}

void    *ft_memset(void *b, int c, size_t len)
{
    size_t  i;

    i = -1;
    while (++i < len)
        ((unsigned char *)b)[i] = (unsigned char)c;
    return (b);
}

Is there any rookie mistake in my code?


Solution

  • Well I ran your code like this

    int main(int a, char**b)
    {
       int f = open("poop.c", O_RDONLY);
       for(int i = 0; i < 10; i++)
       {
        char *x = get_next_line(f);
        printf("x");
        free(x);
       }
    }
    

    reading the first 10 lines. No leaks detected by valgrind (once I added the free in the loop)

    It did moan about other things though

    ==3695== Invalid read of size 1
    ==3695==    at 0x109270: ft_memcpy (poop.c:26)
    ==3695==    by 0x1093BB: ft_substr (poop.c:70)
    ==3695==    by 0x109619: output (poop.c:120)
    ==3695==    by 0x109802: get_next_line (poop.c:155)
    ==3695==    by 0x109857: main (poop.c:162)
    ==3695==  Address 0x4a4a0a5 is 0 bytes after a block of size 101 alloc'd
    ==3695==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==3695==    by 0x109408: ft_strdup (poop.c:81)
    ==3695==    by 0x109724: get_next_line (poop.c:147)
    ==3695==    by 0x109857: main (poop.c:162)
    ==3695==
    ==3695== Conditional jump or move depends on uninitialised value(s)
    ==3695==    at 0x109546: output (poop.c:114)
    ==3695==    by 0x109802: get_next_line (poop.c:155)
    ==3695==    by 0x109857: main (poop.c:162)
    ==3695==
    ==3695== Conditional jump or move depends on uninitialised value(s)
    ==3695==    at 0x109556: output (poop.c:116)
    ==3695==    by 0x109802: get_next_line (poop.c:155)
    ==3695==    by 0x109857: main (poop.c:162)
    ==3695==
    

    seems like the uninitialized read is caused by reading an empty line.

    The invalid read is here

    ((unsigned char*)dst)[i] = ((unsigned char*)src)[i];