0

ファイルから行を読み取るために fgets() を使用しています。ファイルの数行を読み取ることができ、その後 fgets() がアクセス違反を返します。ファイル バッファに問題があると思われるかもしれませんが、私のコードでわかるように、そうではありません。私が気づいた奇妙な動作の 1 つは、タイトなループでファイルのすべての行を読み取って出力しても、問題がないことです。この問題をデバッグするためにいくつかの printf() ステートメントを使用しましたが、実行されるループによってファイルの位置が異なることに気付きました。FILE * は、私の「完全なループ」ロジックでは触れられていません。

タイト ループ ファイルの位置は、0、27、53、80、82、99、127、155 などです。

完全なループ ファイルの位置は次のとおりです: 0、27、53、80、82、99、138

入力ファイル:

!!!!!!!!!!!!!!!!!!!!!!!!!
! Test sparc gagdet file
!!!!!!!!!!!!!!!!!!!!!!!!!

! instruction 1
1: subcc     %g0, %i4, %i4
1: subc      %g0, %i4, %i4 ** access violation reading this line **

! instruction 2
** etc. **

コード:

/*
* parse_profile: Parse the gadget profile and load the memory structures required to scan the library file
*/
int parse_profile(FILE * gadget_file, struct g_handle * gadget_handle){

// Buffers used to temporarily store file imput
char op_code [NODE_BUF_SIZE] = "\0";
char reg [NODE_BUF_SIZE] = "\0";

// Reference nodes in the bod_ops and save_regs lists
struct char_node * temp_node = NULL;
struct char_node * op_node = NULL;
struct char_node * reg_node = NULL;

// 
int level = 1;
int old_level = 1;
int curr_line = 0;

// A buffer to hold file data
char file_buffer [PAGE_SIZE];

// Reference nocdes in the instruction tree
struct instruction_node * current_node = NULL;
struct instruction_node * prev_node = NULL;
struct instruction_node * prev_level = NULL;

// Read a line from the gadget file (data for a single instruction)
//while(fgets(file_buffer, PAGE_SIZE, gadget_file) != NULL){
char * shiz = file_buffer;
while(shiz != NULL){
printf("\n file location: %d", ftell(gadget_file));
fflush(stdout);
shiz = fgets(file_buffer, PAGE_SIZE, gadget_file);
/*
// tight loop with different file position
while(shit != NULL){
printf("\n file location: %d", ftell(gadget_file));
fflush(stdout); 
shiz = fgets(file_buffer, PAGE_SIZE, gadget_file);
}
*/
    // Increment the current line
    curr_line = curr_line + 1;
printf("\nline (%d)", curr_line);
fflush(stdout);     
    // Ensure we have gathered the entire line of the file
    if(strlen(file_buffer) >= PAGE_SIZE){

        // We have exceeded the maximum line size,  quit
        printf("\nError reading gadget profile, Line %d: Maximum line length of 4096 has been exceeded", curr_line);
        return(-1);

    } // Ensure we have gathered the entire line of the file

    // If this is a comment
    if(*file_buffer == '!'){

        // Do nothing
    }
    // If this is a blank line
    else if(sscanf(file_buffer," %s ") < 1){

        // Do nothing
    }
    // Scan the current line until we have saved all instructions
    else if(sscanf(file_buffer,"%d: %s", &level, op_code) == 2){
printf("\n file location: %d", ftell(gadget_file));
fflush(stdout);
printf("1");
fflush(stdout);     
        // Store instruction information
/*      
commented block
*/
    } // Scan the current line until we have saved all instruction/nibble pairs

    // Scan the current line until we have saved all  registers to be preserved
    else if(sscanf (file_buffer,"r: %s", reg) == 1){
/*
commented block
*/          
    } // Scan the current line until we have saved all  registers to be preserved

    // Scan the current line until we have saved all  op_codes to be avoided
    else if(sscanf (file_buffer,"o: %s", op_code) == 1){
/*
commented block
*/

    } // Scan the current line until we have saved all  op_codes to be avoided

    else{

        // quit
        printf("\nError reading gadget profile, Line %d: \n%s", curr_line, file_buffer);
        return(-1); 
    }
printf("7");
printf("\n file location: %d", ftell(gadget_file));
fflush(stdout);     
} // Read a line from the gadget file (data for a single instruction) 
printf("a");
fflush(stdout);     
// If fread() returned an error, exit with an error
if(ferror(gadget_file) != 0){

    // Print error and exit
    printf("\nError reading gadget profile");
    return(-1);

} // If fread() returned an error, exit with an error   

return 0;
}
4

1 に答える 1

1

行に未定義の結果があります

else if(sscanf(file_buffer," %s ") < 1){

フォーマット指定子の数が、渡されたポインターの数を超えています。sscanfがスキャン結果を任意の場所に保存しようとして、ビット パターンが間違った場所にある可能性は十分にあります。

于 2012-04-05T15:38:14.060 に答える