PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Finding invalid pointers
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Finding invalid pointers

Réponse
 
LinkBack Outils de la discussion
Vieux 04/02/2008, 17h14   #1
Morris Dovey
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding invalid pointers

TDB wrote:
>
> int main() {
> void *p;
>
> p=(void *)0x00000005;
>
> // Here
> printf(" %d ",* (int *)p);
>
> return 0;
> }
>
> When I executed the above code using GCC and VC++, it caused
> segmentation error and windows error..
>
> I need a condition that can find whether the pointer address will
> cause error or not..
>
> Any suggestions to construct that condition ?


Peek/poke games require either having access to a memory map,
hooks into the OS (for dynamically-allocated areas), or some way
to intercept the segmentation error and render it non-fatal.

If you don't have one of those three facilities, then you're
stuck with the existing mechanism that detects the invalid access
and terminates the program.

FYI *p == 0xC3 on all CP/M systems. :-)

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
  Réponse avec citation
Vieux 04/02/2008, 17h57   #2
TDB
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Finding invalid pointers



int main() {
void *p;

p=(void *)0x00000005;

// Here
printf(" %d ",* (int *)p);

return 0;
}

When I executed the above code using GCC and VC++, it caused
segmentation error and windows error..

I need a condition that can find whether the pointer address will
cause error or not..

Any suggestions to construct that condition ?


  Réponse avec citation
Vieux 04/02/2008, 18h02   #3
Ulrich Eckhardt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding invalid pointers

TDB wrote:
> void *p;
>
> p=(void *)0x00000005;
>
> // Here
> printf(" %d ",* (int *)p);

[..]
> When I executed the above code using GCC and VC++, it caused
> segmentation error and windows error..
>
> I need a condition that can find whether the pointer address will
> cause error or not..


There isn't any that is not system-dependant. As a rule of thumb, make sure
that pointers are zero unless they point to something real. Then, you can
simply check them with 'if(p)' or 'if(p!=NULL)'.

Uli

  Réponse avec citation
Vieux 04/02/2008, 18h10   #4
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding invalid pointers

In article <6afed105-35a0-43d3-90a0-d1b703b01ba9@u10g2000prn.googlegroups.com>,
TDB <vnrbabu@gmail.com> wrote:

>int main() {
> void *p;


> p=(void *)0x00000005;


> // Here
> printf(" %d ",* (int *)p);


> return 0;
> }


> When I executed the above code using GCC and VC++, it caused
>segmentation error and windows error..


> I need a condition that can find whether the pointer address will
>cause error or not..


> Any suggestions to construct that condition ?


There is no facility in standard C that would allow you to make
that determination.

For any given pointer value, the answer of whether an access
would lead to an error or not can be dynamic, changing over time.

For example the pointer might be to an object that used to exist
but which has been returned to the operating system with the
address having been removed from range of accessible virtual addresses.

Alternately, the pointer might happen to be to a location that
was not in range before, but is in range right now.

Or the pointer might happen to be to an I/O address to a device
that is wandering in and out of availability (e.g., USB devices
get unplugged, SCSI addresses have bus faults.)

You may wish to investigate use of signal() and SIGSEGV .
It is not certain that a SIGSEGV will be raised for every illegal
access (indeed, it is unlikely on Unix machines, which tend to
have a finer discrimination for different kinds of illegal access),
but you might happen to find that on every implementation you wish
to port to that it is sufficient for whatever it is you are trying to do.
--
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth
  Réponse avec citation
Vieux 04/02/2008, 22h50   #5
M.Caggiano
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding invalid pointers

On 4 Feb, 18:57, TDB <vnrb...@gmail.com> wrote:
> int main() {
> void *p;
>
> p=(void *)0x00000005;
>
> // Here
> printf(" %d ",* (int *)p);
>
> return 0;
> }
>
> When I executed the above code using GCC and VC++, it caused
> segmentation error and windows error..
>
> I need a condition that can find whether the pointer address will
> cause error or not..
>
> Any suggestions to construct that condition ?


In my opinion, you are wrong to use the pointer in that way. If you've
declared p with void, but after you've used then it as an int.
  Réponse avec citation
Vieux 04/02/2008, 22h59   #6
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding invalid pointers

In article <6acdf699-c9f4-42b7-be61-e9d5c73323c0@u10g2000prn.googlegroups.com>,
M.Caggiano <Michele.Caggiano@gmail.com> wrote:
>On 4 Feb, 18:57, TDB <vnrb...@gmail.com> wrote:
>> int main() {
>> void *p;


>> p=(void *)0x00000005;


>> // Here
>> printf(" %d ",* (int *)p);


>> return 0;
>> }



>In my opinion, you are wrong to use the pointer in that way. If you've
>declared p with void, but after you've used then it as an int.


It would not have mattered for the purpose of his question if his
code had been

int main() {
int *p;
p = (int *)0x00000005;
printf(" %d ",* (int *)p);
return 0;
}

This code is not inherently invalid: C implementations are permitted
to define meaningful conversions between integral values and pointers,
but they are not required to, and the result of the dereference
is of course undefined if the resulting pointer is not properly
aligned or does not point to an object.
--
"I will speculate that [...] applications [...] could actually see a
performance boost for most users by going dual-core [...] because it
is running the adware and spyware that [...] are otherwise slowing
down the single CPU that user has today" -- Herb Sutter
  Réponse avec citation
Vieux 04/02/2008, 23h13   #7
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding invalid pointers

Morris Dovey wrote:
> TDB wrote:
>
>> int main() {
>> void *p;
>>
>> p=(void *)0x00000005;
>>
>> // Here
>> printf(" %d ",* (int *)p);
>> return 0;
>> }
>>
>> When I executed the above code using GCC and VC++, it caused
>> segmentation error and windows error..
>>
>> I need a condition that can find whether the pointer address
>> will cause error or not..
>>
>> Any suggestions to construct that condition ?

>
> Peek/poke games require either having access to a memory map,
> hooks into the OS (for dynamically-allocated areas), or some way
> to intercept the segmentation error and render it non-fatal.
>
> If you don't have one of those three facilities, then you're
> stuck with the existing mechanism that detects the invalid
> access and terminates the program.
>
> FYI *p == 0xC3 on all CP/M systems. :-)


I can make it work with 0xCD :-)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 05/02/2008, 01h13   #8
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding invalid pointers

"M.Caggiano" <Michele.Caggiano@gmail.com> writes:
> On 4 Feb, 18:57, TDB <vnrb...@gmail.com> wrote:
>> int main() {
>> void *p;
>>
>> p=(void *)0x00000005;
>>
>> // Here
>> printf(" %d ",* (int *)p);
>>
>> return 0;
>> }
>>
>> When I executed the above code using GCC and VC++, it caused
>> segmentation error and windows error..
>>
>> I need a condition that can find whether the pointer address will
>> cause error or not..
>>
>> Any suggestions to construct that condition ?

>
> In my opinion, you are wrong to use the pointer in that way. If you've
> declared p with void, but after you've used then it as an int.


No, that's not what he's doing.

The assignment converts the value 0x00000005 from int to void*. The
result of the conversion is implementation-defined, but it's legal.

In the printf call, the value of p is converted from void* to int* by
the cast, and then the resulting int* value is dereferenced, yielding
a value of type int.

Perhaps you missed the "*" operator (as I briefly did on my first
reading). The spacing (no blank after the comma, blank after the "*")
makes that an easy mistake to make.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h56.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18482 seconds with 16 queries