[ltp] Segmentation fault in PCI module

Matt Graham linux-thinkpad@linux-thinkpad.org
Thu, 07 Oct 2004 07:06:46 -0400


> I have written a piece of code to detect a pci device, but it is =

> causing  segmentation fault. What might have gone wrong in the =

> code?

> #include<linux/pci.h>
/* snip */
> int pci_init( void )
> {
>   struct pci_dev *pci_device =3D NULL; /* initial */

>   if( !pci_present() ){
>       printk("\nPCI not supported\n");
>       return 0;
>       }  =

>   if(( pci_find_slot(1, PCI_DEVFN( 9, 0))) =3D=3D NULL){
>       printk("\n Device not found\n");
>       return 0;  =

>       }
>   printk("\nVendor ID\t%x",pci_device->vendor);

Hang on.  Did you ever assign a value to pci_device between "initial" and=
 when
you printk() the vendor ID?  If not, it'll still be NULL, and dereferenci=
ng
NULL is an instant segfault.  Confirm this with
"printk("pci_device=3D%x\n",pci_device);" before you dereference pci_devi=
ce. =

And you might want to get some more practice with userspace things before=

moving to kernel code.  HTH,


-- =

Matt G / Dances With Crows
There is no Darkness in Eternity/But only Light too dim for us to see
"I backed up my brain to tape, but tar says the tape contains no data....=
"