Fix crash from possible null return from getcwd.
This commit is contained in:
parent
bcb75e1476
commit
109812d223
@ -852,10 +852,11 @@ namespace {
|
||||
// directories (see ROOT-7114). By asking for $PWD (and not ".") it will
|
||||
// be registered as $PWD instead, which is stable even after chdirs.
|
||||
char cwdbuf[2048];
|
||||
const clang::DirectoryEntry* DE
|
||||
= FM.getDirectory(getcwd_func(cwdbuf, sizeof(cwdbuf)));
|
||||
(void)DE;
|
||||
assert(!strcmp(DE->getName(), cwdbuf) && "Unexpected name for $PWD");
|
||||
if (!getcwd_func(cwdbuf, sizeof(cwdbuf))) {
|
||||
// getcwd can fail, but that shouldn't mean we have to.
|
||||
::perror("Could not get current working directory");
|
||||
} else
|
||||
FM.getDirectory(cwdbuf);
|
||||
|
||||
// Build the virtual file, Give it a name that's likely not to ever
|
||||
// be #included (so we won't get a clash in clangs cache).
|
||||
|
@ -9,22 +9,32 @@
|
||||
// Test runing a file in the same directory `cling CurrentDir.C`
|
||||
// More info in CIFactory.cpp createCIImpl (line ~850)
|
||||
|
||||
// RUN: cd %S && %cling -Xclang -verify CurrentDir.C | FileCheck %s
|
||||
// Test CurrentDir
|
||||
// RUN: cd %S && %cling -Xclang -verify CurrentDir.C 2>&1 | FileCheck %s
|
||||
// RUN: mkdir %T/Remove && cd %T/Remove && rm -rf %T/Remove && %cling -DTEST_CWDRETURN %s -Xclang -verify 2>&1 | FileCheck --check-prefix CHECK --check-prefix CHECKcwd %s
|
||||
// Test testCurrentDir
|
||||
|
||||
extern "C" {
|
||||
int printf(const char*, ...);
|
||||
char* getcwd(char *buf, std::size_t size);
|
||||
}
|
||||
|
||||
#ifdef TEST_CWDRETURN
|
||||
// Make sure include still works
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
#endif
|
||||
|
||||
void CurrentDir() {
|
||||
char thisDir[1024];
|
||||
getcwd(thisDir, sizeof(thisDir));
|
||||
/*
|
||||
This would be nice, but doesn't work
|
||||
printf("We are here: %s\n", thisDir);
|
||||
-CHECK: We are here: %S
|
||||
*/
|
||||
#ifdef TEST_CWDRETURN
|
||||
char thisDir[1024];
|
||||
const char *rslt = getcwd(thisDir, sizeof(thisDir));
|
||||
// Make sure cling reported the error
|
||||
// CHECKcwd: Could not get current working directory: {{.*}}
|
||||
|
||||
if (rslt)
|
||||
printf("Working directory exists\n");
|
||||
// CHECK-NOT: Working directory exists
|
||||
#endif
|
||||
|
||||
printf("Script ran\n");
|
||||
// CHECK: Script ran
|
||||
|
Loading…
Reference in New Issue
Block a user