localtime question
Mipam
mipam at ibb.net
Wed Aug 11 03:24:01 PDT 2004
On Wed, 11 Aug 2004, Giorgos Keramidas wrote:
> On 2004-08-11 10:44, Mipam <mipam at ibb.net> wrote:
> > > You'd have to use strftime() and a local buffer for that.
> >
> > I found an example and adjusted it:
> >
> > #include <time.h>
> > #include <stdio.h>
> >
> > int main()
> > {
> > struct tm *ptr;
> > time_t tm;
> > char str[60];
> > char str2[60];
> > char str3[60];
> >
> > tm = time(NULL)-86400;
> > ptr = localtime(&tm);
> > strftime(str ,100 , "%d",ptr);
> > strftime(str2 ,100 , "%m",ptr);
> > strftime(str3 ,100 , "%Y",ptr);
> > printf("%s %s %s\n",str3,str2,str);
> >
> > return 0;
> > }
> >
> > This runs just fine: 2004 08 10
> > I dont know what the 100 is good for?
>
> It's the size of the buffer that strftime() gets as the first argument.
> In this case 100 is a bug waiting to happen, because the buffers are
> allocated with only 60 bytes of data. The manpage of strftime()
> explains what each argument is supposed to be.
Okay, so i should do:
strftime(str ,60 , "%d",ptr);
Could i do a check, i mean:
ptr = localtime(&tm); here i assign the output of locatime... to
ptr and ptr is a string of 60 characters, what if for some reason
localtime(&tm) exceeds the 60 characters, then i have a nice buffer
overflow. Can i do a check before doing: ptr = localtime(&tm); whether
localtime(&tm) does not exceed 60 characters?
When i done that check, i dont need an additional check in the strftime,
where also 60 is assigned.
Bye,
Mipam.
More information about the freebsd-questions
mailing list