|
| 1 | +From c128ad8e830e90a429eaeccc3fb000a73fd6779c Mon Sep 17 00:00:00 2001 |
| 2 | +From: Jakub Jelinek < [email protected]> |
| 3 | +Date: Tue, 19 Sep 2023 09:26:35 +0200 |
| 4 | +Subject: [PATCH] libgomp: Handle NULL environ like pointer to NULL pointer |
| 5 | + [PR111413] |
| 6 | + |
| 7 | +clearenv function just sets environ to NULL (after sometimes freeing it), |
| 8 | +rather than setting it to a pointer to NULL, and our code was assuming |
| 9 | +it is always non-NULL. |
| 10 | + |
| 11 | +Fixed thusly, the change seems to be large but actually is just |
| 12 | ++ if (environ) |
| 13 | + for (env = environ; *env != 0; env++) |
| 14 | +plus reindentation. I've also noticed the block after this for loop |
| 15 | +was badly indented (too much) and fixed that too. |
| 16 | + |
| 17 | +No testcase added, as it needs clearenv + dlopen. |
| 18 | + |
| 19 | +2023-09-19 Jakub Jelinek < [email protected]> |
| 20 | + |
| 21 | + PR libgomp/111413 |
| 22 | + * env.c (initialize_env): Don't dereference environ if it is NULL. |
| 23 | + Reindent. |
| 24 | + |
| 25 | +(cherry picked from commit 15345980633c502f0486a2e40e96224f49134130) |
| 26 | +--- |
| 27 | + libgomp/env.c | 251 +++++++++++++++++++++++++------------------------- |
| 28 | + 1 file changed, 126 insertions(+), 125 deletions(-) |
| 29 | + |
| 30 | +diff --git a/libgomp/env.c b/libgomp/env.c |
| 31 | +index e7a035b593c..65088f2fe18 100644 |
| 32 | +--- a/libgomp/env.c |
| 33 | ++++ b/libgomp/env.c |
| 34 | +@@ -2059,139 +2059,140 @@ initialize_env (void) |
| 35 | + none = gomp_get_initial_icv_item (GOMP_DEVICE_NUM_FOR_NO_SUFFIX); |
| 36 | + initialize_icvs (&none->icvs); |
| 37 | + |
| 38 | +- for (env = environ; *env != 0; env++) |
| 39 | +- { |
| 40 | +- if (!startswith (*env, "OMP_")) |
| 41 | +- continue; |
| 42 | +- |
| 43 | +- /* Name of the environment variable without suffix "OMP_". */ |
| 44 | +- char *name = *env + sizeof ("OMP_") - 1; |
| 45 | +- for (omp_var = 0; omp_var < OMP_VAR_CNT; omp_var++) |
| 46 | +- { |
| 47 | +- if (startswith (name, envvars[omp_var].name)) |
| 48 | +- { |
| 49 | +- pos = envvars[omp_var].name_len; |
| 50 | +- if (name[pos] == '=') |
| 51 | +- { |
| 52 | +- pos++; |
| 53 | +- flag_var_addr |
| 54 | +- = add_initial_icv_to_list (GOMP_DEVICE_NUM_FOR_NO_SUFFIX, |
| 55 | +- envvars[omp_var].flag_vars[0], |
| 56 | +- params); |
| 57 | +- } |
| 58 | +- else if (startswith (&name[pos], "_DEV=") |
| 59 | +- && envvars[omp_var].flag & GOMP_ENV_SUFFIX_DEV) |
| 60 | +- { |
| 61 | +- pos += 5; |
| 62 | +- flag_var_addr |
| 63 | +- = add_initial_icv_to_list (GOMP_DEVICE_NUM_FOR_DEV, |
| 64 | +- envvars[omp_var].flag_vars[0], |
| 65 | +- params); |
| 66 | +- } |
| 67 | +- else if (startswith (&name[pos], "_ALL=") |
| 68 | +- && envvars[omp_var].flag & GOMP_ENV_SUFFIX_ALL) |
| 69 | +- { |
| 70 | +- pos += 5; |
| 71 | +- flag_var_addr |
| 72 | +- = add_initial_icv_to_list (GOMP_DEVICE_NUM_FOR_ALL, |
| 73 | +- envvars[omp_var].flag_vars[0], |
| 74 | +- params); |
| 75 | +- } |
| 76 | +- else if (startswith (&name[pos], "_DEV_") |
| 77 | +- && envvars[omp_var].flag & GOMP_ENV_SUFFIX_DEV_X) |
| 78 | +- { |
| 79 | +- pos += 5; |
| 80 | +- if (!get_device_num (*env, &name[pos], &dev_num, |
| 81 | +- &dev_num_len)) |
| 82 | +- break; |
| 83 | +- |
| 84 | +- pos += dev_num_len + 1; |
| 85 | +- flag_var_addr |
| 86 | +- = add_initial_icv_to_list (dev_num, |
| 87 | +- envvars[omp_var].flag_vars[0], |
| 88 | +- params); |
| 89 | +- } |
| 90 | +- else |
| 91 | +- { |
| 92 | +- gomp_error ("Invalid environment variable in %s", *env); |
| 93 | +- break; |
| 94 | +- } |
| 95 | +- env_val = &name[pos]; |
| 96 | ++ if (environ) |
| 97 | ++ for (env = environ; *env != 0; env++) |
| 98 | ++ { |
| 99 | ++ if (!startswith (*env, "OMP_")) |
| 100 | ++ continue; |
| 101 | + |
| 102 | +- if (envvars[omp_var].parse_func (*env, env_val, params)) |
| 103 | +- { |
| 104 | +- for (i = 0; i < 3; ++i) |
| 105 | +- if (envvars[omp_var].flag_vars[i]) |
| 106 | +- gomp_set_icv_flag (flag_var_addr, |
| 107 | +- envvars[omp_var].flag_vars[i]); |
| 108 | +- else |
| 109 | ++ /* Name of the environment variable without suffix "OMP_". */ |
| 110 | ++ char *name = *env + sizeof ("OMP_") - 1; |
| 111 | ++ for (omp_var = 0; omp_var < OMP_VAR_CNT; omp_var++) |
| 112 | ++ { |
| 113 | ++ if (startswith (name, envvars[omp_var].name)) |
| 114 | ++ { |
| 115 | ++ pos = envvars[omp_var].name_len; |
| 116 | ++ if (name[pos] == '=') |
| 117 | ++ { |
| 118 | ++ pos++; |
| 119 | ++ flag_var_addr |
| 120 | ++ = add_initial_icv_to_list (GOMP_DEVICE_NUM_FOR_NO_SUFFIX, |
| 121 | ++ envvars[omp_var].flag_vars[0], |
| 122 | ++ params); |
| 123 | ++ } |
| 124 | ++ else if (startswith (&name[pos], "_DEV=") |
| 125 | ++ && envvars[omp_var].flag & GOMP_ENV_SUFFIX_DEV) |
| 126 | ++ { |
| 127 | ++ pos += 5; |
| 128 | ++ flag_var_addr |
| 129 | ++ = add_initial_icv_to_list (GOMP_DEVICE_NUM_FOR_DEV, |
| 130 | ++ envvars[omp_var].flag_vars[0], |
| 131 | ++ params); |
| 132 | ++ } |
| 133 | ++ else if (startswith (&name[pos], "_ALL=") |
| 134 | ++ && envvars[omp_var].flag & GOMP_ENV_SUFFIX_ALL) |
| 135 | ++ { |
| 136 | ++ pos += 5; |
| 137 | ++ flag_var_addr |
| 138 | ++ = add_initial_icv_to_list (GOMP_DEVICE_NUM_FOR_ALL, |
| 139 | ++ envvars[omp_var].flag_vars[0], |
| 140 | ++ params); |
| 141 | ++ } |
| 142 | ++ else if (startswith (&name[pos], "_DEV_") |
| 143 | ++ && envvars[omp_var].flag & GOMP_ENV_SUFFIX_DEV_X) |
| 144 | ++ { |
| 145 | ++ pos += 5; |
| 146 | ++ if (!get_device_num (*env, &name[pos], &dev_num, |
| 147 | ++ &dev_num_len)) |
| 148 | + break; |
| 149 | +- } |
| 150 | + |
| 151 | +- break; |
| 152 | +- } |
| 153 | +- } |
| 154 | +- } |
| 155 | ++ pos += dev_num_len + 1; |
| 156 | ++ flag_var_addr |
| 157 | ++ = add_initial_icv_to_list (dev_num, |
| 158 | ++ envvars[omp_var].flag_vars[0], |
| 159 | ++ params); |
| 160 | ++ } |
| 161 | ++ else |
| 162 | ++ { |
| 163 | ++ gomp_error ("Invalid environment variable in %s", *env); |
| 164 | ++ break; |
| 165 | ++ } |
| 166 | ++ env_val = &name[pos]; |
| 167 | + |
| 168 | +- all = gomp_get_initial_icv_item (GOMP_DEVICE_NUM_FOR_ALL); |
| 169 | +- for (omp_var = 0; omp_var < OMP_HOST_VAR_CNT; omp_var++) |
| 170 | +- { |
| 171 | +- if (none != NULL |
| 172 | +- && gomp_get_icv_flag (none->flags, host_envvars[omp_var].flag_var)) |
| 173 | +- get_icv_member_addr (&none->icvs, |
| 174 | +- host_envvars[omp_var].flag_var, params); |
| 175 | +- else if (all != NULL |
| 176 | +- && gomp_get_icv_flag (all->flags, |
| 177 | +- host_envvars[omp_var].flag_var)) |
| 178 | +- get_icv_member_addr (&all->icvs, host_envvars[omp_var].flag_var, |
| 179 | +- params); |
| 180 | +- else |
| 181 | +- continue; |
| 182 | ++ if (envvars[omp_var].parse_func (*env, env_val, params)) |
| 183 | ++ { |
| 184 | ++ for (i = 0; i < 3; ++i) |
| 185 | ++ if (envvars[omp_var].flag_vars[i]) |
| 186 | ++ gomp_set_icv_flag (flag_var_addr, |
| 187 | ++ envvars[omp_var].flag_vars[i]); |
| 188 | ++ else |
| 189 | ++ break; |
| 190 | ++ } |
| 191 | + |
| 192 | +- switch (host_envvars[omp_var].type_code) |
| 193 | +- { |
| 194 | +- case PARSE_INT: |
| 195 | +- for (i = 0; i < 3; ++i) |
| 196 | +- if (host_envvars[omp_var].dest[i] != NULL && params[i] != NULL) |
| 197 | +- *(int *) (host_envvars[omp_var].dest[i]) = *(int *) params[i]; |
| 198 | +- break; |
| 199 | +- case PARSE_BOOL: |
| 200 | +- for (i = 0; i < 3; ++i) |
| 201 | +- if (host_envvars[omp_var].dest[i] != NULL && params[i] != NULL) |
| 202 | +- *(bool *) (host_envvars[omp_var].dest[i]) = *(bool *) params[i]; |
| 203 | +- break; |
| 204 | +- case PARSE_UINT: |
| 205 | +- for (i = 0; i < 3; ++i) |
| 206 | +- if (host_envvars[omp_var].dest[i] != NULL && params[i] != NULL) |
| 207 | +- *(unsigned int *) (host_envvars[omp_var].dest[i]) |
| 208 | +- = *(unsigned int *) params[i]; |
| 209 | +- break; |
| 210 | +- case PARSE_ULONG: |
| 211 | +- for (i = 0; i < 3; ++i) |
| 212 | +- if (host_envvars[omp_var].dest[i] != NULL && params[i] != NULL) |
| 213 | +- *(unsigned long *) (host_envvars[omp_var].dest[i]) |
| 214 | +- = *(unsigned long *) params[i]; |
| 215 | +- break; |
| 216 | +- case PARSE_UCHAR: |
| 217 | +- for (i = 0; i < 3; ++i) |
| 218 | +- if (host_envvars[omp_var].dest[i] != NULL && params[i] != NULL) |
| 219 | +- *(unsigned char *) (host_envvars[omp_var].dest[i]) |
| 220 | +- = *(unsigned char *) params[i]; |
| 221 | +- break; |
| 222 | +- case PARSE_SCHEDULE: |
| 223 | +- *(enum gomp_schedule_type *) (host_envvars[omp_var].dest[0]) |
| 224 | +- = *(enum gomp_schedule_type *) params[0]; |
| 225 | +- *(int *) (host_envvars[omp_var].dest[1]) = *(int *) params[1]; |
| 226 | +- break; |
| 227 | +- case PARSE_BIND: |
| 228 | +- *(char *) (host_envvars[omp_var].dest[0]) = *(char *) params[0]; |
| 229 | +- *(char **) (host_envvars[omp_var].dest[1]) = *(char **) params[1]; |
| 230 | +- *(unsigned long *) (host_envvars[omp_var].dest[2]) |
| 231 | +- = *(unsigned long *) params[2]; |
| 232 | +- break; |
| 233 | ++ break; |
| 234 | ++ } |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | ++ all = gomp_get_initial_icv_item (GOMP_DEVICE_NUM_FOR_ALL); |
| 239 | ++ for (omp_var = 0; omp_var < OMP_HOST_VAR_CNT; omp_var++) |
| 240 | ++ { |
| 241 | ++ if (none != NULL |
| 242 | ++ && gomp_get_icv_flag (none->flags, host_envvars[omp_var].flag_var)) |
| 243 | ++ get_icv_member_addr (&none->icvs, |
| 244 | ++ host_envvars[omp_var].flag_var, params); |
| 245 | ++ else if (all != NULL |
| 246 | ++ && gomp_get_icv_flag (all->flags, |
| 247 | ++ host_envvars[omp_var].flag_var)) |
| 248 | ++ get_icv_member_addr (&all->icvs, host_envvars[omp_var].flag_var, |
| 249 | ++ params); |
| 250 | ++ else |
| 251 | ++ continue; |
| 252 | ++ |
| 253 | ++ switch (host_envvars[omp_var].type_code) |
| 254 | ++ { |
| 255 | ++ case PARSE_INT: |
| 256 | ++ for (i = 0; i < 3; ++i) |
| 257 | ++ if (host_envvars[omp_var].dest[i] != NULL && params[i] != NULL) |
| 258 | ++ *(int *) (host_envvars[omp_var].dest[i]) = *(int *) params[i]; |
| 259 | ++ break; |
| 260 | ++ case PARSE_BOOL: |
| 261 | ++ for (i = 0; i < 3; ++i) |
| 262 | ++ if (host_envvars[omp_var].dest[i] != NULL && params[i] != NULL) |
| 263 | ++ *(bool *) (host_envvars[omp_var].dest[i]) = *(bool *) params[i]; |
| 264 | ++ break; |
| 265 | ++ case PARSE_UINT: |
| 266 | ++ for (i = 0; i < 3; ++i) |
| 267 | ++ if (host_envvars[omp_var].dest[i] != NULL && params[i] != NULL) |
| 268 | ++ *(unsigned int *) (host_envvars[omp_var].dest[i]) |
| 269 | ++ = *(unsigned int *) params[i]; |
| 270 | ++ break; |
| 271 | ++ case PARSE_ULONG: |
| 272 | ++ for (i = 0; i < 3; ++i) |
| 273 | ++ if (host_envvars[omp_var].dest[i] != NULL && params[i] != NULL) |
| 274 | ++ *(unsigned long *) (host_envvars[omp_var].dest[i]) |
| 275 | ++ = *(unsigned long *) params[i]; |
| 276 | ++ break; |
| 277 | ++ case PARSE_UCHAR: |
| 278 | ++ for (i = 0; i < 3; ++i) |
| 279 | ++ if (host_envvars[omp_var].dest[i] != NULL && params[i] != NULL) |
| 280 | ++ *(unsigned char *) (host_envvars[omp_var].dest[i]) |
| 281 | ++ = *(unsigned char *) params[i]; |
| 282 | ++ break; |
| 283 | ++ case PARSE_SCHEDULE: |
| 284 | ++ *(enum gomp_schedule_type *) (host_envvars[omp_var].dest[0]) |
| 285 | ++ = *(enum gomp_schedule_type *) params[0]; |
| 286 | ++ *(int *) (host_envvars[omp_var].dest[1]) = *(int *) params[1]; |
| 287 | ++ break; |
| 288 | ++ case PARSE_BIND: |
| 289 | ++ *(char *) (host_envvars[omp_var].dest[0]) = *(char *) params[0]; |
| 290 | ++ *(char **) (host_envvars[omp_var].dest[1]) = *(char **) params[1]; |
| 291 | ++ *(unsigned long *) (host_envvars[omp_var].dest[2]) |
| 292 | ++ = *(unsigned long *) params[2]; |
| 293 | ++ break; |
| 294 | ++ } |
| 295 | ++ } |
| 296 | ++ |
| 297 | + if (((none != NULL && gomp_get_icv_flag (none->flags, GOMP_ICV_BIND)) |
| 298 | + || (all != NULL && gomp_get_icv_flag (all->flags, GOMP_ICV_BIND))) |
| 299 | + && gomp_global_icv.bind_var == omp_proc_bind_false) |
| 300 | +-- |
| 301 | +2.34.1 |
| 302 | + |
0 commit comments