Re: invalid pointer adress
From: FKothe (fkothe_at_aspecta.com)
Date: 02/11/05
- Next message: Richard Bos: "Re: why this program is not crashing"
- Previous message: SM Ryan: "Re: old style function definitions"
- In reply to: Richard Bos: "Re: invalid pointer adress"
- Next in thread: Richard Bos: "Re: invalid pointer adress"
- Reply: Richard Bos: "Re: invalid pointer adress"
- Reply: Chris Torek: "Re: invalid pointer adress"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 11 Feb 2005 01:26:02 -0800
rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<420b6aaf.190395792@news.individual.net>...
> fkothe@aspecta.com (FKothe) wrote:
>
> > the program below shows a behavior i do not understand. When compiled
> > with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function
> > test_it0 points to an invalid adress and an attempt to write to this
> > pointer causes the program to exit with a core dump.
> > Output after compiling with HP c-compiler:
> > 1. ffffff78
> > 1. 7eff3358
>
> > extern void *memset(void *, int, unsigned long);
>
> Don't do this - #include the proper header.
>
> > static char BLANK_VTRNR[2] = " ";
Yes, you are right, this declaration is absolutly unnecessary, but
commenting this line out the address of v2.p in test_it0 becomes
valid( and identical to that in test_it1 ).
To explain how i came to this program:
I am working on a larger project, which exits with a core dump. The
reason for this is that there ist a strcpy instruction to a misleading
pointer.
After I found where the pointer gets its invalid adress, i copied this
part to a new programm and included al the projects includes. As the
error still occures i startet to remove all includes and definitions
until i found the definition of "static char BLANK_VTRNR[2] = " ";"
changed the program behavior. Because i do not have any idea why this
happened I asked for help here.
After I changed the Programm in the way you suggested the same error
appears:
./test_it
1. ffffff78
1. 7eff3328
----------/snip/--------------
#include <stdio.h>
static char BLANK_VTRNR[2] = " ";
struct s1{ char c[ 81 ]; };
struct s2{ void *p; };
void test_it0( void )
{
struct s1 v1;
memset( (void*)&v1, 0, sizeof( struct s1 ) );
struct s2 v2 = { &v1.c };
printf( "1. %p\n", v2.p );
/*
strcpy( v2.p, "Hallo ich schreibe mal was rein.." );
printf( "%s\n", v2.p );
*/
}
void test_it1(void)
{
struct s1 v1;
struct s2 v2= { &v1.c };
printf( "1. %p\n", v2.p );
/*
strcpy( v2.p, "Hallo ich schreibe mal was rein.." );
printf( "%s\n", v2.p );
*/
}
int main ()
{
test_it0();
test_it1();
return 0;
}
----------/snip/--------------
Unfortunately, removing the static char BLANK_VTRNR[2] = " ";
instruction in the projekt does not have the same effect.
In the following you can see the part of the original code, which has
the described error:
int leseExterneHinweise_masch_storno( void )
{
int iRes = -1; /* Fehler */
char cfDateiMitPfad[ 1024 ];
int iDateiId;
BOOL bEofDatei = FALSE;
int iAnzSaetze = 0;
t_MaschStoExtern AktSatz;
t_ptrMaschStoExtern pNeuerSatz = NULL;
t_ptrMaschStoExtern pLetzterSatz = NULL;
t_AusdatFeldbeschreibung ExterneHinweise[ MASCHSTO_EXTERN_ANZ_FD ]
=
{ /* Typ, Laenge , Pointer zur
Variablen , Fehler */
{ AUSDAT_TYPE_STRING, MASCHSTO_ATTRNAME_LEN,
AktSatz.cfAttributName, FALSE },
{ AUSDAT_TYPE_STRING, MASCHSTO_ATTRWERT_LEN,
AktSatz.cfAttributWert, FALSE },
{ AUSDAT_TYPE_STRING, MASCHSTO_AUSGABE_LEN , AktSatz.cfAusgabe
, FALSE }
};
/* breakpoint here */
...
where
typedef struct s_AusdatFeldbeschreibung
{
short sTyp;
int iLaenge;
void *pWert;
BOOL bFehlerhaft;
} t_AusdatFeldbeschreibung;
and
typedef struct sMaschStoExtern t_MaschStoExtern;
typedef t_MaschStoExtern *t_ptrMaschStoExtern;
struct sMaschStoExtern
{
char cfAttributName[ MASCHSTO_ATTRNAME_LEN + 1 ];
char cfAttributWert[ MASCHSTO_ATTRWERT_LEN + 1 ];
char cfAusgabe[ MASCHSTO_AUSGABE_LEN + 1 ];
t_ptrMaschStoExtern pNext;
};
When having a look at the structures on the breakpoint:
&AktSatz.cfAttributName = 0x800003ffff429cb0
ExterneHinweise[0]:
sTyp = 6 ( according to definition of AUSDAT_TYPE_STRING )
iLaenge = 30 ( according to definition MASCHSTO_ATTRNAME_LEN )
pWert = 0xfffffffffffffea0 <error reading Address
0xfffffffffffffea0:Bad Address>
bFehlerhaft = 0 ( according to definition of FALSE )
But:
&ExterneHinweise[0].pWert( signed char **) 0x800003ffff429d60
>
> (Whatever is this for? It isn't used.)
>
> > struct s1{ char c[ 81 ]; };
> > struct s2{ void *p; };
>
> You cause undefined behaviour:
>
> > void test_it0( void )
> > {
> > struct s1 v1;
> > memset( (void*)&v1, 0, sizeof( struct s1 ) );
> > struct s2 v2 = { &v1.c };
> > printf( "1. %lx\n", v2.p );
>
> here...
>
> > }
> >
> > void test_it1(void)
> > {
> > struct s1 v1;
> > struct s2 v2= { &v1.c };
> > printf( "1. %lx\n", v2.p );
>
> ...and here...
>
> > }
> >
> > void main ()
>
> ...and here.
>
> The first two are probably (!) the most important. You cannot expect
> consistent answers when you lie to printf(). You're not passing an
> unsigned integer, you're passing a pointer. Replace %lx with %p, twice,
> replace void main() with int main(void), return 0 from main(), and try
> again.
>
> Richard
- Next message: Richard Bos: "Re: why this program is not crashing"
- Previous message: SM Ryan: "Re: old style function definitions"
- In reply to: Richard Bos: "Re: invalid pointer adress"
- Next in thread: Richard Bos: "Re: invalid pointer adress"
- Reply: Richard Bos: "Re: invalid pointer adress"
- Reply: Chris Torek: "Re: invalid pointer adress"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|