Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
signal() anomaly
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 28 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
hip cat  
View profile  
 More options Jul 31 2012, 4:13 pm
Newsgroups: comp.lang.c
From: hip cat <nos...@nospam.com>
Date: Tue, 31 Jul 2012 20:13:34 +0000 (UTC)
Local: Tues, Jul 31 2012 4:13 pm
Subject: signal() anomaly
What up

I've used signal to set SIGSEGV to SIG_IGN. But when I later dereference
a null pointer my program still crashes out with segfault SIGSEGV. What
gives?

Communicate laterz ++


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Sosman  
View profile  
 More options Jul 31 2012, 4:35 pm
Newsgroups: comp.lang.c
From: Eric Sosman <esos...@ieee-dot-org.invalid>
Date: Tue, 31 Jul 2012 16:35:41 -0400
Local: Tues, Jul 31 2012 4:35 pm
Subject: Re: signal() anomaly
On 7/31/2012 4:13 PM, hip cat wrote:

> What up

> I've used signal to set SIGSEGV to SIG_IGN. But when I later dereference
> a null pointer my program still crashes out with segfault SIGSEGV. What
> gives?

     Did you check that the signal() call succeeded?  That is,
was the returned value SIG_ERR or something else?

     On POSIX systems, ignoring SIGSEGV produces undefined behavior.
The C Standard isn't quite so clear to me: It's U.B. if a signal
handler for SIGSEGV returns (7.14.1.1p3), but SIG_IGN is not a
"signal handler" under the definition of 7.14.1.1p2.

--
Eric Sosman
esos...@ieee-dot-org.invalid


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Edward A. Falk  
View profile  
 More options Jul 31 2012, 6:12 pm
Newsgroups: comp.lang.c
From: f...@rahul.net (Edward A. Falk)
Date: Tue, 31 Jul 2012 22:12:41 +0000 (UTC)
Local: Tues, Jul 31 2012 6:12 pm
Subject: Re: signal() anomaly
In article <jv9e9e$bj...@speranza.aioe.org>,
hip cat  <nos...@nospam.com> wrote:

>What up

>I've used signal to set SIGSEGV to SIG_IGN. But when I later dereference
>a null pointer my program still crashes out with segfault SIGSEGV. What
>gives?

I am amused and curious -- what did you *want* to happen on a null
pointer dereference?

--
        -Ed Falk, f...@despams.r.us.com
        http://thespamdiaries.blogspot.com/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nobody  
View profile  
 More options Aug 1 2012, 1:06 pm
Newsgroups: comp.lang.c
From: Nobody <nob...@nowhere.com>
Date: Wed, 01 Aug 2012 18:06:25 +0100
Local: Wed, Aug 1 2012 1:06 pm
Subject: Re: signal() anomaly

On Tue, 31 Jul 2012 20:13:34 +0000, hip cat wrote:
> I've used signal to set SIGSEGV to SIG_IGN. But when I later dereference a
> null pointer my program still crashes out with segfault SIGSEGV. What
> gives?

POSIX-2008 §2.4.3 says:

        SIG_IGN

        Ignore signal.

        Delivery of the signal shall have no effect on the process. The behavior
        of a process is undefined after it ignores a SIGFPE, SIGILL, SIGSEGV, or
        SIGBUS signal that was not generated by kill(), sigqueue(), or raise().

http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html

And also:

        The behavior of a process is undefined after it returns normally from a
        signal-catching function for a SIGBUS, SIGFPE, SIGILL, or SIGSEGV signal
        that was not generated by kill(), sigqueue(), or raise().


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hip cat  
View profile  
 More options Aug 1 2012, 2:58 pm
Newsgroups: comp.lang.c
From: hip cat <nos...@nospam.com>
Date: Wed, 1 Aug 2012 18:58:08 +0000 (UTC)
Local: Wed, Aug 1 2012 2:58 pm
Subject: Re: signal() anomaly

On Tue, 31 Jul 2012 22:12:41 +0000, Edward A. Falk wrote:
> In article <jv9e9e$bj...@speranza.aioe.org>, hip cat
> <nos...@nospam.com> wrote:
>>What up

>>I've used signal to set SIGSEGV to SIG_IGN. But when I later dereference
>>a null pointer my program still crashes out with segfault SIGSEGV. What
>>gives?

> I am amused and curious -- what did you *want* to happen on a null
> pointer dereference?

Word Ed,

My code has a certain pointer that sometimes unexpectedly becomes null.
It would be a lot of work to find every place the pointer gets
dereferenced and add a null check. So I want to just ignore it by
catching the signal.

Communicate laterz ++


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hip cat  
View profile  
 More options Aug 1 2012, 2:59 pm
Newsgroups: comp.lang.c
From: hip cat <nos...@nospam.com>
Date: Wed, 1 Aug 2012 18:59:01 +0000 (UTC)
Local: Wed, Aug 1 2012 2:59 pm
Subject: Re: signal() anomaly

On Tue, 31 Jul 2012 16:35:41 -0400, Eric Sosman wrote:
> On 7/31/2012 4:13 PM, hip cat wrote:
>> What up

>> I've used signal to set SIGSEGV to SIG_IGN. But when I later
>> dereference a null pointer my program still crashes out with segfault
>> SIGSEGV. What gives?

>      Did you check that the signal() call succeeded?  That is,
> was the returned value SIG_ERR or something else?

>      On POSIX systems, ignoring SIGSEGV produces undefined behavior.
> The C Standard isn't quite so clear to me: It's U.B. if a signal handler
> for SIGSEGV returns (7.14.1.1p3), but SIG_IGN is not a "signal handler"
> under the definition of 7.14.1.1p2.

Word Eric,

The return value is 0.

Communicate laterz ++


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kuyper  
View profile  
 More options Aug 1 2012, 3:33 pm
Newsgroups: comp.lang.c
From: James Kuyper <jameskuy...@verizon.net>
Date: Wed, 01 Aug 2012 15:33:20 -0400
Local: Wed, Aug 1 2012 3:33 pm
Subject: Re: signal() anomaly
On 08/01/2012 02:58 PM, hip cat wrote:

> On Tue, 31 Jul 2012 22:12:41 +0000, Edward A. Falk wrote:
>> In article <jv9e9e$bj...@speranza.aioe.org>, hip cat
>> <nos...@nospam.com> wrote:
>>> What up

>>> I've used signal to set SIGSEGV to SIG_IGN. But when I later dereference
>>> a null pointer my program still crashes out with segfault SIGSEGV. What
>>> gives?

>> I am amused and curious -- what did you *want* to happen on a null
>> pointer dereference?

> Word Ed,

> My code has a certain pointer that sometimes unexpectedly becomes null.

Rather than trying to make this work, you should be trying to find out
why the pointer is becoming null.

> It would be a lot of work to find every place the pointer gets
> dereferenced and add a null check. So I want to just ignore it by
> catching the signal.

Well, as "Nobody" has already pointed out, that's not an option. Using
SIG_IGN for this has undefined behavior. So does registering your own
SIGSEGV "handler" that doesn't bother to actually do anything, and
simply returns. Therefore, you're going to have to either insert the
null checks, or let the program abort() - your choice.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Angel  
View profile  
 More options Aug 1 2012, 3:41 pm
Newsgroups: comp.lang.c
From: Angel <angel+n...@spamcop.net>
Date: 01 Aug 2012 19:41:44 GMT
Local: Wed, Aug 1 2012 3:41 pm
Subject: Re: signal() anomaly
On 2012-08-01, hip cat <nos...@nospam.com> wrote:

> My code has a certain pointer that sometimes unexpectedly becomes null.
> It would be a lot of work to find every place the pointer gets
> dereferenced and add a null check. So I want to just ignore it by
> catching the signal.

And you expect the program will work correctly and produce meaningful
output if you ignore what is normally a very fatal error condition?

I do hope you're not writing code that will be used in any important
production environment.

--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Bowler  
View profile  
 More options Aug 1 2012, 4:14 pm
Newsgroups: comp.lang.c
From: Nick Bowler <nbow...@draconx.ca>
Date: Wed, 1 Aug 2012 20:14:25 +0000 (UTC)
Local: Wed, Aug 1 2012 4:14 pm
Subject: Re: signal() anomaly

On Tue, 31 Jul 2012 16:35:41 -0400, Eric Sosman wrote:
> On 7/31/2012 4:13 PM, hip cat wrote:
>> What up

>> I've used signal to set SIGSEGV to SIG_IGN. But when I later dereference
>> a null pointer my program still crashes out with segfault SIGSEGV. What
>> gives?

>      Did you check that the signal() call succeeded?  That is,
> was the returned value SIG_ERR or something else?

>      On POSIX systems, ignoring SIGSEGV produces undefined behavior.

POSIX is a bit more specific than that.  But regardless...

> The C Standard isn't quite so clear to me: It's U.B. if a signal
> handler for SIGSEGV returns (7.14.1.1p3), but SIG_IGN is not a
> "signal handler" under the definition of 7.14.1.1p2.

...the C standard doesn't need to be any more explicit on this matter.
The behaviour is undefined because the program has dereferenced an
invalid (null) pointer.  The handling of SIGSEGV is irrelevant.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenneth Brody  
View profile  
 More options Aug 1 2012, 4:16 pm
Newsgroups: comp.lang.c
From: Kenneth Brody <kenbr...@spamcop.net>
Date: Wed, 01 Aug 2012 16:16:25 -0400
Local: Wed, Aug 1 2012 4:16 pm
Subject: Re: signal() anomaly
On 8/1/2012 2:58 PM, hip cat wrote:

> On Tue, 31 Jul 2012 22:12:41 +0000, Edward A. Falk wrote:
>> In article <jv9e9e$bj...@speranza.aioe.org>, hip cat
>> <nos...@nospam.com> wrote:
>>> What up

I wasn't aware that we had a choice of "up".  I think I'll take that "up"
over there...

>>> I've used signal to set SIGSEGV to SIG_IGN. But when I later dereference
>>> a null pointer my program still crashes out with segfault SIGSEGV. What
>>> gives?

>> I am amused and curious -- what did you *want* to happen on a null
>> pointer dereference?

> Word Ed,

Yes, "Ed" is a word.  More specifically, a proper noun.

> My code has a certain pointer that sometimes unexpectedly becomes null.

Then use a debugger to trap when the pointer "unexpectedly becomes
null(sic)".  If the pointer changes without you changing it, find that bug
and fix it.  Don't simply say "that's okay, I'll just ignore NULL pointers
and pretend they don't exist.

> It would be a lot of work to find every place the pointer gets
> dereferenced and add a null check.

Why not put a check at the top, and handle it, rather than pretending
everything is okay?

> So I want to just ignore it by
> catching the signal.

So, what should this do?

     char *pt = NULL;
     char c = *pt;

What value should be in c?

Besides, you're not "catching" the signal.  Instead, you're telling the
system to ignore that signal, which as others have pointed out, is UB for
SIGSEGV.

--
Kenneth Brody


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Sosman  
View profile  
 More options Aug 1 2012, 4:33 pm
Newsgroups: comp.lang.c
From: Eric Sosman <esos...@ieee-dot-org.invalid>
Date: Wed, 01 Aug 2012 16:33:37 -0400
Local: Wed, Aug 1 2012 4:33 pm
Subject: Re: signal() anomaly
On 8/1/2012 2:58 PM, hip cat wrote:

     That's not going to work.  As I wrote earlier, the C Standard
doesn't seem entirely clear on this matter, but the fact that you
get undefined behavior if a SIGSEGV handler returns suggests that
there's no way to recover.  That's explicit under POSIX: If you
ignore SIGSEGV, there's no telling what might happen.

     But, back to your problem: What do you *expect* should happen
if you could somehow continue after dereferencing your null pointer?
Somewhere, your program did `*ptr = 42' and ptr was null: Where do
expect the 42 to go?  Or somewhere you did `x = *ptr' and ptr was
null: What value should x now have?  Or, here's a good one:

        for (ptr = accidentally_null;  *ptr != 0;  ++ptr) {
            putchar(*ptr);
        }

How many times should the loop execute, what output should it
produce, and what value should ptr have when (if) it finishes?

     SIGSEGV means your car's motor has thrown a rod.  You can't
just ignore the broken engine and keep on driving.

--
Eric Sosman
esos...@ieee-dot-org.invalid


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hip cat  
View profile  
 More options Aug 1 2012, 4:47 pm
Newsgroups: comp.lang.c
From: hip cat <nos...@nospam.com>
Date: Wed, 1 Aug 2012 20:47:17 +0000 (UTC)
Local: Wed, Aug 1 2012 4:47 pm
Subject: Re: signal() anomaly

Word Jim,

I think Eric was right on the money asking about the return value. If the
signal() call returns success, but the "ignore" is ignored, I say that's
a bug in the C library. I think I'll file a bug report against glibc.

Communicate laterz ++


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul  
View profile  
 More options Aug 1 2012, 5:34 pm
Newsgroups: comp.lang.c
From: Paul <nos...@needed.com>
Date: Wed, 01 Aug 2012 17:34:11 -0400
Local: Wed, Aug 1 2012 5:34 pm
Subject: Re: signal() anomaly

hip cat wrote:

> Word Jim,

> I think Eric was right on the money asking about the return value. If the
> signal() call returns success, but the "ignore" is ignored, I say that's
> a bug in the C library. I think I'll file a bug report against glibc.

> Communicate laterz ++

For your amusement.

http://efreedom.com/Question/1-2663456/Write-Signal-Handler-Catch-SIG...

You can step around that busted line of code. But
you'd have to be Yoda to succeed.

    Paul


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Sosman  
View profile  
 More options Aug 1 2012, 6:07 pm
Newsgroups: comp.lang.c
From: Eric Sosman <esos...@ieee-dot-org.invalid>
Date: Wed, 01 Aug 2012 18:07:14 -0400
Local: Wed, Aug 1 2012 6:07 pm
Subject: Re: signal() anomaly
On 8/1/2012 4:47 PM, hip cat wrote:

>[...]
> I think Eric was right on the money asking about the return value. If the
> signal() call returns success, but the "ignore" is ignored, I say that's
> a bug in the C library. I think I'll file a bug report against glibc.

     If signal(SIGSEGV, SIG_IGN) does nothing and indicates success,
you may have a case.  But if it can't in fact do anything, the fix
will be to have it return SIG_ERR and tell you so.  That is, you're
still not going to get the signal ignored!  (Nor should you, as has
been pointed out elsethread.)

     You mention glibc, which means you're probably on a POSIX-like
system.  If so, the program's behavior is undefined if SIGSEGV
occurs while it's being ignored -- and among the possible undefined
behaviors is "SIGSEGV crashes the program anyhow."  So from a POSIX
perspective there's no bug: It might be a nice enhancement to have
signal() return SIG_ERR, but returning 0 doesn't reach "bug" level.

     What remains unclear (to me) is how the C Standard rules on this
issue.  We know the behavior is undefined if SIGSEGV goes to a handler
and the handler returns, but SIG_IGN isn't a "handler."  So it's
possible the C Standard and POSIX disagree here -- but it would not
surprise me one little bit to learn that I've overlooked something.

--
Eric Sosman
esos...@ieee-dot-org.invalid


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kuyper  
View profile  
 More options Aug 1 2012, 6:13 pm
Newsgroups: comp.lang.c
From: James Kuyper <jameskuy...@verizon.net>
Date: Wed, 01 Aug 2012 18:13:52 -0400
Local: Wed, Aug 1 2012 6:13 pm
Subject: Re: signal() anomaly
On 08/01/2012 04:47 PM, hip cat wrote:

> On Wed, 01 Aug 2012 15:33:20 -0400, James Kuyper wrote:
...
>> Well, as "Nobody" has already pointed out, that's not an option. Using
>> SIG_IGN for this has undefined behavior. So does registering your own
>> SIGSEGV "handler" that doesn't bother to actually do anything, and
>> simply returns. Therefore, you're going to have to either insert the
>> null checks, or let the program abort() - your choice.

> Word Jim,

> I think Eric was right on the money asking about the return value. If the
> signal() call returns success, but the "ignore" is ignored, I say that's
> a bug in the C library. I think I'll file a bug report against glibc.

The C standard specifies that dereferencing a null pointer  has
undefined behavior. One of the possibilities allowed by that fact is
that a SIGSEGV signal is raised, but the standard does not require that
it be raised. Whether or not that signal is raised, and regardless of
whether the SIGSEGV handler has been set to SIG_IGN, SIG_DFL, or an
actual signal handler, the behavior of your program remains undefined by
the C standard. It doesn't matter what the signal handler actually does,
the behavior of your program became undefined as soon as it dereferenced
a null pointer.

The POSIX standard does specify that a SIGSEGV signal be raised, but
specifies that the behavior of your program is undefined if that signal
is ignored, or if the signal handler returns.

"undefined behavior" means that anything is permitted, including
aborting your program. Your program aborted, which is one of the
permitted behaviors. On what grounds are you going to report a bug? What
requirement of any standard is violated by the fact that your program
aborted?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kuyper  
View profile  
 More options Aug 1 2012, 6:14 pm
Newsgroups: comp.lang.c
From: James Kuyper <jameskuy...@verizon.net>
Date: Wed, 01 Aug 2012 18:14:21 -0400
Local: Wed, Aug 1 2012 6:14 pm
Subject: Re: signal() anomaly
On 08/01/2012 04:47 PM, hip cat wrote:

> On Wed, 01 Aug 2012 15:33:20 -0400, James Kuyper wrote:
...
>> Well, as "Nobody" has already pointed out, that's not an option. Using
>> SIG_IGN for this has undefined behavior. So does registering your own
>> SIGSEGV "handler" that doesn't bother to actually do anything, and
>> simply returns. Therefore, you're going to have to either insert the
>> null checks, or let the program abort() - your choice.

> Word Jim,

> I think Eric was right on the money asking about the return value. If the
> signal() call returns success, but the "ignore" is ignored, I say that's
> a bug in the C library. I think I'll file a bug report against glibc.

The C standard specifies that dereferencing a null pointer  has
undefined behavior. One of the possibilities allowed by that fact is
that a SIGSEGV signal is raised, but the standard does not require that
it be raised. Whether or not that signal is raised, and regardless of
whether the SIGSEGV handler has been set to SIG_IGN, SIG_DFL, or an
actual signal handler, the behavior of your program remains undefined by
the C standard. It doesn't matter what the signal handler actually does,
the behavior of your program became undefined as soon as it dereferenced
a null pointer.

The POSIX standard does specify that a SIGSEGV signal be raised, but
specifies that the behavior of your program is undefined if that signal
is ignored, or if the signal handler returns.

"undefined behavior" means that anything is permitted, including
aborting your program. Your program aborted, which is one of the
permitted behaviors. On what grounds are you going to report a bug? What
requirement of any standard is violated by the fact that your program
aborted?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Bacarisse  
View profile  
 More options Aug 1 2012, 7:15 pm
Newsgroups: comp.lang.c
From: Ben Bacarisse <ben.use...@bsb.me.uk>
Date: Thu, 02 Aug 2012 00:15:39 +0100
Local: Wed, Aug 1 2012 7:15 pm
Subject: Re: signal() anomaly
Eric Sosman <esos...@ieee-dot-org.invalid> writes:

<snip>

>     What remains unclear (to me) is how the C Standard rules on this
> issue.  We know the behavior is undefined if SIGSEGV goes to a handler
> and the handler returns, but SIG_IGN isn't a "handler."  So it's
> possible the C Standard and POSIX disagree here -- but it would not
> surprise me one little bit to learn that I've overlooked something.

I don't think there is anything explicit, but neither can I see any room
for disagreement but, first, a nit: the behaviour is not undefined if
the signal is the result of a call to 'raise'.  The signal can be
handled or ignored and, if handled, the handler can return.  This is
true of both C and POSIX.

When the signal is not the result of a call to 'raise', both C and POSIX
agree that the handler, if it is set, must not return.  Thus the only
possible disagreement could be when the signal was not raised explicitly
and the signal is being ignored.  As you say, POSIX declares this
explicitly to be UB, but C does not.

However, C does say that a SIGSEGV results from "an invalid access to
storage".  It's hard to imagine an invalid access that does not already
result in UB as far as C is concerned.  Some will be explicit (like *0)
but, since the standard defines what valid accesses are, all the rest
(that are not explicitly UB) must be UB by omission.  And if the access
has already made the behaviour of the program undefined, no signal
setting can alter that.

It's not exactly cast-iron, but I can't see anything in gap between the
two standards on this issue.

--
Ben.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Bacarisse  
View profile  
 More options Aug 1 2012, 7:22 pm
Newsgroups: comp.lang.c
From: Ben Bacarisse <ben.use...@bsb.me.uk>
Date: Thu, 02 Aug 2012 00:22:36 +0100
Local: Wed, Aug 1 2012 7:22 pm
Subject: Re: signal() anomaly
hip cat <nos...@nospam.com> writes:

<snip>

> My code has a certain pointer that sometimes unexpectedly becomes null.
> It would be a lot of work to find every place the pointer gets
> dereferenced and add a null check. So I want to just ignore it by
> catching the signal.

From a purely practical point of view, I can't urge you strongly enough
to run your program using a memory checker like valgrind.  I'd have
swapped all other debugging tools for something like valgrind when I was
wrestling with loads of buggy C (well, non-portable C is probably more
fair), but there was no such tool available.  Don't ignore it now if it
is available on your platform.

--
Ben.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Bowler  
View profile  
 More options Aug 1 2012, 8:05 pm
Newsgroups: comp.lang.c
From: Nick Bowler <nbow...@draconx.ca>
Date: Thu, 2 Aug 2012 00:05:32 +0000 (UTC)
Local: Wed, Aug 1 2012 8:05 pm
Subject: Re: signal() anomaly

On Thu, 02 Aug 2012 00:15:39 +0100, Ben Bacarisse wrote:
> Eric Sosman <esos...@ieee-dot-org.invalid> writes:
> <snip>
>>     What remains unclear (to me) is how the C Standard rules on this
>> issue.  We know the behavior is undefined if SIGSEGV goes to a handler
>> and the handler returns, but SIG_IGN isn't a "handler."  So it's
>> possible the C Standard and POSIX disagree here -- but it would not
>> surprise me one little bit to learn that I've overlooked something.

> I don't think there is anything explicit, but neither can I see any room
> for disagreement but, first, a nit: the behaviour is not undefined if
> the signal is the result of a call to 'raise'.  The signal can be
> handled or ignored and, if handled, the handler can return.  This is
> true of both C and POSIX.

I'm not so certain if it is ok for such a handler to return normally in
standard C.  POSIX provides an explicit exception for a SIGSEGV handler
which returns in the case where the signal was generated by a call to
kill, sigqueue or raise.  (Curiously, pthread_kill is missing from that
list).  However, there is no similar language in the C11 standard,
leading me to believe that the behaviour is undefined regardless of
whether the signal was the result of a call to raise or not.  I've
quoted the relevant passages from C11[1] and SUSv4[2] for comparison.

[1] [C11§7.4.1.1#3] When a signal occurs and func points to a
    function, ... if and when the function returns, if the value
    of sig is SIGFPE, SIGILL, SIGSEGV, or any other implementation-
    defined value corresponding to a computational exception, the
    behaviour is undefined; otherwise ...

[2] [SUSv4§2.4.3] The behavior of a process is undefined after it
    returns normally from a signal-catching function for a SIGBUS,
    SIGFPE, SIGILL, or SIGSEGV signal that was not generated by kill(),
    sigqueue(), or raise().

Nothing in the C11 passage suggests that it is acceptable to return from
a SIGSEGV handler that was invoked due to a call to raise.  Moreover,
from the above passage, this implies that a conforming implementation
could define every single signal to "correspond to a computational
exception".  Therefore, no strictly conforming program may ever return
normally from any signal handler.

Perhaps this is an oversight in the standard, or it's specified
elsewhere and I've simply missed it.  But anyway, all this reinforces
my belief that signals, as described by the C standards, are too
ill-specified to be useful at all, and may as well not be there.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Bacarisse  
View profile  
 More options Aug 1 2012, 9:47 pm
Newsgroups: comp.lang.c
From: Ben Bacarisse <ben.use...@bsb.me.uk>
Date: Thu, 02 Aug 2012 02:47:53 +0100
Local: Wed, Aug 1 2012 9:47 pm
Subject: Re: signal() anomaly

How odd!  I am sure you are right, but I was equally sure I'd seen some
claim that it was OK to return from explicitly raised "computational
exceptions" in the C standard.  Oh well, good to know.  Thanks.

<snip>
--
Ben.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johann Klammer  
View profile  
 More options Aug 2 2012, 12:34 am
Newsgroups: comp.lang.c
From: Johann Klammer <klamm...@NOSPAM.a1.net>
Date: Thu, 02 Aug 2012 06:34:51 +0200
Local: Thurs, Aug 2 2012 12:34 am
Subject: Re: signal() anomaly

It is quite possible. In Linux, at least, where the machine state is
passed to the signal handler. You can find out the instruction that
caused the exception and advance the instruction pointer to point to the
next one, thus avoiding subsequent segfaults. This is rather difficult
with the usual variable-length opcodes, and would require limited
instruction decoding.
...
Of course, that does not mean that your program will do anything
meaningful. Register contents will end up undefined etc...

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stephen Sprunk  
View profile  
 More options Aug 2 2012, 1:55 am
Newsgroups: comp.lang.c
From: Stephen Sprunk <step...@sprunk.org>
Date: Thu, 02 Aug 2012 00:55:48 -0500
Local: Thurs, Aug 2 2012 1:55 am
Subject: Re: signal() anomaly
On 01-Aug-12 13:58, hip cat wrote:

> My code has a certain pointer that sometimes unexpectedly becomes null.
> It would be a lot of work to find every place the pointer gets
> dereferenced and add a null check. So I want to just ignore it by
> catching the signal.

Sorry, but the correct solution is to check for null before you
dereference a pointer, even if it is theoretically "impossible" for that
pointer to be null.

Ignoring a null pointer dereference is pretty much impossible, since
there are no sensible semantics for continuing execution after that
point--and trying to do so will probably just cause other "impossible"
bugs to appear and waste even more of your time.

Treat the disease, not the symptom.

S

--
Stephen Sprunk         "God does not play dice."  --Albert Einstein
CCIE #3723         "God is an inveterate gambler, and He throws the
K5SSS        dice at every possible opportunity." --Stephen Hawking


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ike Naar  
View profile  
 More options Aug 2 2012, 3:01 am
Newsgroups: comp.lang.c
From: Ike Naar <i...@sverige.freeshell.org>
Date: Thu, 2 Aug 2012 07:01:17 +0000 (UTC)
Local: Thurs, Aug 2 2012 3:01 am
Subject: Re: signal() anomaly
On 2012-08-02, Stephen Sprunk <step...@sprunk.org> wrote:

> On 01-Aug-12 13:58, hip cat wrote:
>> My code has a certain pointer that sometimes unexpectedly becomes null.
>> It would be a lot of work to find every place the pointer gets
>> dereferenced and add a null check. So I want to just ignore it by
>> catching the signal.

> Sorry, but the correct solution is to check for null before you
> dereference a pointer, even if it is theoretically "impossible" for that
> pointer to be null.

Following this advice would make it impossible to dereference a pointer:

/* assuming pointer is non-volatile */

if (pointer != NULL)
{
  /* at this point it's theoretically impossible for pointer
     to be null, but let's check anyway */
  if (pointer != NULL)
  {
    /* at this point it's theoretically impossible for pointer
       to be null, but let's check anyway */
    if (pointer != NULL)
    {
      /* at this point it's theoretically impossible for pointer
         to be null, but let's check anyway */
      if (pointer != NULL)
      {
        /* at this point it's theoretically impossible for pointer
           to be null, but let's check anyway */
        if (pointer != NULL)
        {
          /* at this point it's theoretically impossible for pointer
             to be null, but let's check anyway */
          if (pointer != NULL)
          {
            /* at this point it's theoretically impossible for pointer
               to be null, but let's check anyway */

                /* ... etc ad infinitum ... */

                        do_something_with(*pointer);

                /* ... */
          }
        }
      }
    }
  }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul  
View profile  
 More options Aug 2 2012, 7:10 am
Newsgroups: comp.lang.c
From: Paul <nos...@needed.com>
Date: Thu, 02 Aug 2012 07:10:27 -0400
Local: Thurs, Aug 2 2012 7:10 am
Subject: Re: signal() anomaly

I could have mentioned as well, that debuggers are good at noting
such things, and can also alert you when one happens. You can even
single-step your new program, and watch the value of the variable
in question. The debuggers on various platforms, vary in how
"friendly" they are, and some of them, I can't run at all.
Can't seem to succeed in getting them to do anything. The last
one I ran, was in Visual Studio, and that seemed OK. Almost worthwhile,
compared to sprinkling printfs all over the place.

    Paul


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nobody  
View profile  
 More options Aug 2 2012, 7:52 am
Newsgroups: comp.lang.c
From: Nobody <nob...@nowhere.com>
Date: Thu, 02 Aug 2012 12:52:07 +0100
Local: Thurs, Aug 2 2012 7:52 am
Subject: Re: signal() anomaly

On Wed, 01 Aug 2012 20:47:17 +0000, hip cat wrote:
> I think Eric was right on the money asking about the return value. If the
> signal() call returns success, but the "ignore" is ignored, I say that's a
> bug in the C library. I think I'll file a bug report against glibc.

It's not a bug in glibc. You tell it to ignore SIGSEGV, it ignores
SIGSEGV. According to POSIX, ignoring a synchronous[1] SIGSEGV results in
undefined behaviour, which could mean anything, including raising SIGSEGV.

[1] One generated by an invalid memory access. A SIGSEGV generated by
kill(), sigqueue() or raise() *can* be ignored, hence it would be a bug if
glibc reported an error for signal(SIGSEGV, SIG_IGN).

Note that it *is* an error (§2.4.3) to set the action for SIGKILL or
SIGSTOP to SIG_IGN:

        The system shall not allow the action for the signals SIGKILL or SIGSTOP
        to be set to SIG_IGN.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 28   Newer >
« Back to Discussions « Newer topic     Older topic »