簡體   English   中英

mktime()段錯誤

[英]mktime() seg fault

我試圖將一個星期重復添加到包含日期信息的事件結構中。 我這樣做是為了創建一個事件的多個實例,直到某個時間為止。 我的mktime函數出現段錯誤,其中full_time = mktime(&caltime); 我不知道為什么。

void multiple(icalevent event, int is_location){
    icalevent temp;
    int rrule_bound = atoi(event.rrule);
    int rtime_bound = atoi(event.rtime);
    int start_bound = atoi(event.start);
    int stime_bound = atoi(event.stime);
    char buffer[9];
    struct tm caltime;
    time_t full_time;
    char time_str[15];

    temp = cpystruct(event, is_location); 
    while((start_bound <= rrule_bound) && (stime_bound <= rtime_bound)){
        memset(&caltime, 0, sizeof(struct tm));
        strncpy(time_str, temp.start, 9);
        strncat(time_str, temp.stime, 6);

        strptime(time_str, "%Y%m%d%H%M%S", &caltime);
        caltime.tm_mday += 7;
        full_time = mktime(&caltime);
        if(caltime.tm_isdst == 1){
            caltime.tm_hour -= 1;
        }
        full_time = mktime(&caltime);

        strftime(buffer, 9, "%Y%m%d", &caltime);
        start_bound = atoi(buffer);
        strncpy(temp.end, buffer, 8);
        strncpy(temp.start, buffer, 8);

        if((start_bound <= rrule_bound) && (stime_bound <= rtime_bound)){
            /*create a sort string*/

            calendar[percent_full] = cpystruct(temp, is_location);
            printst(calendar[percent_full]);
            percent_full++;
        }
        else{
            break;
        }

    }

    return;
    }

icalevent結構:

    typedef struct{
        char start[9]; /*"YYYYMMDD*/
        char stime[7]; /*"HHMMSS"*/
        char end[9]; /*"YYYYMMDD"*/
        char etime[7]; /*"HHMMSS"*/
        char rrule[9]; /*"YYYYMMDD"*/
        char rtime[7]; /*"HHMMSS"*/
        char *location; /*"2343 fake street"*/
        char *summary; /*"Halloween party"*/
        char *sort_str; /*"YYYYMMDDHHMMSSHalloween party*/
     } icalevent

編輯

icalevent cpystruct(icalevent temp, int is_location) {
    icalevent perm;
    strncpy(perm.start, temp.start, 9);
    strncpy(perm.stime, temp.stime, 7);
    strncpy(perm.end, temp.end, 9);
    strncpy(perm.etime, temp.etime, 7);
    strncpy(perm.rrule, temp.rrule, 9);
    strncpy(perm.rtime, temp.rtime, 7);
    if(is_location) {
        perm.location = strdup(temp.location);
    } else {
        perm.location = NULL;
    }

    perm.summary = strdup(temp.summary);
    perm.sort_str = strdup(temp.sort_str);
    return perm;
}

您的問題很可能不是mktime ,而是您的復制語句之一。 第一個是

strncat(time_str, temp.stime, 6);

使用此time_str可能不會被NUL終止。

同樣在這里

strncpy(temp.end, buffer, 8);
strncpy(temp.start, buffer, 8);

temp.endtemp.start可能是NUL終止的,但您不能確定。 只需使用strcpy

下一個是

calendar[percent_full] = cpystruct(temp, is_location);
printst(calendar[percent_full]);
percent_full++;

我看不到calendar結尾的支票。 因此,日歷末尾可能會有寫操作。

順便說一句,當你做

if(caltime.tm_isdst == 1){
    caltime.tm_hour -= 1;
}

tm_hour可能變為負數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM