Uninitialized static int counters?

Jerry Feldman gaf at blu.org
Fri Feb 6 14:18:06 EST 2009


There is no such thing as Uninitialized static.
All static variables in C are initialialized by default according to the 
C standard.
In the case of an int, it is initialized to 0. In the code below, it is 
printing only the first 20 times mt_ioctl_trans() is called with an 
invalid command. What I see confufsing is:
    do { ... } while(0);
What this means is to go through the loop once. You need a leading curly 
so you can set up counter as a local variable as variable names are 
block scope.
 { ... } would be equivalent to above.

On 02/06/2009 01:57 PM, Michael ODonnell wrote:
> OK - I'm seeing stuff like this the following in some kernel
> syscall handling code and it's making my brain hurt, so I hope
> somebody can explain it:
>
>
>         .
>         .
>         .
> static int mt_ioctl_trans(unsigned int fd, unsigned int cmd, unsigned long arg)
> {
>         mm_segment_t old_fs = get_fs();
>         struct mtget get;
>         struct mtget32 __user *umget32;
>         struct mtpos pos;
>         struct mtpos32 __user *upos32;
>         unsigned long kcmd;
>         void *karg;
>         int err = 0;
>
>         switch(cmd) {
>         case MTIOCPOS32:
>                 kcmd = MTIOCPOS;
>                 karg = &pos;
>                 break;
>         case MTIOCGET32:
>                 kcmd = MTIOCGET;
>                 karg = &get;
>                 break;
>         default:
>                 do {
> WTF ?!?! #=->>          static int count;
> WTF ?!?! #=->>          if (++count <= 20)
>                                 printk("mt_ioctl: Unknown cmd fd(%d) "
>                                        "cmd(%08x) arg(%08x)\n",
>                                        (int)fd, (unsigned int)cmd, (unsigned int)arg);
>                 } while(0);
>                 return -EINVAL;
>         }
>         .
>         .
>         .
>
> ...which, as far as I can tell, should yield effectively random behavior,
> yes?  Depending on the initial value of count we'll print the error
> message some number of times (once per pass through that routine) until
> count is incremented to a value greater than 19, after which we'll be
> silent until it wraps negative.  WTF?  And this construction is repeated
> in several different routines in several different files under fs/ in
> the kernel sources on both my linux-2.6.27.6 Debian machine as well as
> my 2.6.18 CentOS/RHEL machine.
>
> Here's another:
>
> static int ppp_ioctl_trans(unsigned int fd, unsigned int cmd, unsigned long arg)
> {
>         int err;
>
>         switch (cmd) {
>         case PPPIOCGIDLE32:
>                 err = ppp_gidle(fd, cmd, arg);
>                 break;
>
>         case PPPIOCSCOMPRESS32:
>                 err = ppp_scompress(fd, cmd, arg);
>                 break;
>
>         default:
>                 do {
>                         static int count;
>                         if (++count <= 20)
>                                 printk("ppp_ioctl: Unknown cmd fd(%d) "
>                                        "cmd(%08x) arg(%08x)\n",
>                                        (int)fd, (unsigned int)cmd, (unsigned int)arg);
>                 } while(0);
>                 err = -EINVAL;
>                 break;
>         };
>
>         return err;
> }
>
>
>   


-- 
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix
PGP key id: 537C5846
PGP Key fingerprint: 3D1B 8377 A3C0 A5F2 ECBB  CA3B 4607 4319 537C 5846


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 251 bytes
Desc: OpenPGP digital signature
Url : http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20090206/549581c5/attachment.bin 


More information about the gnhlug-discuss mailing list