0

次のコードを作成しました。これは、印刷可能な ascii 用の単純なブルート フォース プログラムです。ユーザーは、開始パスフレーズの長さと終了パスフレーズの長さのパラメーターを渡します。プログラムを実行すると、次のエラーが表示されます。

入力:

~$ Enter START length & END length ex:(8 10): 2 3

出力:

... (more output above)
~|
~}
~~

*** glibc detected *** ./wordgen: double free or corruption (out): 0x0916e008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6cbe1)[0x874be1]
/lib/i386-linux-gnu/libc.so.6(+0x6e50b)[0x87650b]
/lib/i386-linux-gnu/libc.so.6(cfree+0x6d)[0x87969d]
./wordgen[0x80486f4]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x81ee37]
./wordgen[0x8048471]
======= Memory map: ========
00110000-0012a000 r-xp 00000000 08:06 3408733    /lib/i386-linux-gnu/libgcc_s.so.1Aborted

Valgrind 出力:

==11050== Invalid read of size 1
==11050==    at 0x804866F: main (wordgen.c:37)
==11050==  Address 0x41a2027 is 1 bytes before a block of size 3 alloc'd
==11050==    at 0x4026864: malloc (vg_replace_malloc.c:236)
==11050==    by 0x8048600: main (wordgen.c:28)
==11050== 
==11050== Invalid write of size 1
==11050==    at 0x8048675: main (wordgen.c:37)
==11050==  Address 0x41a2027 is 1 bytes before a block of size 3 alloc'd
==11050==    at 0x4026864: malloc (vg_replace_malloc.c:236)
==11050==    by 0x8048600: main (wordgen.c:28)
==11050== 
==11050== Invalid read of size 1
==11050==    at 0x8048689: main (wordgen.c:38)
==11050==  Address 0x41a2027 is 1 bytes before a block of size 3 alloc'd
==11050==    at 0x4026864: malloc (vg_replace_malloc.c:236)
==11050==    by 0x8048600: main (wordgen.c:28)
==11050==

C コード:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  int  sLen = 1;
  int  eLen = 0;

  printf("Enter START length & END length ex:(8 10): ");
  scanf("%d %d", &sLen, &eLen);
  int cLen = sLen;
  while (cLen <= eLen) {

    /* Allocate Memory for String  & Initialize */
    char *outStr = malloc(cLen + 1);
    memset(outStr, ' ', cLen);
    outStr[cLen] = 0;

    int outerControl = 1;
    while (outerControl == 1) {
      int cMod = 1;
      int innerControl = 1;
      while(innerControl == 1) {
        outStr[cLen-cMod] += 1;
        if((int)outStr[cLen-cMod] == 127) {
          //Exit Condition Where The Error Occurred
          if(cLen - cMod == 0) { outerControl = 0;  } 
          outStr[cLen-cMod] = 32;
          cMod += 1;
        }
        else { innerControl = 0; }
      }
      printf("%s\n",outStr);
    }
    free(outStr); // Possible source of Error?
    cLen += 1;
  }

  return 0;
}

私は C プログラミングが初めてで、このエラーに完全に困惑しています。どういう意味ですか?どうやってプログラムを間違って作成したのですか? 私はそれがメモリ管理と関係があると仮定しています...

4

2 に答える 2

3

あなたの問題は次のとおりです。

while(innerControl == 1) {
  printf("%d %d\n", cLen, cMod);
  outStr[cLen-cMod] += 1;                       // <-- this here
  if((int)outStr[cLen-cMod] == 127) {
    //Exit Condition Where The Error Occurred
    if(cLen - cMod == 0) { outerControl = 0;  }
    outStr[cLen-cMod] = 32;
    cMod += 1;
  }
  else { innerControl = 0; }
}

ある時点で、cModは よりも大きくなるため、その境界外 (つまり: )にcLenアクセスしています。この動作は未定義です。outStroutStr[-1]

この条件:

if(cLen - cMod == 0) { outerControl = 0;  }

...それを防ぐためにあるようですが、 の場合にのみ実行されます(int)outStr[cLen-cMod] == 127。おそらく次のようなものを追加する必要があります。

if (cMod > cLen)
    break;

outStr[cLen-cMod] += 1;
于 2012-09-10T17:13:39.947 に答える
0

ループの各パスで outStr を解放するため、 < 1 のwhile (cLen <= eLen)場合にのみ正しく機能します;)eLen - cLen

更新:申し訳ありませんが、私は間違っていました-パスごとにmallocします。

私はあなたのプログラムを試してみましたが、私の Mac ではエラーなく動作しました。valgrind を使用して、何が起こっているかを調べてみてください。

于 2012-09-10T16:56:44.433 に答える