Skip to content

Commit e9260f5

Browse files
authored
Guard against system call returning zero processors
1 parent 4cfd6f1 commit e9260f5

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

driver/others/init.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -877,34 +877,34 @@ void gotoblas_affinity_init(void) {
877877
nums = sysconf(_SC_NPROCESSORS_CONF);
878878

879879
#if !defined(__GLIBC_PREREQ)
880-
common->num_procs = nums;
880+
common->num_procs = nums >0 ? nums : 2;
881881
#else
882882

883883
#if !__GLIBC_PREREQ(2, 3)
884-
common->num_procs = nums;
884+
common->num_procs = nums >0 ? nums : 2;
885885
#elif __GLIBC_PREREQ(2, 7)
886-
cpusetp = CPU_ALLOC(nums);
886+
cpusetp = CPU_ALLOC(nums>0? nums:1024);
887887
if (cpusetp == NULL) {
888-
common->num_procs = nums;
888+
common->num_procs = nums>0 ? nums: 2;
889889
} else {
890890
size_t size;
891-
size = CPU_ALLOC_SIZE(nums);
891+
size = CPU_ALLOC_SIZE(nums>0? nums: 1024);
892892
ret = sched_getaffinity(0,size,cpusetp);
893893
if (ret!=0)
894-
common->num_procs = nums;
894+
common->num_procs = nums >0 ? nums : 1;
895895
else
896896
common->num_procs = CPU_COUNT_S(size,cpusetp);
897897
}
898898
CPU_FREE(cpusetp);
899899
#else
900900
ret = sched_getaffinity(0,sizeof(cpu_set_t), &cpuset);
901901
if (ret!=0) {
902-
common->num_procs = nums;
902+
common->num_procs = nums >0 ? nums : 2;
903903
} else {
904904
#if !__GLIBC_PREREQ(2, 6)
905905
int i;
906906
int n = 0;
907-
for (i=0;i<nums;i++)
907+
for (i=0;i<(nums >0 ?nums:1024) ;i++)
908908
if (CPU_ISSET(i,&cpuset)) n++;
909909
common->num_procs = n;
910910
}
@@ -1022,7 +1022,7 @@ void gotoblas_set_affinity2(int threads) {};
10221022

10231023
void gotoblas_affinity_reschedule(void) {};
10241024

1025-
int get_num_procs(void) { return sysconf(_SC_NPROCESSORS_CONF); }
1025+
int get_num_procs(void) { int num = sysconf(_SC_NPROCESSORS_CONF); return (nums >0 ? nums : 2); }
10261026

10271027
int get_num_nodes(void) { return 1; }
10281028

0 commit comments

Comments
 (0)