2012-12-18 6 views
0

패턴의 모든 항목을 검색하고 패턴과 일치하는 파일의 오프셋 배열을 반환하는 함수를 작성하려고합니다. 반환 된 배열을 동적으로 증가시키기 위해 realloc을 사용하고 싶지만 sYSMALLOC 어설 션 오류가 발생합니다. 때로는 다른 검색 패턴을 사용하면 잘못된 다음 크기 오류가 발생할 수 있습니다. 나는 아직 내 컴퓨터에 valgrind를 가지고 있지 않다 (디버그 플래그로 glibc를 재구성해야한다). 이 간단한 문제처럼 보입니다.이 포인터는 두 번 만 터치합니다. 한 번 선언하고 NULL로 설정 한 다음 다시 할당하여 다시 성장 시키십시오. 나는 sizeof (char)가 1이라는 것을 알고, 내 realloc 문장에는 쓸모가 없다.C - sYSMALLOc : 어설 션 실패 (realloc)

다음은 문제가있는 기능의 코드입니다.

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. 
: 검색 패턴으로/"디먼에"사용 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 : 기타/실행

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

는/검색 패턴으로 "루트"를 사용은 passwd

누군가가 이것을보고 괜찮은지 확인할 수 있습니까? 감사! 나는 이것이 왜 난 정말 이해가 안 인정해야

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

: 나는 멍청한 놈이 솔루션은 foundBytes으로 페이지까지 정상을 decalure 다음에 realloc을 라인을 변경했다 :)

+0

'strlen()'은 모든 문자열의 끝에서'NUL' ('\\ 0 ') 문자를 센다. 또한, allocof counts를'sizeof (char)'로 곱하면 안되며 항상 반드시 1이어야한다. –

답변

0

배우려고 노력하고 있어요 필요한만큼, 나는이 정도면 충분하다고 생각한다. (3 문자 검색 패턴에 대해 3 대신에 24 바이트를 더하는 것)