Practical character driver

Peter Teoh htmldeveloper at gmail.com
Sun Feb 27 04:22:44 EST 2011


i think pointer can always be localized, as when then pointer is lost, the
memory allocated is still always there, and moreover, the pointer is kept in
a structure.   more likely is a compilation detected error:   when i
compiled it tell me exactly which lines is not initialized:

  CC [M]  drivers/mymodule/mymem1.o
drivers/mymodule/mymem1.c: In function ‘mem_read’:
drivers/mymodule/mymem1.c:98: warning: ISO C90 forbids mixed declarations
and code
drivers/mymodule/mymem1.c: In function ‘mem_write’:
drivers/mymodule/mymem1.c:125: warning: ISO C90 forbids mixed declarations
and code
drivers/mymodule/mymem1.c: In function ‘module_initial’:
drivers/mymodule/mymem1.c:207: warning: ‘memory’ may be used uninitialized
in this function

which is exactly the following:

  memset(device, 0, sizeof(char) * map_size);

  memory->device = device;================> (memory is unitialized)

correct?    sorry if i am wrong....

On Sun, Feb 27, 2011 at 3:52 PM, Mulyadi Santosa
<mulyadi.santosa at gmail.com>wrote:

> On Sun, Feb 27, 2011 at 11:57, Sameer Rahmani <lxsameer at gmail.com> wrote:
> > static int __init module_initial(void)
> > {
> >   dev_t dev;
> >   int result;
> >   struct memmap *memory;
> >
> >   if (major)
> >     {
> >       dev = MKDEV(major, minor);
> >       result = register_chrdev_region(dev, 1, "memchar");
> >     }
> >   else
> >     {
> >       result = alloc_chrdev_region(&dev, minor, 1,
> >                    "memchar");
> >       major = MAJOR(dev);
> >
> >     }
> >   if (result < 0)
> >     {
> >       printk (KERN_ALERT "Cannot register major number.\n");
> >       return result;
> >     }
> >
> >   device = kmalloc(sizeof(char) * map_size, GFP_KERNEL);
> >   if (! device)
> >     {
> >       printk (KERN_ALERT "Allocating device failed.\n");
> >       result = -ENOMEM;
> >       goto fail;
> >     }
> >
> >
> >   memset(device, 0, sizeof(char) * map_size);
> >
> >   memory->device = device;
> >   mem_setup_cdev(memory);
> >
> >
> >   printk(KERN_ALERT "Major: %d", major);
> >   return 0;
> >
> >  fail:
> >   module_cleanup();
> >   return result;
> > }
>
> As you can see by yourself, you put many data structures as locals to
> module_init. So once module_init is thrashed, those variables/pointers
> also gone. Result? Easy to guess...lost reference :)
>
> --
> regards,
>
> Mulyadi Santosa
> Freelance Linux trainer and consultant
>
> blog: the-hydra.blogspot.com
> training: mulyaditraining.blogspot.com
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>



-- 
Regards,
Peter Teoh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110227/bd65c113/attachment-0001.html 


More information about the Kernelnewbies mailing list