0

I'm doing some test and I'm a bit confused about what valgrind reports, it would be great if someone could help me understand the output better.

Basiclly what I want to do is to create a dynamic array to hold x number of strings, for this test the strings are pre-set.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *array[4]={"str1", "str4", "str0", "str2"};
int arr_index = 0;

char* get_array(void)
{

    if (arr_index >= (sizeof(array)/sizeof(array[0])))
    {
        return NULL;
    }
    else
    {
        return array[arr_index];
    }
}

int main(void)
{
    char **orderIds = malloc(0);
    char *tmp=NULL;
    int variableNumberOfElements=0;

    for(int i = 0; i < 10; i++) {
        tmp = get_array();
        if(tmp == NULL)
        {
            break;
        }
        arr_index++;
        variableNumberOfElements++;
        orderIds = realloc(orderIds, variableNumberOfElements * sizeof(char*));
        orderIds[i] = malloc(strlen(tmp) * sizeof(char));
        strncpy(orderIds[i], tmp, strlen(tmp));
    }

    for(int j=0; j<variableNumberOfElements; j++)
    {
        printf("%s\n", orderIds[j]);
    }
    for(int x=0; x<=variableNumberOfElements; x++)
    {
        free(orderIds[x]);
    }

    free(orderIds);
    exit(0);
}

And the output when running valgrind --leak-check=yes ./a.out is:

==27260== Memcheck, a memory error detector
==27260== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==27260== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==27260== Command: ./a.out
==27260== 
==27260== Invalid read of size 1
==27260==    at 0x484ED24: strlen (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==27260==    by 0x48F1EE7: puts (ioputs.c:35)
==27260==    by 0x109357: main (test.c:42)
==27260==  Address 0x4a9c0d4 is 0 bytes after a block of size 4 alloc'd
==27260==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==27260==    by 0x1092E6: main (test.c:36)
==27260== 
str1
str4
str0
str2
==27260== Invalid read of size 8
==27260==    at 0x109381: main (test.c:46)
==27260==  Address 0x4a9c290 is 0 bytes after a block of size 32 alloc'd
==27260==    at 0x484DCD3: realloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==27260==    by 0x1092B8: main (test.c:35)
==27260== 
==27260== 
==27260== HEAP SUMMARY:
==27260==     in use at exit: 0 bytes in 0 blocks
==27260==   total heap usage: 10 allocs, 10 frees, 1,120 bytes allocated
==27260== 
==27260== All heap blocks were freed -- no leaks are possible
==27260== 
==27260== For lists of detected and suppressed errors, rerun with: -s
==27260== ERROR SUMMARY: 5 errors from 2 contexts (suppressed: 0 from 0)

So basiclly it's telling me that I'm doing some invalid reads when I'm allocating memory. But I think it looks correct, what have I missed?

I have calculated the bytes allocated and I think it looks right.

freska
  • 11
  • 1
  • 3
  • `malloc(strlen(tmp) * sizeof(char));` does not allocate enough memory to include a null byte to terminate the string because `strlen` counts are only the non-null characters, and your `strncpy` copies only `strlen(tmp)` characters. So, when you print the string, it is not null-terminated, and `printf` runs past the end of the allocated memory. Allocate one more byte and change the `strncpy` to `strcpy`. – Eric Postpischil Nov 05 '22 at 10:01
  • The null byte... Thanks, didn't think of that `strlen` didn't count the null character. But this only solves the read of size 1 problem, still gets the read of size 8 issue when running `realloc`. I'm probably miscounting something since `sizeof(char)=8`. – freska Nov 05 '22 at 10:17
  • It tells you exactly where the invalid read is. Look at the loop where the invalid read is. Is it like the other loops? – n. m. could be an AI Nov 05 '22 at 10:27
  • It's saying it is on line 46 `free(orderIds[x]);` so it's trying to free more memory for some reason? – freska Nov 05 '22 at 10:49
  • **Look at the loop where the invalid read is. Is it like the other loops?** – n. m. could be an AI Nov 05 '22 at 11:20
  • Thanks, I get your point. I think I need to get a rubber duck... – freska Nov 06 '22 at 08:27

0 Answers0