gpt4 book ai didi

使用gdb调试器出现c代码段错误 "SIGSEGV"

转载 作者:行者123 更新时间:2023-11-30 20:16:04 28 4
gpt4 key购买 nike

我有一个代码可以扫描整个目录中的 .wor 文件,并从中提取信息。当我在包含 5 个 .wor 文件的目录上运行该文件时,它工作得很好。但是,当我尝试在包含 70 多个 .wor 文件的目录上运行它时,它给出了“内存故障(coredump”错误)。

所以我尝试使用 gdb 调试代码,这就是我得到的结果:

Program received signal SIGSEGV, Segmentation fault.
0x7af9fcbc in _sbrk () from /usr/lib/libc.2
(gdb) bt
#0 0x7af9fcbc in _sbrk () from /usr/lib/libc.2
#1 0x7af9db7c in _sigfillset () from /usr/lib/libc.2
#2 0x7af9bfe8 in __thread_callback_np () from /usr/lib/libc.2
#3 0x7afa0e64 in malloc () from /usr/lib/libc.2
#4 0x7b001160 in _findbuf () from /usr/lib/libc.2
#5 0x7afff7f4 in _filbuf () from /usr/lib/libc.2
#6 0x7afff404 in __fgets_unlocked () from /usr/lib/libc.2
#7 0x7afff5fc in fgets () from /usr/lib/libc.2
#8 0x2c14 in main (argc=1, argv=0x7dff11b4) at book2.c:99

我查看了第 99 行,我有命令: while( fgets(buff, 100,fs) != NULL ),我对 c 不太了解,无法找到错误所在,这里的任何帮助将不胜感激,提前致谢!

这是我的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>


int main(int argc, char **argv) {

char directory[100];
char buff[100];
char delims[] = " :=";
char *result = NULL;
char *customer;
char *device;
char *testprog;
char *software;
char *dutboardid;
char *corlbox;
char *testerid;
char *temp;
char *buffer;
int i=0;
DIR * FD;
struct dirent* in_file;
int c=0;


printf ("Enter directory:");
scanf ("%s",directory);


FILE * ft = fopen ( "workorderlist.csv", "w" ) ; /* Open file to write to*/
if ( ft == NULL )
{
puts ( "Cannot open target file" ) ;
exit( 1 ) ;
}


fprintf (ft, "Work Order,Customer,Device,Test Program,Software,DUT_board_id,Corl box, Tempurature, Test Station\n");



/* Open Directory*/

if (NULL == (FD = opendir (directory)))
{

puts ("Cannot open directory");

return 1;
}


while ((in_file = readdir(FD)))
{

if (!strcmp (in_file->d_name, "."))
continue;
if (!strcmp (in_file->d_name, ".."))
continue;



/* Open files to read from */

buffer = (char*) malloc (200);
sprintf(buffer, "%s/%s", directory, in_file->d_name);


size_t len = strlen(buffer);
if (len >= 4 && memcmp(buffer + len - 4, ".wor", 4) == 0) /* checks if file ends with .wor */
{

FILE * fs = fopen(buffer, "r"); /* open file to read */

customer = "0";
device = "0";
testprog = "0";
software = "0";
dutboardid = "0";
corlbox = "0";
testerid = "0";
temp = "0";

if (fs == NULL)
{
puts ("Cannot open source file");

return 1;
}

/* Scanning each file for targeted words: */


while( fgets(buff, 100,fs) != NULL )
{



result = strtok( buff, delims );
while(result != NULL){


if((strcmp(result,"Customer")==0)){
result = strtok(NULL,delims);
customer = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(customer, result);
for(i=0;i<strlen(customer)+1;i++){ if(customer[i] == '\n') break; }
customer[i] = ' ';
}

if((strcmp(result,"name")==0)){
result = strtok(NULL,delims);
customer = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(customer, result);
for(i=0;i<strlen(customer)+1;i++){ if(customer[i] == '\n') break; }
customer[i] = ' ';
}


if(strcmp(result,"device")==0){
result = strtok(NULL,delims);
device = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(device, result);
for(i=0;i<strlen(device)+1;i++){ if(device[i] == '\n') break; }
device[i] = ' ';
}


if(strcmp(result,"test_prog")==0 || strcmp(result, "testprog")==0){
result = strtok(NULL,delims);
testprog = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(testprog, result);
for(i=0;i<strlen(testprog)+1;i++){ if(testprog[i] == '\n') break; }
testprog[i] = ' ';
}


if(strcmp(result,"Rev")==0 || strcmp(result,"use")==0){
result = strtok(NULL,delims);
software = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(software, result);
for(i=0;i<strlen(software)+1;i++){ if(software[i] == '\n') break; }
software[i] = ' ';
}

if(strcmp(result,"rev")==0){
result = strtok(NULL,delims);
software = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(software, result);
for(i=0;i<strlen(software)+1;i++){ if(software[i] == '\n') break; }
software[i] = ' ';
}

if(strcmp(result,"DUT_board_id")==0){
result = strtok(NULL,delims);
dutboardid = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(dutboardid, result);
for(i=0;i<strlen(dutboardid)+1;i++){ if(dutboardid[i] == '\n') break; }
dutboardid[i] = ' ';
}

if(strcmp(result,"box")==0){
result = strtok(NULL,delims);
corlbox = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(corlbox, result);
for(i=0;i<strlen(corlbox)+1;i++){ if(corlbox[i] == '\n') break; }
corlbox[i] = ' ';
}

if(strcmp(result,"Tester_id")==0){
result = strtok(NULL,delims);
testerid = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(testerid, result);
for(i=0;i<strlen(testerid)+1;i++){ if(testerid[i] == '\n') break; }
testerid[i] = ' ';
}

if(strcmp(result,"@")==0){
result = strtok(NULL,delims);
temp = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(temp, result);
for(i=0;i<strlen(temp)+1;i++){ if(temp[i] == '\n') break; }
temp[i] = ' ';
}


result = strtok(NULL,delims);
}

}

if (strcmp(customer, "0")==0){
customer = "N/A";
}
if (strcmp(device, "0")==0){
device = "N/A";
}
if (strcmp(testprog, "0")==0){
testprog = "N/A";
}
if (strcmp(software,"0")==0){
software = "N/A";
}
if (strcmp(dutboardid, "0")==0){
dutboardid = "N/A";
}
if (strcmp(corlbox, "0")==0){
corlbox = "N/A";
}
if (strcmp(testerid, "0")==0){
testerid = "N/A";
}
if (strcmp(temp, "0")==0){
temp = "N/A";
}

fprintf (ft, "%s,%s,%s,%s,%s,%s,%s,%s,%s\n", in_file->d_name, customer, device, testprog, software, dutboardid, corlbox, temp, testerid);


printf(in_file->d_name);
printf("\n");
fclose (fs) ;
c++;

}
}
printf("Total Workorders Found: %d (Info saved to workorderlist.csv)\n", c);
fclose ( ft ) ;
return 0;

}

最佳答案

如果我们举这个例子:

customer = (char*)malloc((strlen(result)+1)*sizeof(char));
strcpy(customer, result);
for(i=0;i<strlen(customer)+1;i++){ if(customer[i] == '\n') break; }
customer[i] = ' ';

它复制结果,占用strlen(result)+1字节。然后您将结果复制到客户中。之后,您在字符串中搜索“\n”。如果字符串中没有 '\n' 会发生什么? i 是什么值?

我很确定问题在于,在某些时候,这种风格的代码是在分配的内存之外编写的 - 因为我不确切知道您的输入是什么,所以我无法说出其中的哪一个这段代码的几个副本都出了问题,但我敢打赌这就是发生的情况。这反过来意味着堆结构本身中的某些数据正在被损坏,因此在调用 malloc 时的某个时刻,它会在代码中使用这些坏数据。

关于使用gdb调试器出现c代码段错误 "SIGSEGV",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14369369/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com