1 diff -rcp a/nscd/grpcache.c b/nscd/grpcache.c
2 *** a/nscd/grpcache.c Wed Apr 11 12:50:07 2012
3 --- b/nscd/grpcache.c Wed Apr 11 21:45:58 2012
4 *************** cache_addgr (struct database_dyn *db, in
7 const size_t key_len = strlen (key);
8 const size_t buf_len = 3 * sizeof (grp->gr_gid) + key_len + 1;
9 ! char *buf = alloca (buf_len);
15 const size_t key_len = strlen (key);
16 const size_t buf_len = 3 * sizeof (grp->gr_gid) + key_len + 1;
17 ! size_t alloca_used = 0;
18 ! char *buf = alloca_account (buf_len, alloca_used);
22 *************** cache_addgr (struct database_dyn *db, in
24 /* Determine the length of all members. */
25 while (grp->gr_mem[gr_mem_cnt])
27 ! gr_mem_len = (uint32_t *) alloca (gr_mem_cnt * sizeof (uint32_t));
28 for (gr_mem_cnt = 0; grp->gr_mem[gr_mem_cnt]; ++gr_mem_cnt)
30 gr_mem_len[gr_mem_cnt] = strlen (grp->gr_mem[gr_mem_cnt]) + 1;
32 /* Determine the length of all members. */
33 while (grp->gr_mem[gr_mem_cnt])
35 ! gr_mem_len = (uint32_t *) alloca_account (gr_mem_cnt * sizeof (uint32_t),
37 for (gr_mem_cnt = 0; grp->gr_mem[gr_mem_cnt]; ++gr_mem_cnt)
39 gr_mem_len[gr_mem_cnt] = strlen (grp->gr_mem[gr_mem_cnt]) + 1;
40 *************** cache_addgr (struct database_dyn *db, in
42 change. Allocate memory on the cache since it is likely
43 discarded anyway. If it turns out to be necessary to have a
44 new record we can still allocate real memory. */
45 ! bool alloca_used = false;
49 dataset = (struct dataset *) mempool_alloc (db, total + n, 1);
53 change. Allocate memory on the cache since it is likely
54 discarded anyway. If it turns out to be necessary to have a
55 new record we can still allocate real memory. */
56 ! bool dataset_in_stack_or_freed = false;
59 ! if (he == NULL || ! __libc_use_alloca (alloca_used + total + n))
60 dataset = (struct dataset *) mempool_alloc (db, total + n, 1);
63 *************** cache_addgr (struct database_dyn *db, in
65 /* We cannot permanently add the result in the moment. But
66 we can provide the result as is. Store the data in some
68 ! dataset = (struct dataset *) alloca (total + n);
70 /* We cannot add this record to the permanent database. */
74 dataset->head.allocsize = total + n;
76 /* We cannot permanently add the result in the moment. But
77 we can provide the result as is. Store the data in some
79 ! dataset = (struct dataset *) alloca_account (total + n, alloca_used);
81 /* We cannot add this record to the permanent database. */
82 ! dataset_in_stack_or_freed = true;
85 dataset->head.allocsize = total + n;
86 *************** cache_addgr (struct database_dyn *db, in
89 allocated on the stack and need not be freed. */
90 dh->timeout = dataset->head.timeout;
93 + /* If the new record was not allocated on the stack, then it must
94 + be freed. Note that it can no longer be used. */
95 + if (! dataset_in_stack_or_freed)
98 + dataset_in_stack_or_freed = true;
103 *************** cache_addgr (struct database_dyn *db, in
105 key_copy = (char *) newp + (key_copy - (char *) dataset);
107 dataset = memcpy (newp, dataset, total + n);
108 ! alloca_used = false;
111 /* Mark the old record as obsolete. */
113 key_copy = (char *) newp + (key_copy - (char *) dataset);
115 dataset = memcpy (newp, dataset, total + n);
116 ! dataset_in_stack_or_freed = false;
119 /* Mark the old record as obsolete. */
120 *************** cache_addgr (struct database_dyn *db, in
125 ! if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
127 assert (db->wr_fd != -1);
128 assert ((char *) &dataset->resp > (char *) db->data);
133 ! if (__builtin_expect (db->mmap_used, 1) && !dataset_in_stack_or_freed)
135 assert (db->wr_fd != -1);
136 assert ((char *) &dataset->resp > (char *) db->data);
137 *************** cache_addgr (struct database_dyn *db, in
140 /* Add the record to the database. But only if it has not been
141 stored on the stack. */
144 /* If necessary, we also propagate the data to disk. */
148 /* Add the record to the database. But only if it has not been
149 stored on the stack. */
150 ! if (! dataset_in_stack_or_freed)
152 /* If necessary, we also propagate the data to disk. */