Index: lib/Fuzzer/FuzzerUtil.cpp =================================================================== --- lib/Fuzzer/FuzzerUtil.cpp +++ lib/Fuzzer/FuzzerUtil.cpp @@ -113,11 +113,31 @@ void SetSigTermHandler() { SetSigaction(SIGTERM, InterruptHandler); } int NumberOfCpuCores() { - FILE *F = popen("nproc", "r"); + const char *cmdLine = nullptr; + if (LIBFUZZER_LINUX) { + cmdLine = "nproc"; + } else if (LIBFUZZER_APPLE) { + cmdLine = "sysctl -n hw.ncpu"; + } else { + assert(0 && "NumberOfCpuCores() is not implemented for your platform"); + } + + FILE *F = popen(cmdLine, "r"); int N = 0; - if (fscanf(F, "%d", &N) != 1) + if (!F || fscanf(F, "%d", &N) != 1) { + Printf("WARNING: Failed to parse output of command \"%s\" in %s(). " + "Assuming CPU count of 1.\n", + cmdLine, __func__); + N = 1; + } + + int PcloseRet = pclose(F); + if (PcloseRet != 0) { + Printf("WARNING: Executing command \"%s\" failed in %s(). ", + "Assuming CPU count of 1.\n", cmdLine, __func__); N = 1; - fclose(F); + } + assert(N > 0 && "Number of cpu cores must be > 0"); return N; }