1

I am trying to add a week repeatedly to an event struct, which contains date information. I am doing this in order to create a multiple instances of an event up until a certain time. I'm getting a seg fault on my mktime function where full_time = mktime(&caltime); and I have no clue why.

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;
    }

The icalevent structure:

    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

Edit:

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;
}
4
  • How is cpystruct defined? Commented Dec 2, 2012 at 22:18
  • 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; } Commented Dec 3, 2012 at 3:33
  • I added cpystruct to your question. It's easier to read there. You can easily do that yourself. Just use the small "edit" link below the question. Commented Dec 3, 2012 at 6:58
  • You could improve the reliability of the cpystruct code by using sizeof(temp.start) instead of 9, etc. You could also use memmove() or memcpy() instead of strncpy(). But it isn't obvious that there's a problem in that function. You don't check the return value from strptime(); if that fails, you've got a seriously misinitialized variable that you're passing to mktime(). Commented Dec 3, 2012 at 7:12

1 Answer 1

1

Most likely, your problem is not mktime, but one of your copy statements. First one is

strncat(time_str, temp.stime, 6);

with this time_str is likely not NUL terminated.

Same here

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

temp.end and temp.start might be NUL terminated, when they were before, but you can't be sure. Just use strcpy instead.

Next one is

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

I see no check for the end of calendar. So there might be a write beyond the end of calendar.

As an aside, when you do

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

tm_hour might become negative.

6
  • Okay, I've made the changes to the following strncpys and strncats. I made sure to check to calendar and it is fine because once it is totally full memory is reallocated with realloc(). If tm_hour becomes negative and we call mktime() again, won't it reset the hours back to the proper format? Commented Dec 2, 2012 at 23:13
  • strcpy(time_str, temp.start); strcat(time_str, temp.stime); 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); strftime(buffer, sizeof(buffer), "%Y%m%d", &caltime); strncpy(temp.start, buffer,8); temp.start[8] = '\0'; strncpy(temp.end, buffer, 8); temp.end[8] = '\0'; Commented Dec 2, 2012 at 23:15
  • @user1871057 You're right about mktime. According to mktime, it normalizes struct tm fields. Commented Dec 2, 2012 at 23:19
  • ohh okay, do you have any other suggestions for why there would be a seg fault. When I run the debugger it throws a seg fault error on the same line with the mktime function. Commented Dec 2, 2012 at 23:32
  • @user1871057 Besides the suggestions I've given, not really. Depending on how cpystruct is defined, there could be a problem as well. But that is just guesswork. Commented Dec 2, 2012 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.