By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

undefined behaviour?

P: n/a
I stumbled upon the following code (stripped to the minimum)
#include <stdio.h>
#include <stdlib.h>

int main(void){

char *file_name = "t1.txt";
char ch;
FILE *pf = fopen(file_name, "r+");
fputs("bla", pf);
ungetc('z', pf);
ch = getc(pf);
printf("ch = %c\n", ch);
fclose(pf);
return 0;
}

Now I'm wondering if this is defined behaviour or not, and if it's
defined should there be a problem with the close at the end?

Thanks in advance
Friedrich

--
Please remove just-for-news- to reply via e-mail.
Dec 15 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
Friedrich Dominicus wrote:
I stumbled upon the following code (stripped to the minimum)
#include <stdio.h>
#include <stdlib.h>

int main(void){

char *file_name = "t1.txt"; An uninitialised pointer is being used here.
char ch;
FILE *pf = fopen(file_name, "r+"); fopen() is not checked for errors.
fputs("bla", pf); Again, fputs() is not checked for failure.
ungetc('z', pf); The first parameter of ungetc() is an int.
Again, no checking for failure.
ch = getc(pf); getc() returns an int, but may also return EOF on error.
printf("ch = %c\n", ch);
fclose(pf); fclose() isn't being checked for error.
return 0;
}

Now I'm wondering if this is defined behaviour or not, and if it's
defined should there be a problem with the close at the end?

AFAICS, there should be no problems with fclose().
The problem in this program is with the use of a uninitialised
pointer at the start.

I tested it here and it crashes. It because of the uninitialised
pointer.
Either use a string literal or allocate space via malloc or use an
array
to hold the file name.

Dec 15 '05 #2

P: n/a
On Thu, 15 Dec 2005 14:19:26 +0100, Friedrich Dominicus wrote:
#include <stdio.h>
#include <stdlib.h>

int main(void){

char *file_name = "t1.txt";
char ch;
FILE *pf = fopen(file_name, "r+");
fputs("bla", pf);
Definitely undefined behaviour if fopen() failed. Other than that, no.
ungetc('z', pf);
One ungetc() is guaranteed to work. So, no problem.
ch = getc(pf);
getc() returns an int. Implementation-defined behaviour might arise if
getc() fails and if a char is signed.

This because getc() returns EOF if it fails, which might not fit in
a char.
If chars are unsigned, the value will be EOF with CHAR_MAX+1 added to it
until the value can be represented in a char.
If chars are signed, and EOF cannot be represented in it, the resulting
value is implementation-defined. Part of the behaviour might be a raised
signal.

However, I don't know if getc() is allowed to fail if you just called
ungetc(). I don't think the standard makes any guarantees about this.
printf("ch = %c\n", ch);
printf() expects an unsigned char converted to an int. The conversion to
int is done automatically thanks to the default integer promotion rules,
but char is not guaranteed to be unsigned.

I believe this is undefined behaviour.
fclose(pf);
No problem with this.
return 0;
}


--
Pieter Droogendijk <pieter at binky dot org dot uk>
PGP/1E92DBBC [ Make way for the Emperor's Finest. ] binky.org.uk

Dec 15 '05 #3

P: n/a
On 2005-12-15, Friedrich Dominicus <ju*****************@q-software-solutions.de> wrote:
I stumbled upon the following code (stripped to the minimum)
#include <stdio.h>
#include <stdlib.h>

int main(void){
char *file_name = "t1.txt";
char ch;
FILE *pf = fopen(file_name, "r+");
Whether it is possible to open a text file for updating is
implementation-defined. that said, this could have been "r+b" without
changing the essential meaning of your question.
fputs("bla", pf);
ungetc('z', pf);
ch = getc(pf);
printf("ch = %c\n", ch);
fclose(pf);
return 0;
}

Now I'm wondering if this is defined behaviour or not, and if it's
defined should there be a problem with the close at the end?


I can't think of a single line in there that invokes undefined
behavior... however, it's not clear to me whether the contents of the
file are well-defined afterwards.
Dec 15 '05 #4

P: n/a
On 2005-12-15, Pieter Droogendijk <pi******@binkySPAM.orgFOR.ukME> wrote:
On Thu, 15 Dec 2005 14:19:26 +0100, Friedrich Dominicus wrote:
#include <stdio.h>
#include <stdlib.h>

int main(void){

char *file_name = "t1.txt";
char ch;
FILE *pf = fopen(file_name, "r+");
fputs("bla", pf);
Definitely undefined behaviour if fopen() failed. Other than that, no.
ungetc('z', pf);
ch = getc(pf);
printf("ch = %c\n", ch);


printf() expects an unsigned char converted to an int. The conversion to
int is done automatically thanks to the default integer promotion rules,
but char is not guaranteed to be unsigned.


'z' is guaranteed to be positive, as are all members of the basic
execution character set. In any case, it's not the issue he was looking
for an answer on.
I believe this is undefined behaviour.


You believe incorrectly.
fclose(pf);
return 0;
}


so what about the contents of the file afterwards? I suspect this is
what he was wondering about.
Dec 15 '05 #5

P: n/a
On 2005-12-15, santosh <sa*********@gmail.com> wrote:
char *file_name = "t1.txt";

An uninitialised pointer is being used here.


The line you are responding to is a declaration, and, moreover, one with
an initializer.

You claim that on your implementation the program crashes on that line.
Either you're lying, or you have a very crappy [and non-conforming]
implementation.
Dec 15 '05 #6

P: n/a
Friedrich Dominicus wrote:
I stumbled upon the following code (stripped to the minimum)
#include <stdio.h>
#include <stdlib.h>

int main(void){

char *file_name = "t1.txt";
char ch;
FILE *pf = fopen(file_name, "r+");
Undefined behavior for all following operations that use pf if fopen
returns NULL.
fputs("bla", pf);
ungetc('z', pf);
Undefined behavior for not calling fflush on a stream after performing
an output operation and before performing an input operation.
ch = getc(pf);
getc returns int.
printf("ch = %c\n", ch);
fclose(pf);
return 0;
}

Now I'm wondering if this is defined behaviour or not, and if it's
defined should there be a problem with the close at the end?


Check the return value of fopen and fflush(pf) after the call to fputs
and you should be alright.

Robert Gamble

Dec 15 '05 #7

P: n/a
santosh wrote:
Friedrich Dominicus wrote:
I stumbled upon the following code (stripped to the minimum)
#include <stdio.h>
#include <stdlib.h>

int main(void){

char *file_name = "t1.txt"; An uninitialised pointer is being used here.


<snip part of the answer>
I tested it here and it crashes. It because of the uninitialised
pointer.
Either use a string literal or allocate space via malloc or use an
array
to hold the file name.


That line declares a pointer to char and initializes it to point to a
string literal. How does this constitute a use of an uninitialized
pointer?

Dec 15 '05 #8

P: n/a
Friedrich Dominicus wrote:
I stumbled upon the following code (stripped to the minimum)


Well, the following modified version of the code you posted works
here for me.
The output is:
ch = z

<snip>
#include <stdio.h>
#include <stdlib.h>

int main(void){

int ch;
FILE *pf;
if((pf = fopen("t1.txt", "w+"))==NULL)
{
puts("fopen() returned NULL...");
exit(EXIT_FAILURE);
}
if((fputs("bla", pf))==EOF)
{
puts("fputs() returned EOF...");
exit(EXIT_FAILURE);
}
ch = 'z';
fflush(pf);
if((ungetc(ch, pf))==EOF)
{
puts("ungetc() returned EOF...");
exit(EXIT_FAILURE);
}

ch = getc(pf);
if(ch==EOF)
{
puts("getc() returned EOF...");
exit(EXIT_FAILURE);
}

printf("ch = %c\n", ch);
if((fclose(pf))==-1)
{
puts("fclose() returned -1...");
exit(EXIT_FAILURE);
}
return 0;

}

Dec 15 '05 #9

P: n/a
Friedrich Dominicus <ju*****************@q-software-solutions.de> wrote:
I stumbled upon the following code (stripped to the minimum)
#include <stdio.h>
#include <stdlib.h>

int main(void){

char *file_name = "t1.txt";
char ch;
FILE *pf = fopen(file_name, "r+");
fputs("bla", pf);
ungetc('z', pf);


Yes, undefined behaviour. You just wrote (using fputs()) to an update
stream, causing it to be set to output mode. ungetc() expects an input
stream. You cannot use ungetc() on an output-oriented update stream
without first using fflush() or a file positioning function.

This raises the question of what happens when you fflush() between
fputs() and ungetc(). AFAICT, that should work, not cause UB, cause the
'z' to be read by the following getc(), and as the Standard says about
ungetc(): "The external storage corresponding to the stream is
unchanged".

Richard
Dec 15 '05 #10

P: n/a
santosh wrote:
Friedrich Dominicus wrote:
I stumbled upon the following code (stripped to the minimum)
#include <stdio.h>
#include <stdlib.h>

int main(void){

char *file_name = "t1.txt";

An uninitialised pointer is being used here.


What is your definition of initialized?

Robert Gamble

Dec 15 '05 #11

P: n/a
Jordan Abel wrote:
On 2005-12-15, santosh <sa*********@gmail.com> wrote:
char *file_name = "t1.txt";

An uninitialised pointer is being used here.


The line you are responding to is a declaration, and, moreover, one with
an initializer.

You claim that on your implementation the program crashes on that line.
Either you're lying, or you have a very crappy [and non-conforming]
implementation.


Your right. I'm sorry. It was an incredible oversight on my part.

That being said, the code as was posted did crash over here.
The compiler is gcc (mingw).
After I introduced error checking for each function call I found that
the
first error was with foen().
I modified "r+" to "w+" which succeeded, but then ungetc() returned
EOF.
I corrected that by inserting a 'fflush(pf)' before calling ungetc().
Then it was getc()'s turn to return EOF.
After I changed 'ch' from char to int the program finally executed as
it
should. The contents of the file after running the code is:
bla [Hex dump = 626c 610d 0a]

Dec 15 '05 #12

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Friedrich Dominicus <ju*****************@q-software-solutions.de> wrote:
I stumbled upon the following code (stripped to the minimum)
#include <stdio.h>
#include <stdlib.h>

int main(void){

char *file_name = "t1.txt";
char ch;
FILE *pf = fopen(file_name, "r+");
fputs("bla", pf);
ungetc('z', pf);
Yes, undefined behaviour. You just wrote (using fputs()) to an update
stream, causing it to be set to output mode. ungetc() expects an input
stream.

Well that's what the 'r' is for or not? So for me it's an input stream
in first line and writable also.
You cannot use ungetc() on an output-oriented update stream
without first using fflush() or a file positioning function.

As posted before I can understand this argument, and as said before I
have found it out there, but was puzzling about its validity.

Regards
Friedrich
--
Please remove just-for-news- to reply via e-mail.
Dec 15 '05 #13

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:

This raises the question of what happens when you fflush() between
fputs() and ungetc(). AFAICT, that should work, not cause UB, cause the
'z' to be read by the following getc(), and as the Standard says about
ungetc(): "The external storage corresponding to the stream is
unchanged".

So I'd argue it's defined if there is an fflush between the fputs and
ungetc, and undefined without it. That's sounds reasonable

Regards
Friedrich
--
Please remove just-for-news- to reply via e-mail.
Dec 15 '05 #14

P: n/a

Friedrich Dominicus wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Friedrich Dominicus <ju*****************@q-software-solutions.de> wrote:
I stumbled upon the following code (stripped to the minimum)
#include <stdio.h>
#include <stdlib.h>

int main(void){

char *file_name = "t1.txt";
char ch;
FILE *pf = fopen(file_name, "r+");
fputs("bla", pf);
ungetc('z', pf);


Yes, undefined behaviour. You just wrote (using fputs()) to an update
stream, causing it to be set to output mode. ungetc() expects an input
stream.

Well that's what the 'r' is for or not? So for me it's an input stream
in first line and writable also.
You cannot use ungetc() on an output-oriented update stream
without first using fflush() or a file positioning function.

As posted before I can understand this argument, and as said before I
have found it out there, but was puzzling about its validity.


Here is the appropriate section of the Standard (n1124 to be exact):

7.19.5.3p6

When a file is opened with update mode ('+' as the second or third
character in the
above list of mode argument values), both input and output may be
performed on the
associated stream. However, output shall not be directly followed by
input without an
intervening call to the fflush function or to a file positioning
function (fseek,
fsetpos, or rewind), and input shall not be directly followed by output
without an
intervening call to a file positioning function, unless the input
operation encounters endof-
file. Opening (or creating) a text file with update mode may instead
open (or create) a
binary stream in some implementations.

I hope that relieves any doubt you had about the validity of the
statements made to that effect here.

Robert Gamble

Dec 15 '05 #15

P: n/a
"santosh" <sa*********@gmail.com> writes:
Jordan Abel wrote:
On 2005-12-15, santosh <sa*********@gmail.com> wrote:
>> char *file_name = "t1.txt";
> An uninitialised pointer is being used here.
The line you are responding to is a declaration, and, moreover, one with
an initializer.

You claim that on your implementation the program crashes on that line.
Either you're lying, or you have a very crappy [and non-conforming]
implementation.


Your right. I'm sorry. It was an incredible oversight on my part.

That being said, the code as was posted did crash over here.
The compiler is gcc (mingw).
After I introduced error checking for each function call I found that
the
first error was with foen().

Well that does not mean that the code as given was invalid
I modified "r+" to "w+" which succeeded, but then ungetc() returned
EOF. That's interesting and I think that should not happen, even if you use
"r+". I corrected that by inserting a 'fflush(pf)' before calling
ungetc(). I'd argue this is the real problem.

The system as used her has worked fine but baild out on the fclose at
the end. After insering the fflush here this was gone and the result
were as expected.
After I changed 'ch' from char to int the program finally executed as
it I don't think this is really a problem. should. The contents of the file after running the code is:

Can't tell I did not care about the content just the fputs + ungetc
stuff. However thanks for taking the time checking all this "stuff"

Regards
Friedrich

--
Please remove just-for-news- to reply via e-mail.
Dec 15 '05 #16

P: n/a
"Robert Gamble" <rg*******@gmail.com> writes:
Here is the appropriate section of the Standard (n1124 to be exact):

7.19.5.3p6

When a file is opened with update mode ('+' as the second or third
character in the
above list of mode argument values), both input and output may be
performed on the
associated stream. However, output shall not be directly followed by
input without an
intervening call to the fflush function or to a file positioning
function (fseek,
fsetpos, or rewind), and input shall not be directly followed by output
without an
intervening call to a file positioning function, unless the input
operation encounters endof-
file. Opening (or creating) a text file with update mode may instead
open (or create) a
binary stream in some implementations.

I hope that relieves any doubt you had about the validity of the
statements made to that effect here.


Well that is the "definitive" answer for me. I have not seen
it. Thanks for pointing it out to me.

Regards
Friedrich
--
Please remove just-for-news- to reply via e-mail.
Dec 15 '05 #17

P: n/a
santosh wrote:
Friedrich Dominicus wrote:

ungetc('z', pf);

The first parameter of ungetc() is an int.


and 'z' *is* an int
--
Nick Keighley

Dec 15 '05 #18

P: n/a
"Nick Keighley" <ni******************@hotmail.com> writes:
santosh wrote:
Friedrich Dominicus wrote:

> ungetc('z', pf);

The first parameter of ungetc() is an int.


and 'z' *is* an int


And even if it weren't, it would be implicitly converted to int (since
there's a "#include <stdio.h>").

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 15 '05 #19

P: n/a
On Thu, 15 Dec 2005 16:14:21 +0100, in comp.lang.c , Friedrich
Dominicus <ju*****************@q-software-solutions.de> wrote:
You cannot use ungetc() on an output-oriented update stream
without first using fflush() or a file positioning function.
As posted before I can understand this argument, and as said before I
have found it out there, but was puzzling about its validity.


Its in the Standard.

7.19.5.3(6) When a file is opened with update mode ... output shall
not be directly followed by input without an intervening call to the
fflush function or to a file positioning function..., and input shall
not be directly followed by output without an intervening call to a
file positioning function...

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 15 '05 #20

P: n/a

Though the uninitialised pointer leeds to error,

The code
char *file_name = "t1.txt";
is legal

Always try to check the file opening cause

the code
FILE *pf = fopen(file_name, "r+");

"r+" -- to open an existing text file for reading and writing

if the file does not exists, it will give a null pointer.

If the file does not exists and u try to execute do fputs with null
pointer will cause undefined behaviour

Best Regards,
Abdur

Dec 16 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.