In the linux kernel (version 4.8), "struct pid" is defined as follows (from the file: http://lxr.free-electrons.com/source/include/linux/pid.h ). Here, "numbers [1]" (on line 64) is a static array that can have only one element (due to the size of the array referred to as 1).
57 struct pid
58 {
59 atomic_t count;
60 unsigned int level;
61
62 struct hlist_head tasks[PIDTYPE_MAX];
63 struct rcu_head rcu;
64 struct upid numbers[1];
65 };
But then in the following code on line 319 and 320 (from the file: http://lxr.free-electrons.com/source/kernel/pid.c ), the array of "numbers" is inside the for loop as 'numbers [i]' . How is this even right, because the variable "i" cannot have any value other than zero without a segmentation error? I checked the value of "i" during the cycles to see if it will ever be greater than 1. Yes, but it is still, but I do not see any segmentation error. Did I miss something?
297 struct pid *alloc_pid(struct pid_namespace *ns)
298 {
299 struct pid *pid;
300 enum pid_type type;
301 int i, nr;
302 struct pid_namespace *tmp;
303 struct upid *upid;
304 int retval = -ENOMEM;
305
306 pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
307 if (!pid)
308 return ERR_PTR(retval);
309
310 tmp = ns;
311 pid->level = ns->level;
312 for (i = ns->level; i >= 0; i--) {
313 nr = alloc_pidmap(tmp);
314 if (nr < 0) {
315 retval = nr;
316 goto out_free;
317 }
318
319 pid->numbers[i].nr = nr;
320 pid->numbers[i].ns = tmp;
321 tmp = tmp->parent;
322 }
source
share