From owner-freebsd-questions@FreeBSD.ORG Wed Mar 21 15:35:31 2012 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 9AB9E106567D for ; Wed, 21 Mar 2012 15:35:31 +0000 (UTC) (envelope-from bmettee@pchotshots.com) Received: from mail.pchotshots.com (mail.pchotshots.com [12.172.123.237]) by mx1.freebsd.org (Postfix) with ESMTP id 53BDE8FC2C for ; Wed, 21 Mar 2012 15:35:30 +0000 (UTC) Received: (qmail 2910 invoked by uid 89); 21 Mar 2012 15:28:49 -0000 Received: from unknown (HELO ?12.172.123.228?) (bmettee@pchotshots.com@12.172.123.228) by mail.pchotshots.com with ESMTPA; 21 Mar 2012 15:28:49 -0000 Message-ID: <4F69F3AB.4010903@pchotshots.com> Date: Wed, 21 Mar 2012 11:28:43 -0400 From: Brad Mettee User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 MIME-Version: 1.0 To: Martin McCormick References: <201203211450.q2LEoPqM078354@dc.cis.okstate.edu> In-Reply-To: <201203211450.q2LEoPqM078354@dc.cis.okstate.edu> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: freebsd-questions@freebsd.org Subject: Re: Freebsd9.0 and the fgets directive in gcc X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Mar 2012 15:35:31 -0000 On 3/21/2012 10:50 AM, Martin McCormick wrote: > I've got some code which I wrote about 6 or 8 years ago that > apparently doesn't get along right now with FreeBSD9.0. In the > problem code, there is a loop that uses fgets to read a line > from a file. It runs properly until the 2708TH iteration and > then it dumps core with a segmentation fault. > > char string0[256]; > more lines of code . . . > > while ( fgets(string0,sizeof(string0),fp_config)) { > code to be run for each line > } > > It runs fine until the 2,709TH iteration. Instead of > reading the next line, it jumps to the line that closes > fp_config even though it is far from read and exits with the > segmentation fault. > > The man page on fgets says that if errors occur while > running fgets, one must use perr to see whether the error > terminated activity or it was the end of the file. In this case, > it is definitely the error. > > Some observations: > > The crash occurs on the 2,709TH input no matter how long I > declared string0 to be. string0 is over-written each new > iteration so nothing should be accumulating that uses up > resources. > > Maybe I am declaring string0 in the wrong data type. > Originally, it had been 1024 characters long but 2709 seems to > be the C equivalent to the apocalypse and I thought it was > supposed to be next December:-) > > This same code, by the way, also fails at about the same > number of iterations if one uses fgetc and builds the line one > char at a time. > Have you tried allocating a large block of maybe 15-20k (using alloc, or calloc) instead of declaring it on the stack? You can print the length of the line (and iteration count) for each line processed and see what max line length is. What you describe sounds more like a problem with the content of the file overflowing the space allocated and causing a stack fault than something wrong with fgets (especially if fgetc and concatenating the string causes the same problem). Also, what happens if you remove your processing code and replace it with a print of the line length & the iteration count? Compilers have been know to generate buggy loop code sometimes, so that's also a possibility. -- Brad Mettee PC HotShots, Inc. Westminster, MD (410) 848-0588 -> Let us bring out the **Power** of your PCs. <- -> Custom Business Software Solutions since 1991 <- Visit us for information about our company.