Question

I am trying to write a function that searches for all occurrences of a pattern and returns an array of offsets in the file that match the pattern. I want to use realloc to dynamically grow my returned array, but I am getting a sYSMALLOC Assertion error. Sometimes if I use a different search pattern I might get an invalid next size error. I do not have valgrind on my machine yet (would require rebuilding glibc with debug flag). This seems like such a simple issue, I only touch this pointer twice - once to declare it and set it to NULL and again to realloc to grow it. I know that sizeof(char) is 1, and is useless to have in my realloc statement.

Here is the code for the function having problems.

unsigned long long* searchBytes(FILE* fp, char* byteString, char* searchType, unsigned   long long fileSize)
{
if (fp == NULL)
    return NULL;

unsigned long long* foundBytes = NULL;
long numBytes = 0;

// make some memory for the array of found bytes
if (strcmp(searchType, "ascii") == 0)
{
    numBytes = strlen(byteString);
    //foundBytes = realloc(NULL, numBytes * sizeof(char));
}
else
{
    // TODO strip the spaces from the string and handle hex searches
    printf("hex-search not implemented yet.\n");
    return NULL;
}

// loop over all the bytes in the file looking for this ascii pattern
unsigned long long currentOffset = 0;
unsigned long long origOffset = 0;
unsigned long long m = 0;
foundWords = 0;
char* possibleWord = malloc(numBytes * sizeof(char));

do
{
    fseek(fp, currentOffset, SEEK_SET);
    unsigned long long i;
    int n = 0;
    int failed = 0;
    origOffset = currentOffset;

    for(i=currentOffset; i<currentOffset+numBytes; i++)
    {
        possibleWord[n] = fgetc(fp);
        n++;
    }
    //printf("possibleWord: %s\n", possibleWord);

    // is this our word? use strstr just in case
    char* found = strstr((const char*) byteString, (const char*) possibleWord);
    if (found)
    {
        foundWords++;
        // make a bigger spot for it
        printf("allocating %ld bytes to add word %d to list...\n", (numBytes*foundWords) * sizeof(char), foundWords);
        unsigned long long* p = realloc(foundBytes, (numBytes*foundWords) * sizeof(char));
        if (p)
        {
            foundBytes = p;

            for (i = origOffset; i<origOffset+numBytes; i++)
            {
                foundBytes[m] = i;
                //printf("added offset %llu to foundBytes[%llu]\n", i, m);
                m++;
            }

        }
        else
        {
            return NULL;
        }

    }
    else
    {
        failed = 1;
    }

    if (failed == 0)
    {
        currentOffset += numBytes;
        //printf("Yay! moving forward %ld bytes.\n", numBytes);
    }
    else
    {
        currentOffset++;
    }   
}
while (currentOffset < fileSize);

if (foundWords > 0)
{
    //printf("returning foundBytes!\n");

    //unsigned long long z;
    //for (z=0; z<foundWords*numBytes; z++)
    //  printf("%llu\n", foundBytes[z]);
    //printf("...\n");
    return foundBytes;
}
//printf("returning NULL\n");
return NULL;
}

when run on /etc/passwd using "root" as search pattern:

allocating 4 bytes to add word 1 to list...
allocating 8 bytes to add word 2 to list...
*** glibc detected *** ./chex3: realloc(): invalid next size: 0x0000000001a59270 ***

or on /etc/passwd using "daemon" as search pattern:

allocating 6 bytes to add word 1 to list...
allocating 12 bytes to add word 2 to list...
chex3: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.

Can someone look at this and see if it looks OK? Thanks! I am a noob trying to learn :)

Was it helpful?

Solution

The solution was to decalure p up top with foundBytes and then change the realloc line to:

p = realloc(foundBytes, (numBytes*foundWords) * sizeof(*p));

I must admit I don't really understand why this is necessary, it seems like I allocating a lot more than enough this way. (adding 24 bytes each time instead of 3 for a 3 character search pattern)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top