Home > Back-end >  Is "segmentation fault" caused from fopen line a stack overflow problem in C? (darknet, de
Is "segmentation fault" caused from fopen line a stack overflow problem in C? (darknet, de

Time:04-29

I am experiencing a darknet YOLOv4. I want to add detection result logging line to this mAP example code. I think I wrote a line that writes files according to the grammar, but the "segmentation fault" error occurs in the line where "fopen" is operated. The line like printf works well.

The validate_detector_map() part of the detector.c of darknet is attached below.

https://github.com/AlexeyAB/darknet/blob/master/src/detector.c#L940

And below is the part I added.(////ju added~ //////////)

...    // For multi-class precision and recall computation
float *avg_iou_per_class = (float*)xcalloc(classes, sizeof(float));
int *tp_for_thresh_per_class = (int*)xcalloc(classes, sizeof(int));
int *fp_for_thresh_per_class = (int*)xcalloc(classes, sizeof(int));

for (t = 0; t < nthreads;   t) {
    args.path = paths[i   t];
    args.im = &buf[t];
    args.resized = &buf_resized[t];
    thr[t] = load_data_in_thread(args);
}

////ju added //파일 열려야 하는 위치
printf("\nlogging start.. \n\n");

FILE *log_csv;
printf('%d\n', fileno(log_csv));
if(!(fopen("/home/aicar/darknet/0_log_ju0.csv", 'a')))
{
    printf("failed to open csv file\n\n");
    exit(1);
    
}
else
{
    printf("csv file opened\n\n");
    fprintf(log_csv, "timestamp, class_id, gt, conf_mat, \n" ); //header row
    fclose(log_csv);
}
//////////
time_t start = time(0);

for (i = nthreads; i < m   nthreads; i  = nthreads) { ......

Is this a stack overflow problem? The reason I thought that was because the author used the free function.

CodePudding user response:

fopen returns a file handle. Just a couple lines above you're defining a variable FILE *log_csv; which would be probably where you want to put that handle.

Furthermore the second argument of fopen is a C-string (i.e. a pointer to a NUL terminated array of chars). TTBT, I'm surprised your compiler did let this pass.

Try this:

FILE *log_csv = fopen("/home/aicar/darknet/0_log_ju0.csv", "a");
if( !log_csv ){
    printf("failed to open csv file\n\n");
    exit(1);
} else {
    printf("%d\n", fileno(log_csv));
    …
}

CodePudding user response:

This code is also dangerous:

FILE *log_csv;
printf('%d\n', fileno(log_csv));

The fileno() function will attempt to dereference log_csv, which does not point to a FILE structure and therefore extremely likely to cause a SIGSEGV crash.

  • Related