Skip to content

Commit df9c3c2

Browse files
committed
Use 'environ' C extern for environment variables (except Mac OS)
The function __fort_getenv uses the C extern variable environ to get the values of the environment variables. What's happening is 'environ' is getting reallocated, but this code caches a value of 'environ' in a local static pointer 'env' and dereferences that. The old values in the old copy of 'environ' that 'env' points to are no longer valid. The code has been fixed using 'environ' directly rather than using the local static pointer.
1 parent 965f574 commit df9c3c2

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

runtime/flang/initpar.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
2+
* Copyright (c) 1995-2018, NVIDIA CORPORATION. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -404,7 +404,11 @@ __fort_getenv(const char *nm)
404404
int n;
405405

406406
n = strlen(nm);
407+
#if defined(TARGET_OSX)
407408
e = env;
409+
#else
410+
e = environ;
411+
#endif
408412
while (*e != NULL) {
409413
if ((strncmp(*e, nm, n) == 0) && ((*((*e) + n)) == '=')) {
410414
return ((*e) + n + 1);

0 commit comments

Comments
 (0)