c - fgets 工作然后返回访问冲突

标签 c

我正在使用 fgets() 从文件中读取行。我能够读取文件的几行,然后 fgets() 返回访问冲突。人们可能会认为我的文件缓冲区存在问题,但正如您在我的代码中看到的那样,情况并非如此。我注意到的一个奇怪的行为是,如果我以紧密循环的方式读取并打印文件的所有行,则没有任何问题。我使用一些 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;
}

最佳答案

您有未定义的结果

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

格式说明符的数量超过了传递的指针的数量。 sscanf 很可能尝试将扫描结果存储在位模式错误的任意位置。

关于c - fgets 工作然后返回访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10031269/

相关文章:

c - 在 C 中使用 OpenSSL 填充 SSL_METHOD 结构

c++ - fputs()、fgets()、ferror() 问题和 C++ 等效项

使用 Bitshift 运算符将小端转换为大端

从 getopt 转换参数

c -/usr/bin/ld 找不到 -lfftw3?

c - ptrace(),如何停止在子进程中被跟踪?

c - 当被测可执行文件以0退出时,为什么Valgrind的退出代码为1?

c - fread函数只读大约700kb

c - 拦截linux pthread_create函数,导致JVM/SSH崩溃

c - 如何为单个变量定义不同的结构