0
我正在嘗試編寫一個函數,用於搜索所有出現的模式,並返回與該模式匹配的文件中的偏移量數組。我想使用realloc動態增長我返回的數組,但我得到sYSMALLOC斷言錯誤。有時候,如果我使用不同的搜索模式,我可能會收到無效的下一個大小錯誤。我的機器上沒有valgrind(需要用調試標誌重建glibc)。這似乎是一個這樣簡單的問題,我只觸摸這個指針兩次 - 一次聲明它並將其設置爲NULL,並再次重新分配以增加它。我知道sizeof(char)是1,在我的realloc語句中沒有用處。C - sYSMALLOc:聲明失敗(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;
}
時上運行/ 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 ***
或上/ 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.
有人可以看看這個,看看它看起來好嗎?謝謝!我是一個小白努力學習:)
'strlen()'不計算每個字符串末尾的尾部NUL(''\ 0'')字符。另外,不要用'sizeof(char)'乘以alloc計數,它總是爲'1'。 –