C - sYSMALLOc:断言失败(realloc)

时间:2012-12-18 06:36:07

标签: c realloc assertion

我正在尝试编写一个函数来搜索所有出现的模式,并在文件中返回与模式匹配的偏移数组。我想使用realloc来动态增长我返回的数组,但是我收到了一个sYSMALLOC Assertion错误。有时如果我使用不同的搜索模式,我可能会得到无效的下一个大小错误。我的机器上还没有valgrind(需要用调试标志重建glibc)。这看起来像是一个简单的问题,我只触摸这个指针两次 - 一次声明它并将其设置为NULL并再次重新分配以增长它。我知道sizeof(char)是1,在我的realloc语句中没用。

以下是有问题的功能的代码。

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;
}

使用&#34; root&#34;在/ etc / passwd上运行时作为搜索模式:

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 ***

或使用&#34; daemon&#34;在/ etc / passwd上作为搜索模式:

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.

有人可以看一下,看看它看起来不错吗?谢谢!我是一个努力学习的菜鸟:)

1 个答案:

答案 0 :(得分:0)

解决方案是使用foundBytes将p顶部剥离,然后将realloc行更改为:

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

我必须承认我并不真正理解为什么这是必要的,似乎我分配了这么多的东西。 (每次添加24个字节而不是3个字符搜索模式的3个字节)