Small question about segfaults

View: New views
4 Messages — Rating Filter:   Alert me  

Small question about segfaults

by Jehan-4 :: Rate this Message:

| View Threaded | Show Only this Message


Hi,

in a C program, does the program send a SIGSEV signal (so ends with a
"Segmentation fault") immediately when you try to read or write in a
non-allocated memory, or does it do so only when it reads/writes in a
forbidden location, allocated for another program? What I means is: if you
go out of your allocated memory, but this segment belongs to no other
processus, then will it segfault?

I am trying to understand such a violation in the utf-8 branch, I found the
line where it segfaults with valgring/gdb, but don't manage to find why the
pointer was not allocated (my first verification seem to conclude it should
be allocated), and why it does not always segfault to the same line/column
for the exact same action.

And for gdb experts (or valgrind, or any other debugging program), do you
know if it is possible to focus on one pointer-variable, and follow its
memory allocation/liberation and size in the program run?
Thanks.

Jehan

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Materm-devel mailing list
Materm-devel@...
https://lists.sourceforge.net/lists/listinfo/materm-devel
mrxvt home page: http://materm.sourceforge.net

Parent Message unknown Re: Small question about segfaults

by Jehan-4 :: Rate this Message:

| View Threaded | Show Only this Message

Frederik Deweerdt writes:

> From the program's point of view, all memory space is his, some
> addresses are allocated, some not. But all the memory (eg. from 0 to
> 0xffffffff on a 32bits arch) is potentially his.

Ok, so what I understand here is that you confirm that the program won't
segfault as long as I am not accessing another program's allocated memory,
even if I get out my own allocated memory. Is that so?

>>
>> I am trying to understand such a violation in the utf-8 branch, I found the
>> line where it segfaults with valgring/gdb, but don't manage to find why the
>> pointer was not allocated (my first verification seem to conclude it should
>> be allocated), and why it does not always segfault to the same line/column
>> for the exact same action.
> Care to send valgrind's trace?

Yes I can and I will as soon as I have some time (right now, I cannot).
Several memory loss are seen in Valgrind, and I could fix some of them (4
exactly, which were already existing before my branch I think). But for many
of those I have looked at, I wonder whether they are not outside the
program, in the used library (so we cannot do much).

> You could have a look at gdb's watch.

Ok thanks for the hint. It seems what I was looking for. I should have read
better the doc. I will look all what I can do with this...

Jehan

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Materm-devel mailing list
Materm-devel@...
https://lists.sourceforge.net/lists/listinfo/materm-devel
mrxvt home page: http://materm.sourceforge.net

Re: Small question about segfaults

by Gautam Iyer-3 :: Rate this Message:

| View Threaded | Show Only this Message

On Mon, Oct 20, 2008 at 04:35:06PM +0200, jehan wrote:

>> From the program's point of view, all memory space is his, some
>> addresses are allocated, some not. But all the memory (eg. from 0 to
>> 0xffffffff on a 32bits arch) is potentially his.
>
> Ok, so what I understand here is that you confirm that the program
> won't segfault as long as I am not accessing another program's
> allocated memory, even if I get out my own allocated memory. Is that
> so?

It was my understanding that as soon as you try and read / write to any
memory that is not yours, you get a segfault. Regardless weather the
memory is used by another program or not. This is a "safety" feature of
the kernel / glibc, and is generally viewed as a good thing.

I'm more of a mathematician than a C programmer, so might be totally off
on this...

GI

--
Twenty Ways To Maintain A Healthy Level of Insanity
4. Put your garbage can on your desk and label it "In".


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Materm-devel mailing list
Materm-devel@...
https://lists.sourceforge.net/lists/listinfo/materm-devel
mrxvt home page: http://materm.sourceforge.net

attachment0 (204 bytes) Download Attachment

Re: Small question about segfaults

by Terminator-2 :: Rate this Message:

| View Threaded | Show Only this Message

The No.1 rule in C/C++ programming is: never touch any memory that
are not allocated by your program, even if it is a single byte. Yes, the
program may not segfault immediately when your program accesses
memory beyond its boundary. But it can cause catastrophe later, such
as airplane crashes. ;-)  Google "Smash the stack for fun and profit",
you will get more ideas on the unpleasant result.

A good practice is: always initialize a pointer with NULL value, assign
it with the memory by malloc/realloc later. after free it, always reset it
to NULL value. Thus, if your program accidentally access the pointer
after freeing the memory it pointed to, you get segfault immediately.
And then you know there is a bug in the program.

On Mon, Oct 20, 2008 at 6:12 AM, jehan <jehan@...> wrote:

Hi,

in a C program, does the program send a SIGSEV signal (so ends with a
"Segmentation fault") immediately when you try to read or write in a
non-allocated memory, or does it do so only when it reads/writes in a
forbidden location, allocated for another program? What I means is: if you
go out of your allocated memory, but this segment belongs to no other
processus, then will it segfault?

I am trying to understand such a violation in the utf-8 branch, I found the
line where it segfaults with valgring/gdb, but don't manage to find why the
pointer was not allocated (my first verification seem to conclude it should
be allocated), and why it does not always segfault to the same line/column
for the exact same action.

And for gdb experts (or valgrind, or any other debugging program), do you
know if it is possible to focus on one pointer-variable, and follow its
memory allocation/liberation and size in the program run?
Thanks.

Jehan

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Materm-devel mailing list
Materm-devel@...
https://lists.sourceforge.net/lists/listinfo/materm-devel
mrxvt home page: http://materm.sourceforge.net


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Materm-devel mailing list
Materm-devel@...
https://lists.sourceforge.net/lists/listinfo/materm-devel
mrxvt home page: http://materm.sourceforge.net