简体   繁体   English

我的程序有 AddressSanitizer 错误,但我不知道如何阅读

[英]My program has AddressSanitizer error but I'm not sure how to read it

I'm relatively new to C and just finished a train station schedule program that looks for the nearest time departing time, from a schedule file, relative to the user input time, source, and destination.我对 C 比较陌生,刚刚完成了一个火车站时刻表程序,该程序从时刻表文件中查找最近的出发时间,相对于用户输入的时间、来源和目的地。 The restrictions were no string.h and no dynamic memory allocation of any kind.限制是没有 string.h 并且没有任何类型的动态 memory 分配。 The program worked fine when I tested it but when marked, it produced AddressSanitizer errors which we haven't learned yet.该程序在我测试时运行良好,但在标记时,它产生了我们尚未了解的 AddressSanitizer 错误。 I'm not sure how to read the error code.我不确定如何阅读错误代码。 The Following is my code:以下是我的代码:

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

#define lineSize 4096

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

if(argc != 4){
    printf("Please provide <source> <destination> <time> as command line arguments\n");
    return(0);
}

int c = 0;
while(argv[3][c] != 0){
    c++;
}
if(c < 8){
    printf("Please enter a proper time\n");
    return(0);
}

char line[lineSize];
FILE * fpointer = fopen("timetable.list", "r");

int i;
int j;

int StartSize;
int DestinationSize;
int DepartTime;

for(StartSize = 0; argv[1][StartSize] != '\0'; StartSize += 1){
    continue;
}

for(DestinationSize = 0; argv[2][DestinationSize] != '\0'; DestinationSize += 1){
    continue;
}

for(DepartTime = 0; argv[3][DepartTime] != '\0'; DepartTime += 1){
    continue;
}

int hour = 23;
int min = 59;
int sec = 59;
int check = 1;
int matchStart = 0;
int matchEnd = 0;
int checkNearestTime = 0;
int isThereATime = 0;

while(fgets(line, lineSize, fpointer) != NULL){
    check = 1;

    for(i = 0; i < StartSize; i++){
        if(argv[1][i] == line[i]){
            matchStart = 1;
            continue;
        }
        else{
            check = 0;
            matchStart = 0;
        }
    }

  
    if(check == 1){
        i = 0;
        int j = StartSize + 2;

        while(i < DestinationSize){
            if(argv[2][i] == line[j]){
                i += 1;
                j += 1;
                matchEnd = 1;
            }
            else{
                check = 0;
                matchEnd = 0;
                break;
            }
        }
    }


    if(check == 1){
        i = 0;
        j = StartSize + DestinationSize + 4;
        while(i < DepartTime){
            if(argv[3][i] == line[j]){
                i += 1;
                j += 1;
            }
            else{
                check = 0;
                break;
            }
        }
        if(check == 1){
            printf("The next train to %s from %s departs at %s\n", argv[2], argv[1], argv[3]);
            return 0;
        }

        if(check == 0){
            j = StartSize + DestinationSize + 4;
            int lineFullHour = (line[j] - '0') * 10 + (line[j+1] -'0');
            int lineFullMin = (line[j+3] - '0') * 10 + (line[j+4] -'0');
            int lineFullSec = (line[j+6] - '0') * 10 + (line[j+7] -'0');
            int argvHour = (argv[3][0] - '0') * 10 + (argv[3][1]-'0');
            int argvMin = (argv[3][3] - '0') * 10 + (argv[3][4] -'0');
            int argvSec = (argv[3][6] - '0') * 10 + (argv[3][7] -'0');
                          
            if(lineFullHour > argvHour){
                if(lineFullHour < hour){
                    hour = lineFullHour;
                    min = lineFullMin;
                    sec = lineFullSec;
                    checkNearestTime = 1;
                }
                else if(lineFullHour == hour){
                    if(lineFullMin < min){
                        min = lineFullMin;
                        sec = lineFullSec;
                        checkNearestTime = 1;
                    }
                    else if(lineFullMin == min){
                        if(lineFullSec < sec){
                            sec = lineFullSec;
                            checkNearestTime = 1;
                        }
                    }
                }
            }

            else if(lineFullHour == argvHour && lineFullMin > argvMin){
                if(lineFullHour < hour){
                    hour = lineFullHour;
                    min = lineFullMin;
                    sec = lineFullSec;
                    checkNearestTime = 1;
                }
                else if(lineFullHour == hour){
                    if(lineFullMin < min){
                        min = lineFullMin;
                        sec = lineFullSec;
                        checkNearestTime = 1;
                    }
                    else if(lineFullMin == min){
                        if(lineFullSec < sec){
                            sec = lineFullSec;
                            checkNearestTime = 1;
                        }
                    }
                }
            }

            else if(lineFullHour == argvHour && lineFullMin == argvMin && lineFullSec > argvSec){
                if(lineFullHour < hour){
                    hour = lineFullHour;
                    min = lineFullMin;
                    sec = lineFullSec;
                    checkNearestTime = 1;
                }
                else if(lineFullHour == hour){
                    if(lineFullMin < min){
                        min = lineFullMin;
                        sec = lineFullSec;
                        checkNearestTime = 1;
                    }
                    else if(lineFullMin == min){
                        if(lineFullSec < sec){
                            sec = lineFullSec;
                            checkNearestTime = 1;
                        }
                    }
                }
            }

        }
    }

    if(matchStart == 1 && matchEnd == 1 && checkNearestTime == 1){
        isThereATime = 1;
    }

}

if(isThereATime == 1){
    
    printf("The next train to %s from %s departs at %02d:%02d:%02d\n", argv[2], argv[1], hour, min, sec);
    return 0;
}
if(isThereATime == 0){
    printf("No suitable trains can be found\n");
    return 0;
}

return 0;
}

错误描述

The source code (line 53) and your report do not match (line 59) do not match.源代码(第 53 行)和您的报告不匹配(第 59 行)不匹配。 It complains about a NULL pointer, per @kaylum, that only happens if fpointer is NULL.它抱怨 NULL 指针,每个@kaylum,只有当 fpointer 是 NULL 时才会发生。 In turn this means │FILE * fpointer = fopen("timetable.list", "r");反过来这意味着│FILE * fpointer = fopen("timetable.list", "r"); failed.失败的。 Always implement error checking:始终实施错误检查:

#include <errno.h>
#include <string.h>

...

FILE * fpointer = fopen("timetable.list", "r");
if(!fpointer) {
  fprintf(stderr, "fopen failed with %s", strerror(errno)));
  exit(1);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的程序无限循环,我老实说不清楚为什么 - My program is looping infinitely, and I'm honestly not sure why 当我运行我的程序时,我收到“Segmentation fault 11”错误。 我不确定我的代码中的什么会导致此错误 - When I run my program I get the 'Segmentation fault 11' error. I'm not sure what in my code would cause this error 学校课程测试无法正确覆盖我的代码,我不确定为什么 - School program tests not covering my code correctly and I'm not sure why 我的阅读程序中不断显示“双重释放或损坏(输出)”,我不确定它是什么意思(已修复) - I keep getting shown “double free or corruption (out)” in my reading program and I'm not sure what it means (fixed) 我不确定为什么该程序无法编译 - I'm not sure why this program won't compile “输入末尾解析错误”,但我确定方括号正确。 (在C中) - “parse error at end of input”, but I'm sure my brackets are correct. (in C) 不确定为什么我尝试关闭程序时提示提示错误? - Not sure why my program keeps prompting error when I try to close it? 我实际上如何使用AddressSanitizer和MemorySanitizer? - How can I practically use AddressSanitizer and MemorySanitizer? 如何确保程序只能访问它自己的文件夹和子文件夹? - How do I make sure a program only has access to it's own folder and subfolders? 如何在C程序中捕获此“此应用程序已请求运行时以异常方式终止它”错误? - How can I catch this “This application has requested the Runtime to terminate it in an unusual way” error in my C program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM