Stack Overflow
Stack Overflow

Reputation: 51

realloc failing on second call

I am trying to add a bunch of WCHARs to a buffer. This function is what adds it to my buffer..

DWORD add_to_buffer(BYTE *databuffer, WCHAR *path, WCHAR *value_name, DWORD type, BYTE *data, DWORD data_size, DWORD already_added) {
    DWORD path_size = wcslen(path) * 2;
    DWORD value_name_size = wcslen(value_name) * 2;

    WCHAR *type_name = reg_type_to_wchar(type);
    DWORD type_size = wcslen(type_name) * 2;


    DWORD total_length = already_added + path_size + value_name_size + type_size + data_size;

    *databuffer = realloc(databuffer, total_length);
    
    CopyMemory(databuffer, path, path_size);
    CopyMemory(databuffer + path_size, value_name, value_name_size);
    CopyMemory(databuffer + path_size + value_name_size, type_name, type_size);
    CopyMemory(databuffer + path_size + value_name_size + type_size, data, data_size);
        
    return total_length;
}

On the second call to the add_to_buffer() the realloc() fails. I am basically calling this function over and over, while adding information to it and making it bigger as needed. I am not sure how to troubleshoot this issue as everything in VS looks correct when going into the function. databuffer is not getting freed anywhere outside of this function.

Upvotes: 3

Views: 70

Answers (3)

Devi Khositashvili
Devi Khositashvili

Reputation: 576

realloc return value: "This function returns a pointer to the newly allocated memory, or NULL if the request fails." On the 2nd call, there should be no more available space to realloc... i think the easiest way is to change it as: *databuffer = realloc(databuffer, total_length); if(databuffer == NULL) { *databuffer = malloc(total_length); }

Upvotes: 1

Adrian Mole
Adrian Mole

Reputation: 51815

You are mixing up levels of pointer indirection. For example, in your line, *databuffer = realloc(databuffer, total_length); your are (on the left-hand side) dereferencing the databuffer variable but, inside the call, you are not.

If you want your function to modify the pointer (which it does), then you need to pass a pointer to that pointer, so that the modified value (new address) is available to the calling module. Like this:

DWORD add_to_buffer(BYTE **databuffer, WCHAR *path, WCHAR *value_name, DWORD type, BYTE
 *data, DWORD data_size, DWORD already_added) { // Pass a "databuffer" as a DOUBLE pointer
    DWORD path_size = wcslen(path) * 2;
    DWORD value_name_size = wcslen(value_name) * 2;

    WCHAR *type_name = reg_type_to_wchar(type);
    DWORD type_size = wcslen(type_name) * 2;

    DWORD total_length = already_added + path_size + value_name_size + type_size + data_size;

    // It is also bad practice to overwrite the argument in "realloc" calls; save to a
    // temp, so that you can check for failure ...
    BYTE *temp = realloc(*databuffer, total_length); // Not the "*" before databuffer!
    if (temp == NULL) { // Allocation failure ...
        // Here, place code to handle/signal the error
        // But note that we STILL HAVE THE ORIGINAL POINTER!
        return 0; // and return a value that indicates failure
    }
    *databuffer = temp; // Succeeded: we can now safely reassign the passed pointer.
    
    // We now need to also dereference the double pointer in the following calls ...
    CopyMemory(*databuffer, path, path_size);
    CopyMemory(*databuffer + path_size, value_name, value_name_size);
    CopyMemory(*databuffer + path_size + value_name_size, type_name, type_size);
    CopyMemory(*databuffer + path_size + value_name_size + type_size, data, data_size);
        
    return total_length;
}

Upvotes: 3

Skult
Skult

Reputation: 46

*databuffer = realloc(databuffer, total_length);

Here you assign value returned by realloc to first byte of databuffer. You shouldn't dereference databuffer.

Upvotes: 1

Related Questions