B
bcpkh
[C application running on TRU64 Unix]
Hello All
I have a simple task that is driving me crazy. A string representing a
duration in the following format is passed to my application, a
function is dedicated to convert this duration to seconds;
[H]H:MM:SS
e.g. 0:00:00 or 00:12:45
The original function used atoi as part of the conversion process and
it produced some scary conversions 0:00:10 converted to 956 seconds
etc.
The function was rewritten to make use of strtol because of its better
error handling functionality but it now core dumps! Do someone have a
bullet proof idea to do this conversion assuming that anything might
be passed into this function;
e.g. A:00:00 or :0:45 etc
Reproducted below is the offending function the lines where it
currently dumps is marked with a @.
Any help would be much appreciated.
Thank you,
B
void secondDurations()
{
long hr, min, sec, sec_duration = 0;
char tm[10];
int ch = ':';
char *end_ptr = NULL;
char duration[41];
char *pfirst;
char *plast;
// 00:00:00
// 0:00:00
// 01234567
strcpy(duration, "00:00:34");
//String should at leat be 7 characters long
if (strlen(duration) >= 7)
{
pfirst = strchr(duration, ch);
plast = strrchr(duration, ch);
//Test duration format 00:00:00
if((pfirst != NULL || plast != NULL) && (pfirst != plast))
{
errno = 0;
//Get hour portion
strcpy(tm, strtok(duration,":"));
//Convert
hr = strtol(tm, &end_ptr, 10);
//Test
if (hr > INT_MAX || hr < INT_MIN)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
errno = 0;
@ strcpy(tm, strtok(NULL,":"));
min = strtol(tm, &end_ptr, 10);
if (min > INT_MAX || min < INT_MIN || min > 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
errno = 0;
@ strcpy(tm, strtok(NULL,":"));
sec = strtol(tm, &end_ptr, 10);
if (sec > INT_MAX || sec < INT_MIN || sec > 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
sec_duration = hr * 3600 + min * 60 + sec;
printf("duration in seconds=%d\n",(int)sec_duration);
}
}
}
}
}
Hello All
I have a simple task that is driving me crazy. A string representing a
duration in the following format is passed to my application, a
function is dedicated to convert this duration to seconds;
[H]H:MM:SS
e.g. 0:00:00 or 00:12:45
The original function used atoi as part of the conversion process and
it produced some scary conversions 0:00:10 converted to 956 seconds
etc.
The function was rewritten to make use of strtol because of its better
error handling functionality but it now core dumps! Do someone have a
bullet proof idea to do this conversion assuming that anything might
be passed into this function;
e.g. A:00:00 or :0:45 etc
Reproducted below is the offending function the lines where it
currently dumps is marked with a @.
Any help would be much appreciated.
Thank you,
B
void secondDurations()
{
long hr, min, sec, sec_duration = 0;
char tm[10];
int ch = ':';
char *end_ptr = NULL;
char duration[41];
char *pfirst;
char *plast;
// 00:00:00
// 0:00:00
// 01234567
strcpy(duration, "00:00:34");
//String should at leat be 7 characters long
if (strlen(duration) >= 7)
{
pfirst = strchr(duration, ch);
plast = strrchr(duration, ch);
//Test duration format 00:00:00
if((pfirst != NULL || plast != NULL) && (pfirst != plast))
{
errno = 0;
//Get hour portion
strcpy(tm, strtok(duration,":"));
//Convert
hr = strtol(tm, &end_ptr, 10);
//Test
if (hr > INT_MAX || hr < INT_MIN)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
errno = 0;
@ strcpy(tm, strtok(NULL,":"));
min = strtol(tm, &end_ptr, 10);
if (min > INT_MAX || min < INT_MIN || min > 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
errno = 0;
@ strcpy(tm, strtok(NULL,":"));
sec = strtol(tm, &end_ptr, 10);
if (sec > INT_MAX || sec < INT_MIN || sec > 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != '\0')
{
//Reject
return;
}
else
{
sec_duration = hr * 3600 + min * 60 + sec;
printf("duration in seconds=%d\n",(int)sec_duration);
}
}
}
}
}