473,791 Members | 3,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,e rrno);
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 3069
Keith Doyle <ke****@rexx.co m> 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
1515
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. build 5026)" Looks to be that the order of evaluation of the assignment on OSX is different to other systems. I *think* it is wrong on OSX, or am I simply relying on undefined behaviour?
14
1752
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 undefined behaviour in the first place. Does the subexpression (y1=(y+y1)/2) cause UB because /y/ is both modified and used without an intervening sequence point attached to the larger expression or is it ok because the value of the assignment is...
3
1996
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 is passed an argument that it doesn't need. So I can only think of using it this way as a viable method
7
1971
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 after the next sequence point, the >behavior is undefined. What does this actually mean? Can anyone give me a code example that leads to undefined behaviour?
1
3115
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 like this value = myHashtable; then, is the value of "myKey" key is null, then the result is the same, as
49
14530
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 http://www.webreference.com/programming/javascript/gr/column9/ they say: <snip> The undefined property A relatively recent addition to JavaScript is the undefined property.
7
16275
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 javascript and make it work with asp. Any thought, ideas or pointers in the right direction would be greatly appreciated. I am getting the following error: Microsoft JScript runtime error '800a1391'
45
4863
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 "new Array() vs " question or to the array performance, I dared to move it to a new thread. Gecko takes undefined value strictly as per Book 4, Chapter 3, Song 9 of Books of ECMA :-)
12
3070
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 difference. For example: Consider the following code: a = i; We say that the above expression statement produces undefined
6
2334
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 and provides a mechanism to encode/decode them. However I can't seem to get it to work: .... <asp:textbox id=txtDescription style="Z-INDEX: 102; LEFT: 111px; POSITION: absolute; TOP: 124px" tabIndex=29 runat="server" onblur="return...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.