From owner-freebsd-questions@FreeBSD.ORG Wed Aug 11 10:35:27 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 1258916A4CE for ; Wed, 11 Aug 2004 10:35:27 +0000 (GMT) Received: from kane.otenet.gr (kane.otenet.gr [195.170.0.27]) by mx1.FreeBSD.org (Postfix) with ESMTP id 50E7443D2F for ; Wed, 11 Aug 2004 10:35:26 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from orion.daedalusnetworks.priv (aris.bedc.ondsl.gr [62.103.39.226])i7BAZHY8009441; Wed, 11 Aug 2004 13:35:21 +0300 Received: from orion.daedalusnetworks.priv (orion [127.0.0.1]) i7BAZEWn068680; Wed, 11 Aug 2004 13:35:14 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost)i7BAZDFt068670; Wed, 11 Aug 2004 13:35:13 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Wed, 11 Aug 2004 13:35:13 +0300 From: Giorgos Keramidas To: Mipam Message-ID: <20040811103513.GA46089@orion.daedalusnetworks.priv> References: <20040810171119.GA26303@orion.daedalusnetworks.priv> <20040811094244.GA30843@orion.daedalusnetworks.priv> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: cc: freebsd-questions@freebsd.org Subject: Re: localtime question X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Aug 2004 10:35:27 -0000 On 2004-08-11 12:23, Mipam wrote: > On Wed, 11 Aug 2004, Giorgos Keramidas wrote: > > On 2004-08-11 10:44, Mipam wrote: > > > #include > > > #include > > > > > > 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, No, most certainly not. `ptr' is not a string of characters. It's a pointer to a (struct tm). > 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? In your program above `ptr' is a (struct tm *). It doesn't have anything to do with the character arrays `str', `str2' and `str3' which are declared below as vectors of 60 characters. > When i done that check, i dont need an additional check in the > strftime, where also 60 is assigned. You would still have to pass a valid buffer size to strftime() to avoid overflows. If you don't then there is no way for strftime() to know the size of the array that it gets as its first argument. By the way... Since freebsd-questions is not a place for lessons about programming in C, could I ask you to post future messages personally or to another mailing list more related to programming in C? - Giorgos