473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Undefined side-effect of setvbuf?

I'm curious if setvbuf behavior is known to be undefined with regards
to effects it may have on the file pointer. I've found that the
following code works different on AIX 4.3.3 xlc and FreeBSD gcc 2.95.2
19990314 than it does on SCO 5.0.4 cc or Linux gcc egcs-1.1.2 19991024.
This is a bit curious as here's a gcc example that works one way and
one that works the other, and I would think the relevant code is in
libc/glibc.

This is what is going on. If you fopen a file, read a small amount
of data from it and then fseek(fp,0,SEEK_SET), a subsequent setvbuf
to some buffer value (non-0) causes the filepointer returned by
ftell(fp) to be nonzero in the AIX and FreeBSD cases listed above,
and it is zero in the other cases. If you don't do the fread and
fseek first, the filepointer is zero in all cases.

I couldn't find any mention of this behavior, though the particular
conditions here are probably a little unusual. The reason I'm
reading the file first is to check the type of file (by looking at
a header) and decide whether or not to setvbuf based on that.
There are several easy workarounds, I could close and reopen the
file, or simply fseek(fp,0,SEEK_SET) AFTER the setvbuf. But I was
curious as to whether there is any mention anywhere about setvbuf
effect on the filepointer, as I wasn't able to find any...

--

Keith Doyle
ke****@rexx.com

====================== sample code =========================
/*
run the resultant binary with the name of a small file
(a couple hundred bytes long) as a parameter. This program
will fopen the file, read 16 bytes, fseek to 0, then do a
setvbuf with a buffer size of 16384. ftell is then used to
read the filepointer and report its value if it is non zero.
*/
#include <stdio.h>
#include <errno.h>

unsigned char dat[16];
char buf[16394];

int
main(int argc,char **argv)
{
int n,p;
FILE *fp;

if (argv[1][0])
{
fp = fopen(argv[1],"r");
if (!fp)
{
fprintf(stderr,"Can't open %s\n",argv[1]);
exit(1);
}

fread(dat,1,16,fp); /* remove these and they all act the same*/
fseek(fp,0,SEEK_SET);

errno = 0;
n = setvbuf(fp,buf,_IOFBF,16384);
p = ftell(fp);
if (p)
{
printf("setvbuf changed file position to %d\n",p);
}
if (n < 0)
printf("setvbuf returned %d, errno=%d\n",n,errno);
else
printf("setvbuf returned %d\n",n);
exit(0);
} else {
fprintf(stderr,"Usage: bufbug <filename>\n");
exit(1);
}
}
===================end included code========================
Nov 13 '05 #1
2 3046
Keith Doyle <ke****@rexx.com> wrote:

This is what is going on. If you fopen a file, read a small amount
of data from it and then fseek(fp,0,SEEK_SET), a subsequent setvbuf
to some buffer value (non-0) causes the filepointer returned by
ftell(fp) to be nonzero in the AIX and FreeBSD cases listed above,
and it is zero in the other cases. If you don't do the fread and
fseek first, the filepointer is zero in all cases.


The C Standard says you're not allowed to perform other operations on
the stream before calling setvbuf. You're violating that, so you get
undefined behavior.

-Larry Jones

I suppose if I had two X chromosomes, I'd feel hostile too. -- Calvin
Nov 13 '05 #2
On 22 Aug 2003 18:17:44 -0700, ke****@rexx.com (Keith Doyle) wrote:
This is what is going on. If you fopen a file, read a small amount
of data from it and then fseek(fp,0,SEEK_SET), a subsequent setvbuf


Can't do that. setvbuf must be called after the file is opened, but
before any other operations on the file.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: duckfreezone | last post by:
Hi, I've got a small test program which behaves correctly (IMHO) on all compilers that I have acccess to, with the exception of the gcc on Macintosh OSX "gcc version 4.0.0 (Apple Computer, Inc....
14
by: ozbear | last post by:
Someone was asking in another forum how to translate: if (x && (y1=(y+y1)/2)) /* all integers with defined values */ to another language. I am at odds with myself as to whether this causes...
3
by: Mantorok Redgormor | last post by:
In this context would an indeterminate value lead down the path to undefined behavior? (void)foo->member; I want to keep my interface consisent in my program so I have this one function which...
7
by: Andy Lomax | last post by:
The C99 standard contains various statements like this one (in this case, 6.5.16, assignment operator): >If an attempt is made to modify >the result of an assignment operator or to access it...
1
by: Pavils Jurjans | last post by:
Hello, I am building custom hashtable class, and thinking about value retrieval issues. The thing is, that sometimes the hashtable value may contain value null. If someone is reading this value...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
7
by: dd | last post by:
Hello all. First, I'm a newbie to javascript but not to ASP. Secondly I've been searching for the answer for two hours and I decided to finally post this question. I'm trying to take a...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
12
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the...
6
by: teddysnips | last post by:
In my application I need to allow users to cut 'n' paste stuff from various sources, some of which might include dodgy characters such as "<". Natch, IE interprets these as potentially dangerous...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.