-1

First off, I have looked through other similar questions and they don't help me. I have the following Valgrind output:

My Code:

int f_gozlemZamaniFarki(long long t1,long long t2)
{
    char *time1 = (char*)calloc(12,sizeof(char));
    char *time2 = (char*)calloc(12,sizeof(char));

    *time1 = '\0';
    *time2 = '\0';

    sprintf(time1,"%lli",t1);
    sprintf(time2,"%lli",t2);

    int year,month,day,hour,minute,timeDif;
    int status = 0;

    struct tm tm1;
    struct tm tm2;

    time_t timeF = NULL;
    time_t timeS = NULL;

    if(sscanf(time1,"%4d%2d%2d%2d%2d",&year,&month,&day,&hour,&minute) != EOF)
    {
        tm1.tm_year = year - 1900;
        tm1.tm_mon = month;
        tm1.tm_mdmonth = day;
        tm1.tm_hour = hour;
        tm1.tm_min = minute;
        tm1.tm_sec = 0;
        timeF = mktime(&tm1);
    }

    if(sscanf(time2,"%4d%2d%2d%2d%2d",&year,&month,&day,&hour,&minute) != EOF)
    {
        tm2.tm_year = year - 1900;
        tm2.tm_mon = month;
        tm2.tm_mdmonth = day;
        tm2.tm_hour = hour;
        tm2.tm_min = minute;
        tm2.tm_sec = 0;
        timeS = mktime(&tm2);
    }

    timeDif = (timeF - timeS) / 60;

    if ((timeDif < 0) || (timeDif > 59))
    {
        status = 1;
    }

    free(time1);
    free(time2);

    return status;
}

valngrid output

==12726== Invalid read of size 1
==12726==  Address 0x1786dccc is 0 bytes after a block of size 12 alloc'd
==12726==    at 0x4C2AE45: calloc (vg_replace_malloc.c:711)
==12726==    by 0x405171: f_gozlemZamaniFarki (in /home/aws/bin/awsdDataWriter)
==12726== Conditional jump or move depends on uninitialised value(s)
==12726==  Uninitialised value was created by a stack allocation
==12726==    at 0x405150: f_gozlemZamaniFarki (in /home/aws/bin/awsdDataWriter)

All my variables are initialized (i hope), so I don't understand why Valgrind is yelling at me. Do you have a suggestion to make the code right and to make the mistake?

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
hanifigoktas
  • 40
  • 1
  • 9

1 Answers1

0

I changed these lines, which solved my problem.

char *time1 = (char*)calloc(13,sizeof(char));
char *time1 = (char*)calloc(13,sizeof(char));

//*time1 = '\0';
//*time2 = '\0';
Jongware
  • 22,200
  • 8
  • 54
  • 100
hanifigoktas
  • 40
  • 1
  • 9