LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-15-2009, 03:52 PM   #16
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled

ref wje_lq edit:
Yes anybody expecting anything other than what is outputted is in the land of UB. If someone was to say it works then the output is what they _think_ it should be, therefore relying on UB.

Last edited by dmail; 08-15-2009 at 03:53 PM.
 
Old 08-17-2009, 12:25 AM   #17
ebd
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Original Poster
Rep: Reputation: 0
conclusion:
errno is unreliable ater a successfull system or library function call, because errno may well be changed too. so, only check the errno after a failed function call for which it is explicitly stated to be set to indicate an error.
right?
 
Old 08-17-2009, 12:53 AM   #18
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Correct.

In other words:
Code:
  int ierr = 0;
  if (open ("myfile", MYFLAGS) < 0)
  {
    ierr = errno; // Copy "errno" for subsequent error handling...
This is good advice, regardless if we're talking about Linux (e.g. "errno.h"), Windows (GetLastError() is admittedly much better - but still suffers more or less the same problem "the next global 'error' will overwrite the current one"...) or even stuff like h_errno (DNS calls, etc).

IMHO .. PSM

PS:
Deliberately performing "Undefined Behavior" (like calling "perror()" multiple times, without any bona fide error for perror to inspect) will, by definition, cause "undefined results".

So even though "save your results variable as quickly as you can" is good advice, I wouldn't necessarily agree that this "experiment" taught you anything useful about how either "error" or "perror()" are actually supposed to work...

Last edited by paulsm4; 08-17-2009 at 12:57 AM.
 
Old 08-17-2009, 04:17 AM   #19
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Regardless of whether it complies or not with standards (and indeed we are in UB land), the fact "Illegal seek" is returned means that error happened during the perror call and is still to be explained.
 
Old 08-17-2009, 05:42 AM   #20
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by jlliagre View Post
... the fact "Illegal seek" is returned means that error happened during the perror call and is still to be explained.
I thought it was already explained?
The facts are that calling perror when there has not been an error is an error in itself. The perror function does not define if it does not change the value of errno so it is unspecified and can change the value to anything it wants, which on this distro and this occasion chose "Illegal seek".
 
Old 08-17-2009, 05:44 AM   #21
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by jlliagre View Post
the fact "Illegal seek" is returned means that error happened during the perror call
Yes. But "error" does not mean "problem".
 
Old 08-17-2009, 05:54 AM   #22
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
I'm afraid this isn't an explanation but just dodging the question.
I have no doubt this illegal seek error, even harmless, not breaking any standard and not a problem per se, is real. I'm just still curious to know why and where it happened. That was the OP question and it is still unanswered.
 
Old 08-17-2009, 06:07 AM   #23
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
This is like banging my head on a brick wall, it has been explained many times now and I will not be posting again in this thread!

Quote:
Originally Posted by jlliagre View Post
I'm afraid this isn't an explanation but just dodging the question.
I have no doubt this illegal seek error, even harmless, not breaking any standard and not a problem per se, is real.
No it is not real.
Quote:
I'm just still curious to know why and where it happened. That was the OP question and it is still unanswered.
No it is not.
Quote:
The setting of errno after a successful call to a function is unspecified unless the description of that function specifies that errno shall not be modified.
 
Old 08-17-2009, 06:29 AM   #24
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
"undefined" and "unexplained" are different status. Programs behaviors, whether they are specified or not should always be explainable.

I'm curious to understand why you claim the "Illegal seek" didn't happen for real. How would you demonstrate it ?
 
Old 08-17-2009, 11:52 PM   #25
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by jlliagre View Post
I'm just still curious to know why and where it happened.
Fair enough.

I'm guessing that the seek is real.

perror() gets this setting of errno from its call to fdopen().

fdopen(), in turn, is a complicated chunk of code which is carefully designed to be standards-compliant without breaking any ancient C code. You'll find some seeking done in there. My guess is that under certain conditions, it gets an illegal seek, determines that the error indication is not a problem, and keeps on truckin'.

The one thing that neither perror() nor fdopen() is allowed to do is to set errno to zero. Saving and restoring an old errno would violate this, because that might mean restoring the value zero.

If you wish to explore the source code for fdopen(), be my guest. :)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
error: unpacking of archive failed on file /usr/bin/perror;4a3b6207: cpio: read faile mehdi1973 Linux - General 4 06-22-2009 05:47 AM
perror breaks cursor control and placement Gecopa Linux - General 2 06-19-2009 03:17 PM
Why the message of perror() is "success"? henry0712 Programming 3 12-23-2006 10:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration